2017-04-21 82 views
2

我需要幫助。我得到了Crashlytics的崩潰:沒有找到id的片段

Fatal Exception: java.lang.IllegalArgumentException: No view found for id 0x7f0f016c (com.my.my:id/fragment_root_layout) for fragment a{2e8d61e1 #4 id=0x7f0f016c fragment_tag} 

我使用片段進入片段。在父片段的OnCreate方法中,我使用幫助FragmentManager添加子片段。我有一個想法,問題是在OnCreate方法中添加子片段,並且需要在OnCreateView中執行此操作。但我不確定。事情是我不能再現這次崩潰,但我在Crashlytic上得到這個崩潰。 父級片段裝載的所有視圖都有一個ID。只有當我在OnCreateView方法中膨脹不正確的佈局時,我才能重現此崩潰,但在實時構建中不會發生這種情況。 請給我一個建議。

編輯:添加子片段

準則
@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     LogUtils.verbose(TAG, "displayOverlayFragment: attempt to add a fragment using FragmentTransaction"); 
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
     OverlayFragment overlayFragment = new OverlayFragment(); 
     fragmentTransaction.add(R.id.fragment_root_layout, overlayFragment, FRAGMENT_TAG); 
     fragmentTransaction.commit(); 
    } 

代碼onCreateView的:

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(getLayoutResId(), container, false); 
     mRecyclerView = initRecyclerView(view); 

     mProgressBar = view.findViewById(R.id.progressBar); 
     mProgressBarLabel = (TextView) view.findViewById(R.id.progressBarLabel); 
     } 

     return view; 
} 
@Override 
    protected int getLayoutResId() { 
     return R.layout.fragment_favorites; 
    } 
+0

你確定存在一個ID爲'fragment_root_layout'的視圖嗎? –

+0

嘗試將子片段添加到父片段的「onViewCreated」方法中。在此之前,您不應使用父母版面中的任何ID。 – hibob

+0

發佈你的代碼到目前爲止你已經嘗試過嗎? – FAT

回答

0

也許你要申報proguard-rules.pro東西在你的發佈版本?你有沒有檢查它是否僅在發佈版本中發生?我認爲你的調試版忽略了proguard,它的工作正常,但如果你檢查你的發佈版本,你將有機會重現你的崩潰。也許一些片段子組件應該在proguard中聲明。

+0

謝謝,它是有道理的。但我仍然無法重現它,會嘗試更多。 – LeShChEnKoUa

0

對於android中的片段,您需要在onStart()方法中對它們進行編碼。並使用onCreateView()返回你想要的特定片段的佈局

相關問題