在 OnCreateView 中膨胀视图

Inflating view within OnCreateView

遵循 Xamarin 教程 here 我正在尝试将小部件包含在 axml 文件中(而不是像教程中那样以编程方式)。但是,当我调用 Inflate 方法时,出现异常:

Android.Views.InflateException: Binary XML file line #1: Error inflating class fragment

所以我的片段class如下:

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>

详细活动:

[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 布局?我在这里做错了什么?

您必须使用命名空间更改片段的名称。

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" />

命名空间小写。