我尝试将 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