我尝试将Google地图添加到我的应用程序并使用this教程我生成了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
答案 0 :(得分:2)
找不到课程com.google.android.gms.maps.MapFragment
。检查库google-play-services_lib
是否与项目正确链接。
答案 1 :(得分:1)
你的ShowMap应该扩展FragmentActivity
,使其无法工作Activity
,查看google-play-sevice库中包含的google地图示例,这些都是一个很大的帮助。
修改强>
确保您包含google-play-service库项目,例外情况为ClassNotFoundException: com.google.android.gms.maps.MapFragment
检查this链接,了解如何包含库项目