user1400995
user1400995

Reputation:

Inflating view within OnCreateView

Following the Xamarin tutorial here I am trying to include widgets within the axml file (and not programmatically as in the tutorial). However when I invoke the Inflate method I get the Exception:

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

So my Fragment class is as follows:

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();
    }
}

Why is the main_selector layout not recognized? What am I doing wrong here?

Upvotes: 0

Views: 62

Answers (1)

Sven-Michael St&#252;be
Sven-Michael St&#252;be

Reputation: 14750

You have to change the name of your fragment using your namespace.

For a definition of TitlesFragment that looks like this:

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

you have to use:

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

The namespace gets lower cased.

Upvotes: 1

Related Questions