错误膨胀类片段引起:“片段没有创建视图。”

时间:2017-10-18 19:08:32

标签: java android android-fragments

当用户重新打开应用时,我的Android应用会出现此错误。应用程序第一次正常打开。它只是发生在一些设备(有时),我无法解决它。 Stackoverflow中有一些关于它的问题,但没有一个对我来说是正确的。

以下是Firebase崩溃的完整错误:

Exception java.lang.RuntimeException: Unable to start activity ComponentInfo{[REDACTED_DOMAIN_NAME]/com.app.Activities.Activities.PrincipalActivity}: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class fragment
android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2434)
android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2494)
android.app.ActivityThread.access$900 (ActivityThread.java:153)
android.app.ActivityThread$H.handleMessage (ActivityThread.java:1347)
android.os.Handler.dispatchMessage (Handler.java:102)
android.os.Looper.loop (Looper.java:148)
android.app.ActivityThread.main (ActivityThread.java:5451)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:726)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:616)


Caused by android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class fragment
android.view.LayoutInflater.inflate (LayoutInflater.java:539)
android.view.LayoutInflater.inflate (LayoutInflater.java:423)
android.view.LayoutInflater.inflate (LayoutInflater.java:374)
com.android.internal.policy.PhoneWindow.setContentView (PhoneWindow.java:393)
android.app.Activity.setContentView (Activity.java:2215)
comwe.app.Activities.Activities.PrincipalActivity.onCreate (PrincipalActivity.java:60)
android.app.Activity.performCreate (Activity.java:6323)
android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1108)
android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2387)
android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2494)
android.app.ActivityThread.access$900 (ActivityThread.java:153)
android.app.ActivityThread$H.handleMessage (ActivityThread.java:1347)
android.os.Handler.dispatchMessage (Handler.java:102)
android.os.Looper.loop (Looper.java:148)
android.app.ActivityThread.main (ActivityThread.java:5451)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:726)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:616)

Caused by android.view.InflateException: Binary XML file line #11: Error inflating class fragment
android.view.LayoutInflater.createViewFromTag (LayoutInflater.java:782)
android.view.LayoutInflater.createViewFromTag (LayoutInflater.java:704)
android.view.LayoutInflater.rInflate (LayoutInflater.java:835)
android.view.LayoutInflater.rInflateChildren (LayoutInflater.java:798)
android.view.LayoutInflater.inflate (LayoutInflater.java:515)
android.view.LayoutInflater.inflate (LayoutInflater.java:423)
android.view.LayoutInflater.inflate (LayoutInflater.java:374)
com.android.internal.policy.PhoneWindow.setContentView (PhoneWindow.java:393)
android.app.Activity.setContentView (Activity.java:2215)
com.app.Activities.Activities.PrincipalActivity.onCreate (PrincipalActivity.java:60)
android.app.Activity.performCreate (Activity.java:6323)
android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1108)
android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2387)
android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2494)
android.app.ActivityThread.access$900 (ActivityThread.java:153)
android.app.ActivityThread$H.handleMessage (ActivityThread.java:1347)
android.os.Handler.dispatchMessage (Handler.java:102)
android.os.Looper.loop (Looper.java:148)
android.app.ActivityThread.main (ActivityThread.java:5451)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:726)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:616)


Caused by java.lang.IllegalStateException: Fragment com.app.Activities.Activities.EmptyFragment did not create a view.
android.support.v4.app.FragmentManagerImpl.onCreateView (FragmentManager.java:3706)
android.support.v4.app.FragmentController.onCreateView (FragmentController.java:111)
android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView (FragmentActivity.java:338)
android.support.v4.app.BaseFragmentActivityApi14.onCreateView (BaseFragmentActivityApi14.java:39)
android.support.v4.app.FragmentActivity.onCreateView (FragmentActivity.java:67)
android.view.LayoutInflater.createViewFromTag (LayoutInflater.java:754)
android.view.LayoutInflater.createViewFromTag (LayoutInflater.java:704)
android.view.LayoutInflater.rInflate (LayoutInflater.java:835)
android.view.LayoutInflater.rInflateChildren (LayoutInflater.java:798)
android.view.LayoutInflater.inflate (LayoutInflater.java:515)
android.view.LayoutInflater.inflate (LayoutInflater.java:423)
android.view.LayoutInflater.inflate (LayoutInflater.java:374)
com.android.internal.policy.PhoneWindow.setContentView (PhoneWindow.java:393)
android.app.Activity.setContentView (Activity.java:2215)
com.app.Activities.Activities.PrincipalActivity.onCreate (PrincipalActivity.java:60)
android.app.Activity.performCreate (Activity.java:6323)
android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1108)
android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2387)
android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2494)
android.app.ActivityThread.access$900 (ActivityThread.java:153)
android.app.ActivityThread$H.handleMessage (ActivityThread.java:1347)
android.os.Handler.dispatchMessage (Handler.java:102)
android.os.Looper.loop (Looper.java:148)
android.app.ActivityThread.main (ActivityThread.java:5451)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:726)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:616)

的活动:

public class PrincipalActivity extends FragmentActivity {

    private FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_principal);

             mFragmentManager = getSupportFragmentManager();
        getSupportFragmentManager().beginTransaction().add(R.id.fragment_place, anotherFragment).commit();

活动XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:design="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_principal2"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.app.Activities.Activities.PrincipalActivity">

    <fragment
        android:id="@+id/fragment_place"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/divider"
        class = "com.app.Activities.Activities.EmptyFragment"
        />

片段XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.app.Activities.Activities.EmptyFragment">


</RelativeLayout>

片段类:

public class EmptyFragment extends Fragment {

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



        return v;
    }


}

1 个答案:

答案 0 :(得分:0)

尝试更改您的片段。

从:

<fragment
        android:id="@+id/fragment_place"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/divider"
        class = "com.app.Activities.Activities.EmptyFragment"
        />

为:

<fragment
        android:name="com.app.Activities.Activities.EmptyFragment"
        android:id="@+id/fragment_place"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/divider"
        />

并将此行代码移至PrincipalActivity

mFragmentManager = getSupportFragmentManager();
        getSupportFragmentManager().beginTransaction().add(R.id.fragment_place, anotherFragment).commit();

希望这有帮助!