当我运行我的应用程序时,我收到此错误:
01-06 15:00:42.865: E/AndroidRuntime(24230): FATAL EXCEPTION: main
01-06 15:00:42.865: E/AndroidRuntime(24230): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fotolife.app/com.fotolife.app.MainView}: android.view.InflateException: Binary XML file line #8: Error inflating class com.google.android.maps.MapView
01-06 15:00:42.865: E/AndroidRuntime(24230): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
01-06 15:00:42.865: E/AndroidRuntime(24230): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
01-06 15:00:42.865: E/AndroidRuntime(24230): at android.app.ActivityThread.access$600(ActivityThread.java:140)
01-06 15:00:42.865: E/AndroidRuntime(24230): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
01-06 15:00:42.865: E/AndroidRuntime(24230): at android.os.Handler.dispatchMessage(Handler.java:99)
01-06 15:00:42.865: E/AndroidRuntime(24230): at android.os.Looper.loop(Looper.java:137)
01-06 15:00:42.865: E/AndroidRuntime(24230): at android.app.ActivityThread.main(ActivityThread.java:4898)
01-06 15:00:42.865: E/AndroidRuntime(24230): at java.lang.reflect.Method.invokeNative(Native Method)
01-06 15:00:42.865: E/AndroidRuntime(24230): at java.lang.reflect.Method.invoke(Method.java:511)
01-06 15:00:42.865: E/AndroidRuntime(24230): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
01-06 15:00:42.865: E/AndroidRuntime(24230): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
01-06 15:00:42.865: E/AndroidRuntime(24230): at dalvik.system.NativeStart.main(Native Method)
01-06 15:00:42.865: E/AndroidRuntime(24230): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class com.google.android.maps.MapView
01-06 15:00:42.865: E/AndroidRuntime(24230): at android.view.LayoutInflater.createView(LayoutInflater.java:613)
01-06 15:00:42.865: E/AndroidRuntime(24230): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
01-06 15:00:42.865: E/AndroidRuntime(24230): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
01-06 15:00:42.865: E/AndroidRuntime(24230): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
01-06 15:00:42.865: E/AndroidRuntime(24230): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-06 15:00:42.865: E/AndroidRuntime(24230): at com.fotolife.app.Map.onCreateView(Map.java:12)
01-06 15:00:42.865: E/AndroidRuntime(24230): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:871)
01-06 15:00:42.865: E/AndroidRuntime(24230): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1083)
01-06 15:00:42.865: E/AndroidRuntime(24230): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:635)
01-06 15:00:42.865: E/AndroidRuntime(24230): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1431)
01-06 15:00:42.865: E/AndroidRuntime(24230): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:523)
01-06 15:00:42.865: E/AndroidRuntime(24230): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1178)
01-06 15:00:42.865: E/AndroidRuntime(24230): at android.app.Activity.performStart(Activity.java:5201)
01-06 15:00:42.865: E/AndroidRuntime(24230): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2073)
01-06 15:00:42.865: E/AndroidRuntime(24230): ... 11 more
01-06 15:00:42.865: E/AndroidRuntime(24230): Caused by: java.lang.reflect.InvocationTargetException
01-06 15:00:42.865: E/AndroidRuntime(24230): at java.lang.reflect.Constructor.constructNative(Native Method)
01-06 15:00:42.865: E/AndroidRuntime(24230): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
01-06 15:00:42.865: E/AndroidRuntime(24230): at android.view.LayoutInflater.createView(LayoutInflater.java:587)
01-06 15:00:42.865: E/AndroidRuntime(24230): ... 24 more
01-06 15:00:42.865: E/AndroidRuntime(24230): Caused by: java.lang.IllegalArgumentException: MapViews can only be created inside instances of MapActivity.
01-06 15:00:42.865: E/AndroidRuntime(24230): at com.google.android.maps.MapView.<init>(MapView.java:291)
01-06 15:00:42.865: E/AndroidRuntime(24230): at com.google.android.maps.MapView.<init>(MapView.java:264)
01-06 15:00:42.865: E/AndroidRuntime(24230): at com.google.android.maps.MapView.<init>(MapView.java:247)
01-06 15:00:42.865: E/AndroidRuntime(24230): ... 27 more
它说这个文件的第 12 行有错误:
package com.fotolife.app;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Map extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.map, container, false);
}
}
第 12 行是:return inflater.inflate .....
这是片段应该膨胀的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" >
<com.google.android.maps.MapView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="@string/maps_api_key"
/>
</LinearLayout>