2017-10-18 64 views
0

我的Android應用程序在用戶重新打開應用程序時出現此錯誤。應用第一次正常打開。它只發生在某些設備(有時),我無法解決它。在Stackoverflow中有一些關於它的問題,但是他們都沒有爲我正確回答。錯誤膨脹類片段引起:「片段沒有創建視圖。」

下面是從火力地堡崩潰完整的錯誤:

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

顯示您的EmptyFragment類。 –

+0

你忘了在Fragment類中實現'onCreateView'嗎? –

+0

@NoumanCh,我補充道。 –

回答

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(); 

希望得到這個幫助!

+0

這是我的第一個代碼。我改變了,因爲發生了同樣的問題,並在其他問題中說要這樣做。 –

+0

@RodrigoTaborda我試着用你的代碼,但我的工作。你能告訴我們EmptyFragment位於哪裏嗎? – Jerrol

+0

@RodrigoTaborda我認爲你需要仔細檢查你的'EmptyFragment',所以你可以修改'fragment'的'android:name'的值 – Jerrol

相關問題