【问题标题】:Inflating view within OnCreateViewOnCreateView 中的膨胀视图
【发布时间】:2016-04-22 04:28:32
【问题描述】:

按照 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;
    }
}

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

【问题讨论】:

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


    【解决方案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" />
    

    命名空间变为小写。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-23
      • 1970-01-01
      • 2019-03-09
      • 1970-01-01
      • 1970-01-01
      • 2016-09-29
      相关资源
      最近更新 更多