Android Google地图活动无法启动

时间:2013-04-23 13:49:00

标签: android android-layout android-intent google-maps-android-api-2

我正在开发一款Android应用,应该从列表中选择游览时启动MapView。然而,每当我尝试通过Intent打开这个MapView时,应用程序似乎都会崩溃......

这是我的代码:

MainActivity.java:

public class MainActivity extends Activity {
    public final static String SELECTED_TOUR = "com.me.tourapp.TOUR";
    public static TourListAdapter tourListAdapter;
    private static int selectedTour;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
    }

    public void startTour(View view) {
        Intent intent = new Intent(view.getContext(), TourMapActivity.class);
        intent.putExtra(SELECTED_TOUR, selectedTour);
        startActivity(intent);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        ...
    }

}

TourMapActivity.java:

public class TourMapActivity extends Activity {

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tour_map);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }

        //check if tour is passed via intent (test):
        Intent intent = getIntent();
        int tourIndex = Integer.parseInt(intent.getStringExtra(MainActivity.SELECTED_TOUR));
        Toast.makeText(getApplicationContext(), "the tour: "+ tourIndex, Toast.LENGTH_LONG).show();
    }
}

activity_tour_map.xml:

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

    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.MapFragment" />

</LinearLayout>

以及清单中的一些片段:

<permission
    android:name="com.me.tourapp.permission.MAPS_RECEIVE"
    android:protectionLevel="signature"/>
<uses-permission android:name="com.me.tourapp.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<uses-library android:name="com.google.android.maps" />

<meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="AIzaSyBqs_*****"/>

我还将android-support-v4.jar包含在java构建路径中并安装并导入了google play服务......

我只是不知道我在犯错误的地方......

编辑: 我添加了logcat:

E/AndroidRuntime(28248): FATAL EXCEPTION: main
E/AndroidRuntime(28248): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hansvn.plaktour/com.hansvn.plaktour.TourMapActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
E/AndroidRuntime(28248):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
E/AndroidRuntime(28248):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
E/AndroidRuntime(28248):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
E/AndroidRuntime(28248):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
E/AndroidRuntime(28248):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(28248):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(28248):    at android.app.ActivityThread.main(ActivityThread.java:4898)
E/AndroidRuntime(28248):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(28248):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(28248):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
E/AndroidRuntime(28248):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
E/AndroidRuntime(28248):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(28248): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
E/AndroidRuntime(28248):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
E/AndroidRuntime(28248):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
E/AndroidRuntime(28248):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
E/AndroidRuntime(28248):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
E/AndroidRuntime(28248):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
E/AndroidRuntime(28248):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:308)
E/AndroidRuntime(28248):    at android.app.Activity.setContentView(Activity.java:1924)
E/AndroidRuntime(28248):    at com.hansvn.plaktour.TourMapActivity.onCreate(TourMapActivity.java:16)
E/AndroidRuntime(28248):    at android.app.Activity.performCreate(Activity.java:5206)
E/AndroidRuntime(28248):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
E/AndroidRuntime(28248):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
E/AndroidRuntime(28248):    ... 11 more
E/AndroidRuntime(28248): Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.SupportMapFragment: make sure class name exists, is public, and has an empty constructor that is public
E/AndroidRuntime(28248):    at android.support.v4.app.Fragment.instantiate(Fragment.java:401)
E/AndroidRuntime(28248):    at android.support.v4.app.Fragment.instantiate(Fragment.java:369)
E/AndroidRuntime(28248):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
E/AndroidRuntime(28248):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
E/AndroidRuntime(28248):    ... 21 more
E/AndroidRuntime(28248): Caused by: java.lang.ClassNotFoundException: com.google.android.gms.maps.SupportMapFragment
E/AndroidRuntime(28248):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
E/AndroidRuntime(28248):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
E/AndroidRuntime(28248):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
E/AndroidRuntime(28248):    at android.support.v4.app.Fragment.instantiate(Fragment.java:391)
E/AndroidRuntime(28248):    ... 24 more

1 个答案:

答案 0 :(得分:1)

您将API V2与Google地图的API V1混淆。因此,首先要确定您要使用哪个API。

接下来,如果您已经决定使用API​​ V2(因为它看起来大部分代码都是如此),请删除此行:

<uses-library android:name="com.google.android.maps" />

它是API V1的一部分,不需要在V2中。

您需要决定的第二件事是应用程序的minAPI级别。如果您正在将应用程序编写为API级别&lt; 11然后你应该在你的XML中使用SupportMapFragment,如下所示:

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

<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment" />

并且您的活动应该延长FragmentActivity,这是您应该使用的情况 the google-support-v4包,因为FragmentActivity是此包的一部分。

<强>更新 从您的logcat输出:

 Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.me.tourapp/com.me.tourapp.TourMapActivity}; have you declared this activity in your AndroidManifest.xml?

这意味着您尚未在清单文件中声明您的Activity TourMapActivity,这就是您出错的原因。

<强> UPDATE2: 这个错误:

Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.SupportMapFragment: make sure class name exists, is public, and has an empty constructor that is public.

从错误中所说的派生SupportMapFragment类没有找到。 这意味着您没有以正确的方式引用google-play-services库,因为此类是此库的一部分。请阅读我写的关于如何正确引用它的博客文章的前3个步骤:

Google Map API V2