2014-04-24 15 views
0

當我試圖在對話框中添加片段時,該應用程序崩潰。崩潰說「沒有查看發現ID 0x01276」Xamarin中的自定義對話框中的片段事務期間發生崩潰Android

這是對話的佈局文件(my_dialog_layout.axml)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:id="@+id/fragment_container" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

</LinearLayout> 

這是在打開對話框和片段交易代碼

class CustomDialog : Dialog{ 
    public override void OnCreate(Bundle savedInstanceState) 
    { 
     base.OnCreate(savedInstanceState) 

     SetContentView(Resource.Layout.dialog_fragment_layout); 

     var myCustomFragmnent = new MyCustomFragment(_context); 

     // Start Fragment Transaction Process 
     var transaction = FragmentManager.BeginTransaction(); 

     // Here is a crash saying (No View found for ID 0x01276....) 
     transaction.Add(Resource.Id.fragment_container, myCustomFragmnent); 

     transaction.Commit();    
    } 

} 

回答

0

首先,您不需要使用「重」佈局,如​​LinearLayout,我建議您使用FrameLayout作爲您的容器。

其次,嘗試使用transaction.Replace來代替。另外請確保MyCustomFragment不會在OnCreateView中爆炸。這可能是您的問題所在,因爲您沒有發佈完整的堆棧跟蹤。您Replace通話後

transaction.AddToBackStack(null); 

,這樣當你按下手機上的後退按鈕它導航回出以前Fragment

當使用transaction.Replace你可以把它處理堆棧中爲您加入。