在OnCreateView中膨胀视图

时间:2016-04-21 16:56:15

标签: android android-layout android-fragments xamarin xamarin.android

遵循Xamarin教程here我试图在axml文件中包含小部件(而不是像教程中那样以编程方式)。但是,当我调用Inflate方法时,我得到了例外:

  

Android.Views.InflateException:二进制XML文件行#1:错误   膨胀类片段

所以我的Fragment类如下:

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 个答案:

答案 0 :(得分: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" />

命名空间变得更低了。