Android - 启动时“IllegalStateException:片段未创建视图”

时间:2016-01-17 18:57:50

标签: java android android-fragments fragment

似乎某种程度上我错过了一些东西,因为在我的应用程序被最小化(没有关闭)之后,然后它的一些内存被清除,应用程序将在恢复它时崩溃。例如,通过最小化应用程序,快速打开其他8个应用程序,然后切换到我的应用程序,可以100%重现崩溃。

我已经在这里查看了这个问题,但没有发现任何相关内容(没有替换我创建的XML片段,我的super.onCreate在setContentView之前等)。

确切的错误(FragmentManager.java的第2325行)是:

if (fragment.mView == null) {
        throw new IllegalStateException("Fragment " + fname
                + " did not create a view.");
    }

错误:

01-17 13:32:34.902 16520-16520/com.spielpark.steve.bernieapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           Process: com.spielpark.steve.bernieapp, PID: 16520
                                                                           java.lang.RuntimeException: Unable to start activity ComponentInfo{com.spielpark.steve.bernieapp/com.spielpark.steve.bernieapp.actMainPage}: android.view.InflateException: Binary XML file line #18: Error inflating class fragment
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3143)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3242)
                                                                               at android.app.ActivityThread.access$1000(ActivityThread.java:205)
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1695)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                               at android.os.Looper.loop(Looper.java:145)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:6895)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at java.lang.reflect.Method.invoke(Method.java:372)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
                                                                            Caused by: android.view.InflateException: Binary XML file line #18: Error inflating class fragment
                                                                               at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:770)
                                                                               at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
                                                                               at android.view.LayoutInflater.rInflate(LayoutInflater.java:821)
                                                                               at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
                                                                               at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
                                                                               at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
                                                                               at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:257)
                                                                               at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
                                                                               at com.spielpark.steve.bernieapp.actMainPage.onCreate(actMainPage.java:52)
                                                                               at android.app.Activity.performCreate(Activity.java:6500)
                                                                               at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120)
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3096)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3242) 
                                                                               at android.app.ActivityThread.access$1000(ActivityThread.java:205) 
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1695) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                               at android.os.Looper.loop(Looper.java:145) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:6895) 
                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                               at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 
                                                                            Caused by: java.lang.IllegalStateException: Fragment com.spielpark.steve.bernieapp.fragments.BottomNavFragment did not create a view.
                                                                               at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2325)
                                                                               at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:111)
                                                                               at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:314)
                                                                               at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:31)
                                                                               at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:79)
                                                                               at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:740)
                                                                               at android.view.LayoutInflater.rInflate(LayoutInflater.java:813) 
                                                                               at android.view.LayoutInflater.rInflate(LayoutInflater.java:821) 
                                                                               at android.view.LayoutInflater.inflate(LayoutInflater.java:511) 
                                                                               at android.view.LayoutInflater.inflate(LayoutInflater.java:415) 
                                                                               at android.view.LayoutInflater.inflate(LayoutInflater.java:366) 
                                                                               at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:257) 
                                                                               at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109) 
                                                                               at com.spielpark.steve.bernieapp.actMainPage.onCreate(actMainPage.java:52) 
                                                                               at android.app.Activity.performCreate(Activity.java:6500) 
                                                                               at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120) 
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3096) 
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3242) 
                                                                               at android.app.ActivityThread.access$1000(ActivityThread.java:205) 
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1695) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                               at android.os.Looper.loop(Looper.java:145) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:6895) 
                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                               at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 

片段:

public class BottomNavFragment extends Fragment {

private static BottomNavFragment mInstance;

public static BottomNavFragment getInstance() {
    if (mInstance == null) {
        mInstance = new BottomNavFragment();
        return mInstance;
    } else {
        return mInstance;
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.frag_bottom_nav, container, false);
}
}

XML:

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".actMainPage">

<RelativeLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#F1F1F1">

    <fragment
        android:id="@+id/bottom_navigation"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:name="com.spielpark.steve.bernieapp.fragments.BottomNavFragment"
        tools:layout="@layout/frag_bottom_nav"
        android:layout_alignParentBottom="true"/>

</RelativeLayout>

frag_bottom_nav.xml:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="60dp"
tools:context="com.spielpark.steve.bernieapp.fragments.BottomNavFragment"
android:background="#147FD7"
android:weightSum="4"
android:orientation="vertical">

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:weightSum="4.0">

    <ImageView
        android:id="@+id/newsPic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_public_white_24dp"
        android:layout_weight="1.0"
        android:onClick="switchPage"
        android:tag="0"/>

    <ImageView
        android:id="@+id/issuesPic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_event_note_white_24dp"
        android:layout_weight="1.0"
        android:onClick="switchPage"
        android:tag="1"/>
    <ImageView
        android:id="@+id/organizePic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_people_white_24dp"
        android:layout_weight="1.0"
        android:onClick="switchPage"
        android:tag="2"/>

    <ImageView
        android:id="@+id/connectPic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_map_white_24dp"
        android:layout_weight="1.0"
        android:onClick="switchPage"
        android:tag="3"/>

</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="4.0"
    android:layout_marginBottom="4dp"
    android:paddingBottom="4dp">

    <TextView
        android:id="@+id/newsTxt"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="News"
        android:textColor="#FFFFFF"
        android:layout_weight="1.0"
        android:gravity="center_horizontal"
        android:onClick="switchPage"
        android:tag="0"/>

    <TextView
        android:id="@+id/issuesTxt"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Issues"
        android:textColor="#FFFFFF"
        android:layout_weight="1.0"
        android:gravity="center_horizontal"
        android:onClick="switchPage"
        android:tag="1"/>

    <TextView
        android:id="@+id/organizeTxt"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Organize"
        android:textColor="#FFFFFF"
        android:layout_weight="1.0"
        android:gravity="center_horizontal"
        android:onClick="switchPage"
        android:tag="2"/>

    <TextView
        android:id="@+id/connectTxt"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Connect"
        android:textColor="#FFFFFF"
        android:layout_weight="1.0"
        android:gravity="center_horizontal"
        android:onClick="switchPage"
        android:tag="3"/>
</LinearLayout>

..最后,主要活动。 actMainPage.42是setContentView();

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_main_page);
    mNavigationDrawerFragment = (NavigationDrawerFragment)
            getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
    mTitle = getTitle();
    // Set up the drawer.
    mNavigationDrawerFragment.setUp(
            R.id.navigation_drawer,
            (DrawerLayout) findViewById(R.id.drawer_layout));
    ActionBar bar = getSupportActionBar();
    bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#147FD7")));
    mTitle = "News";
    preferences = getApplicationContext().getSharedPreferences("bernie_app_prefs", 0);
    adjustNavBarText(0);
}

非常感谢您提供的任何帮助!如果您需要更多信息,请与我们联系。

0 个答案:

没有答案