1

我尝试将 Google 地图添加到我的应用程序并使用教程生成 API 代码,添加所有必要的元数据、权限等。每当我单击按钮开始新活动时,应用程序在我的 Galaxy S3 上崩溃

onClickListener

Button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v == Button) {
                    startActivity(new Intent(MainActivity.this, ShowMap.class));
                }
            }
        });

地图类

public class ShowMap extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show_map);

    }
}

show_map.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"/>

错误

03-15 20:14:51.825: E/AndroidRuntime(18318): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.appname.subappname/com.appname.subappname.ShowMap}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
03-15 20:14:51.825: E/AndroidRuntime(18318):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
03-15 20:14:51.825: E/AndroidRuntime(18318):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
03-15 20:14:51.825: E/AndroidRuntime(18318):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
03-15 20:14:51.825: E/AndroidRuntime(18318):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
03-15 20:14:51.825: E/AndroidRuntime(18318):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-15 20:14:51.825: E/AndroidRuntime(18318):    at android.os.Looper.loop(Looper.java:137)
03-15 20:14:51.825: E/AndroidRuntime(18318):    at android.app.ActivityThread.main(ActivityThread.java:4898)
03-15 20:14:51.825: E/AndroidRuntime(18318):    at java.lang.reflect.Method.invokeNative(Native Method)
03-15 20:14:51.825: E/AndroidRuntime(18318):    at java.lang.reflect.Method.invoke(Method.java:511)
03-15 20:14:51.825: E/AndroidRuntime(18318):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
03-15 20:14:51.825: E/AndroidRuntime(18318):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
03-15 20:14:51.825: E/AndroidRuntime(18318):    at dalvik.system.NativeStart.main(Native Method)
03-15 20:14:51.825: E/AndroidRuntime(18318): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
03-15 20:14:51.825: E/AndroidRuntime(18318):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
03-15 20:14:51.825: E/AndroidRuntime(18318):    at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
03-15 20:14:51.825: E/AndroidRuntime(18318):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
03-15 20:14:51.825: E/AndroidRuntime(18318):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
03-15 20:14:51.825: E/AndroidRuntime(18318):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:308)
03-15 20:14:51.825: E/AndroidRuntime(18318):    at android.app.Activity.setContentView(Activity.java:1924)
03-15 20:14:51.825: E/AndroidRuntime(18318):    at com.appname.subappname.ShowMap.onCreate(ShowMap.java:11)
03-15 20:14:51.825: E/AndroidRuntime(18318):    at android.app.Activity.performCreate(Activity.java:5206)
03-15 20:14:51.825: E/AndroidRuntime(18318):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
03-15 20:14:51.825: E/AndroidRuntime(18318):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
03-15 20:14:51.825: E/AndroidRuntime(18318):    ... 11 more
03-15 20:14:51.825: E/AndroidRuntime(18318): Caused by: java.lang.ClassCastException: com.google.android.gms.maps.MapFragment cannot be cast to android.support.v4.app.Fragment
03-15 20:14:51.825: E/AndroidRuntime(18318):    at android.support.v4.app.Fragment.instantiate(Fragment.java:394)
03-15 20:14:51.825: E/AndroidRuntime(18318):    at android.support.v4.app.Fragment.instantiate(Fragment.java:369)
03-15 20:14:51.825: E/AndroidRuntime(18318):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
03-15 20:14:51.825: E/AndroidRuntime(18318):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
03-15 20:14:51.825: E/AndroidRuntime(18318):    ... 20 more
4

2 回答 2

2

com.google.android.gms.maps.MapFragment找不到该类。检查库google-play-services_lib是否正确链接到项目。

于 2013-03-15T17:38:55.430 回答
1

您的 ShowMap 应该FragmentActivity对此进行扩展以使其无法正常工作Activity,请检查 google-play-service 库中包含的示例以获取 google 地图,它们是一个很大的帮助。

编辑

确保您包含 google-play-service 库项目,因为例外情况是ClassNotFoundException: com.google.android.gms.maps.MapFragment

检查链接以了解如何包含库项目

于 2013-03-15T17:32:12.470 回答