1

我正在开发一个 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
4

1 回答 1

1

您将 API V2 与 Google Maps 的 API V1 混为一谈。所以首先决定你要使用哪个 API。

接下来,如果您决定使用 API V2(正如您的大多数代码所显示的那样),则删除此行:

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

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

您需要决定的第二件事是minAPI您的应用程序的级别。如果您将应用程序编写到 API 级别 < 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 个步骤:

谷歌地图 API V2

于 2013-04-23T14:28:44.840 回答