0

繼Xamarin教程here我想將小部件包含在axml文件中(而不是像本教程中那樣以編程方式)。然而,當我調用膨脹方法我得到異常:在OnCreateView內充氣視圖

Android.Views.InflateException:二進制XML文件行#1:錯誤 充氣類片段

所以我的碎片等級如下表所示:

DetailsFragment.cs:

internal class DetailsFragment : Fragment 
{ 
    public static DetailsFragment NewInstance(int playId) 
    { 
     var detailsFrag = new DetailsFragment { Arguments = new Bundle() }; 
     detailsFrag.Arguments.PutInt("current_play_id", playId); 
     return detailsFrag; 
    } 
    public int ShownPlayId 
    { 
     get { return Arguments.GetInt("current_play_id", 0); } 
    } 
    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    { 
     if (container == null) 
     { 
      // Currently in a layout without a container, so no reason to create our view. 
      return null; 
     } 

     **// ** FAILS HERE ===>** 
     View view = inflater.Inflate(Resource.Layout.main_selector, container, false); 
     var text = view.FindViewById<TextView>(Resource.Id.textViewDetails); 
     text.Text = Shakespeare.Dialogue[ShownPlayId]; 
     return view; 
    } 
} 

main_selector.axml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <fragment 
     class="com.sample.TitlesFragment" 
     android:id="@+id/titles_fragment" 
     android:layout_weight="1" 
     android:layout_width="0px" 
     android:layout_height="match_parent" /> 
    <FrameLayout 
     android:id="@+id/details" 
     android:layout_weight="1" 
     android:layout_width="0px" 
     android:layout_height="match_parent" 
     android:minWidth="25px" 
     android:minHeight="25px"> 
     <ScrollView 
      android:minWidth="25px" 
      android:minHeight="25px" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:id="@+id/scrollView1"> 
     <TextView 
       android:text="Text" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:id="@+id/textViewDetails" /> 
     </ScrollView> 
    </FrameLayout> 
</LinearLayout> 

DetailsActivity:

[Activity(Label = "Details Activity", Theme = "@style/Theme.NoTitle")] 
public class DetailsActivity : Activity 
{ 
    protected override void OnCreate(Bundle bundle) 
    { 
     base.OnCreate(bundle); 

     var index = Intent.Extras.GetInt("current_play_id", 0); 

     var details = DetailsFragment.NewInstance(index); 
     var fragmentTransaction = FragmentManager.BeginTransaction(); 
     fragmentTransaction.Add(Android.Resource.Id.Content, details); 
     fragmentTransaction.Commit(); 
    } 
} 

爲什麼main_selector佈局不承認?我在這裏做錯了什麼?

回答

1

您必須使用您的名稱空間更改片段的名稱。

對於TitlesFragment的定義,看起來像這樣:

namespace MyCompany.MyApp 
{ 
    public class TitlesFragment : ListFragment 
    { 
     // ... 
    } 
} 

,你必須使用:

<fragment 
    class="mycompany.myapp.TitlesFragment" 
    android:id="@+id/titles_fragment" 
    android:layout_weight="1" 
    android:layout_width="0px" 
    android:layout_height="match_parent" /> 

命名空間被下套管。