Java 应用程序在后台被杀死后MapFragment崩溃

Java 应用程序在后台被杀死后MapFragment崩溃,java,android,mapfragment,Java,Android,Mapfragment,像这样在我的项目上实现googlemap 从xml <fragment android:id="@+id/map" android:layout_width="match_parent" android:layout_height="250dp" class="com.google.android.gms.maps.MapFragment"/> 即使当我从一个片段切换到另一个片段时,这也可以很好地工作。 然而,过了一段

像这样在我的项目上实现googlemap

从xml

<fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        class="com.google.android.gms.maps.MapFragment"/>
即使当我从一个片段切换到另一个片段时,这也可以很好地工作。 然而,过了一段时间,比如当应用程序在后台运行了很长时间,当我再次尝试打开应用程序时,它只是使应用程序崩溃,我得到了这个错误

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ticketing.app/com.ticketing.app.Main}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
            at android.app.ActivityThread.access$600(ActivityThread.java:162)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
            at android.os.Handler.dispatchMessage(Handler.java:107)
            at android.os.Looper.loop(Looper.java:194)
            at android.app.ActivityThread.main(ActivityThread.java:5371)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
            at com.ticketing.app.Search.onCreateView(Search.java:55)
            at android.app.Fragment.performCreateView(Fragment.java:1695)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
            at android.app.BackStackRecord.run(BackStackRecord.java:694)
            at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
            at android.app.Activity.performStart(Activity.java:5131)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2316)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
            at android.app.ActivityThread.access$600(ActivityThread.java:162)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
            at android.os.Handler.dispatchMessage(Handler.java:107)
            at android.os.Looper.loop(Looper.java:194)
            at android.app.ActivityThread.main(ActivityThread.java:5371)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.IllegalArgumentException: Binary XML file line #7: Duplicate id 0x7f0a0065, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.MapFragment
            at android.app.Activity.onCreateView(Activity.java:4740)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
            at com.ticketing.app.Search.onCreateView(Search.java:55)
            at android.app.Fragment.performCreateView(Fragment.java:1695)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
            at android.app.BackStackRecord.run(BackStackRecord.java:694)
            at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
            at android.app.Activity.performStart(Activity.java:5131)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2316)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
            at android.app.ActivityThread.access$600(ActivityThread.java:162)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
            at android.os.Handler.dispatchMessage(Handler.java:107)
            at android.os.Looper.loop(Looper.java:194)
            at android.app.ActivityThread.main(ActivityThread.java:5371)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at dalvik.system.NativeStart.main(Native Method)
清单文件是:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="package name is here" >


    <!-- To enable the app display map, you need these permission below. without any of these permission, the app will crash.
       -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/logo"
        android:label="@string/app_name"
        android:theme="@style/CustomActionBarTheme" >
        <activity
            android:name=".Launcher"
            android:label="Appname" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Create_Account"
            android:label="" >
        </activity>
        <activity
            android:name=".Sign_In"
            android:label="" >
        </activity>
        <activity
            android:name=".Main"
            android:label="Search" >
        </activity>
        <activity
            android:name=".Select_Bus"
            android:label="@string/title_activity_select__bus"
            android:parentActivityName=".Main" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.ticketing.app.Main" />
        </activity>

        <!-- the metadata helps store value that will probably be different across apps but should be accessible in the same way
        the ones below, help store the API key and a google play service integer. Note that these information have to be provided
        when using google play service and the map API.
        -->
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version"/>
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="api key for map is here"/>

    </application>

</manifest>


我该如何解决这个问题。

我认为这个错误可以忽略,应用程序应该可以正常加载。 试试这个:

View rootView;

public View onCreateView(LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState)
    {
        // The last two arguments ensure LayoutParams are inflated
        // properly.
        if (rootView != null)
        {
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (parent != null)
                parent.removeView(rootView);
        }

        try
        {
            rootView = inflater.inflate( R.layout.yourMapFragment, container, false );
        }
        catch( InflateException ex )
        {
        }

        ...
        return rootView;
    }

请把你的主要文件寄出去,我刚寄了。问题已更新查看清单文件中的本教程我认为您错过了在此处添加一些权限:
View rootView;

public View onCreateView(LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState)
    {
        // The last two arguments ensure LayoutParams are inflated
        // properly.
        if (rootView != null)
        {
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (parent != null)
                parent.removeView(rootView);
        }

        try
        {
            rootView = inflater.inflate( R.layout.yourMapFragment, container, false );
        }
        catch( InflateException ex )
        {
        }

        ...
        return rootView;
    }