【问题标题】:I cannot get Google Maps API to work on my android Application (V2)我无法让 Google Maps API 在我的 android 应用程序 (V2) 上工作
【发布时间】:2014-04-17 12:09:52
【问题描述】:

ma​​in2.xml 中显然没有错误。 main.xml 也是如此,但我认为问题出在某个地方。
该应用程序的快速摘要:它旨在使用 Google Maps onClick 一个按钮,但我遇到了错误,因此我可以在测试时加载主菜单。 XML
我没有足够的代表来发布图片,所以可以找到错误图片here

ma​​in2.xml

    <?xml version="1.0" encoding="utf-8"?>

     <fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.google.android.gms.maps.MapFragment"/> 

ma​​in.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Club Deals"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="GPS locations" />
    <Button
        android:id="@+id/button2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Closest Deals"
        android:onClick="open_close_deals" />

    <Button
        android:id="@+id/button6"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Cheapest Deals"
        android:onClick="open_cheap_deals" />


    <Button
        android:id="@+id/button7"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Best Value Deals"
        android:onClick="best_value" />

    <Button
        android:id="@+id/button8"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Best Events"
        android:onClick="best_events" />

    <Button
        android:id="@+id/button9"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="News" 
        android:onClick="news"/>
</LinearLayout>

AppActivity.java

在 AppActivity 中给我 "map 无法解析或不是字段" & "main2 无法解析或不是字段"

package com.example.clubnightsdeals;
import android.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;

public class AppActivity extends Activity {

    Button button;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    //  addListenerOnButton();
    }

    public void addListenerOnButton() {

        final Context context = this;
        button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent intent = new Intent(getApplicationContext(), App2Activity.class);
                startActivity(intent);   

            }
        });
    };
        public void addListenerOnButtonNews() {

        final Context context = this;

        button = (Button) findViewById(R.id.button9);

        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent intent = new Intent(getApplicationContext(), App2Activity.class);
                startActivity(intent);   

            }
        });

        };

    /* protected void onCreate1(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main2);
            }

        */
         // Google Map
         GoogleMap googleMap;

        protected void onCreate1(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main2);

            try {
                // Loading map
                initilizeMap();

            } catch (Exception e) {
                e.printStackTrace();
            }

        } 

        /**
         * function to load map. If map is not created it will create it for you
         * */
        private void initilizeMap() {
            if (googleMap == null) {
                googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                        R.id.map)).getMap();

                // check if map is created successfully or not
                if (googleMap == null) {
                    Toast.makeText(getApplicationContext(),
                            "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                            .show();
                }
            }
        }

        @Override
        protected void onResume() {

            super.onResume();
            initilizeMap();
        } 

}      

App2Activity 中我收到错误 “无法解析导入 com.example.clubnightsdeals.R”

App2Activity.java

package com.example.clubnightsdeals;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import com.example.clubnightsdeals.R;

public class App2Activity extends Activity {

    Button button;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);
    }
 }  

清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.clubnightsdeals"
    android:versionCode="1"
    android:versionName="1.0" >
  <permission
        android:name="info.androidhive.googlemapsv2.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />
    <uses-sdk
        android:minSdkVersion="12"
        android:targetSdkVersion="18" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <!-- Required to show current location -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <!-- Required OpenGL ES 2.0. for Maps V2 -->
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

     <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/clubnightsdeals"
            android:name=".AppActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
       <uses-library
        android:name="com.google.android.maps" />
        <activity
            android:label="@string/clubnightsdeals"
            android:name=".App2Activity" >
        </activity>

        <meta-data
     android:name="com.google.android.maps.v2.API_KEY"
     android:value="AIzaSyDetfYqNFORnvtR245fht3RIK25T6DRY" />
          <activity
            android:name="info.androidhive.googlemapsv2.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppBaseTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>



</manifest>

【问题讨论】:

  • 你必须在你的活动中import com.example.clubnightsdeals.R;
  • 尝试在main2.xml 中将fragment 放入FrameLayout 或将片段的类设置为SupportMapFragment
  • 您的最低 api 级别是多少?

标签: java android xml eclipse google-maps


【解决方案1】:

您指向错误的 R.java 文件。替换您的以下导入行

import android.R;

import com.example.clubnightsdeals.R; 

在您的 AppActivity.java 文件中。

【讨论】:

  • 嘿,我把它放进去了,但现在我得到了 The import com.example.clubnightsdeals.R cannot be resolve
  • 替换该行后,您需要清理一次项目。
  • 嘿,完成后错误似乎仍然存在。 i.imgur.com/wTP9kaX.png我添加了这张显示控制台错误的图片
  • @user2849976,由于代理问题,我看不到那个链接,你能把你的图片发到别的地方吗?
  • @user2849976 这表明在你的清单文件中你没有关闭 标签。关闭您的活动标签&lt;activity......./&gt;
【解决方案2】:

您的错误表明您尚未关闭清单文件中的 &lt;activity&gt; 标记。
确保您已将活动标签关闭为 &lt;activity android:name=""...../&gt;&lt;activity android:name=""&gt; &lt;/activity&gt;

【讨论】:

  • 感谢您的帮助。我把它贴在描述中。据我所知,我确实关闭了它(显然我错了)。
  • @user2849976 那么我想你的问题现在已经解决了。
  • 不,我的意思是它似乎已经关闭。它仍然给我同样的错误/
  • 尝试清理你的项目@user2849976
  • 不幸的是,这也行不通。我正在考虑重新开始。
【解决方案3】:

先删除

<intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

在清单中(应用程序中不能有 2 个启动活动)然后尝试导入 R 库。
如果仍然出现此 R 错误,请检查您的 xml 文件 - 确保检查所有 xml 文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-14
    相关资源
    最近更新 更多