Chris Koiak
Chris Koiak

Reputation: 1059

What's the proper way to implement Fragments in MvvmCross 3.5.1

After upgrading from 3.5 to 3.5.1 I am getting errors inflating Fragments in my Xamarin.Android project.

Unable to start activity ComponentInfo{xxx.xxx/md51537e9497dba76724980c8d1dff85301.MyView}: android.view.InflateException: Binary XML file line #1: Error inflating class fragment


Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class md51537e9497dba76724980c8d1dff85301.LoginView that is not a Fragment

What is causing this error? Are there breaking changes in 3.5.1?

My Fragment is defined as

public class LoginFormView : MvxFragment
    {
        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            var ignore = base.OnCreateView(inflater, container, savedInstanceState);
        return this.BindingInflate(Resource.Layout.LoginFormView, null);
    }
}

I have included the V4 support via the Xamarin Component store.

I inflate my fragment via:

public class LoginView : BaseMvxFragmentActivity
    {
...

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.LoginView);

        _loginForm = (LoginFormView)SupportFragmentManager.FindFragmentById(Resource.Id.sub_login_form);
        _loginForm.ViewModel = MyViewModel.LoginFormViewModel;
    }
    ...
 }

Update 1

My axml layout is as follows

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">
<!-- Snip -->
<LinearLayout
    android:id="@+id/InnerRelativeLayout"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:animateLayoutChanges="true"
    android:padding="10dp">
    <fragment
        class="com.namespace.LoginFormView"
        android:id="@+id/sub_login_form"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        local:MvxBind="Visibility ShowLoginFormViewModel,Converter=Visibility" />

Update 2

It works fine when I build to a physical device

Update 3

If I add [Register] as per CheeseBaron's suggestion then the cause changes to:

[MonoDroid] Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class com.namespace.Droid.Views.LoginFormView that is not a Fragment

More info:

[MonoDroid] UNHANDLED EXCEPTION:
[MonoDroid] Android.Views.InflateException: Exception of type 'Android.Views.InflateException' was thrown.
[MonoDroid] at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () <IL 0x00011, 0x0004b>
[MonoDroid] at Android.Runtime.JNIEnv.CallObjectMethod (intptr,intptr,Android.Runtime.JValue*) [0x00064] in /Users/builder/data/lanes/monodroid-mavericks-monodroid-5.1-series/d23da369/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:195
[MonoDroid] at Android.Views.LayoutInflater.Inflate (int,Android.Views.ViewGroup,bool) [0x00082] in /Users/builder/data/lanes/monodroid-mavericks-monodroid-5.1-series/d23da369/source/monodroid/src/Mono.Android/platforms/android-16/src/generated/Android.Views.LayoutInflater.cs:687
[MonoDroid] at Cirrious.MvvmCross.Binding.Droid.BindingContext.MvxAndroidBindingContext.CommonInflate (int,Android.Views.ViewGroup,Cirrious.MvvmCross.Binding.Droid.Binders.IMvxLayoutInfactorFactory,bool) <IL 0x00030, 0x00136>
[MonoDroid] at Cirrious.MvvmCross.Binding.Droid.BindingContext.MvxAndroidBindingContext.BindingInflate (int,Android.Views.ViewGroup,bool) <IL 0x00015, 0x000a5>
[MonoDroid] at Cirrious.MvvmCross.Binding.Droid.BindingContext.MvxAndroidBindingContext.BindingInflate (int,Android.Views.ViewGroup) <IL 0x00004, 0x00044>
[MonoDroid] at Cirrious.MvvmCross.Binding.Droid.BindingContext.MvxBindingContextOwnerExtensions.BindingInflate (Cirrious.MvvmCross.Binding.BindingContext.IMvxBindingContextOwner,int,Android.Views.ViewGroup) <IL 0x0000f, 0x000f5>
[MonoDroid] at Cirrious.MvvmCross.Droid.Fragging.MvxFragmentActivity.SetContentView (int) <IL 0x00003, 0x00037>
[MonoDroid] at com.namespace.Droid.Views.LoginView.OnCreate (Android.OS.Bundle) [0x0000e] in /xxx/Ccom.namespace.Droid/Views/LoginView.cs:34
[MonoDroid] at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) [0x00011] in /Users/builder/data/lanes/monodroid-mavericks-monodroid-5.1-series/d23da369/source/monodroid/src/Mono.Android/platforms/android-16/src/generated/Android.App.Activity.cs:2374
[MonoDroid] at (wrapper dynamic-method) object.facfe989-fd2b-40d3-bf63-959b844af987 (intptr,intptr,intptr) <IL 0x00017, 0x0002b>

Upvotes: 4

Views: 2605

Answers (1)

Cheesebaron
Cheesebaron

Reputation: 24470

Xamarin.Android 5.1 introduces some breaking changes. This means you need to add [Register("app.namespace.FragmentName")] in all your Fragments.

For more information see: https://github.com/MvvmCross/MvvmCross/issues/987 http://forums.xamarin.com/discussion/37277/stable-release-xamarin-android-5-1-0-breaking-changes-new-features-and-bug-fixes

EDIT:

OK! This is not related to #990. The problem is that for some reason the MvxFragment cannot be inflated as it is not recognized as a Fragment. When changing to minimum target 15 and the FullFragging MvvmCross nuget instead it starts to work.

Also remember, that all Java namespaces need to be lowercase. So when you use [Register] you must conform to that.

Your sample is also having two MainLauncher Activities, so I had to remove the default MainActivity. Your FirstViewFragment also defines an Activity, why does it do that???

So changes in short I made:

  • Minimum target 15
  • Replace Fragging with FullFragging
  • Remove [Activity] flags from FirstViewFragment
  • Lowercase namespace in [Register] in FirstViewFragment
  • Change FirstView from MvxFragmentActivity to MvxActivity
  • Remove MainActivity
  • Replace SupportFragmentManager with FragmentManager
  • Lowercase namespace in class in FirstView.axml

Then it works. Why the support version of framents in MvvmCross does not work needs to be investigated.

Upvotes: 3

Related Questions