从3.5升级到3.5.1后,我在Xamarin.Android项目中出现错误,导致碎片膨胀。
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
导致此错误的原因是什么? 3.5.1中是否有重大变化?
My Fragment定义为
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);
}
}
我已经通过Xamarin Component商店提供了V4支持。
我通过以下方式给我的片段充气:
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;
}
...
}
更新1
我的axml布局如下
<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" />
更新2
当我构建物理设备时它工作正常
更新3
如果按照CheeseBaron的建议添加[Register]
,则原因会更改为:
[MonoDroid] Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class com.namespace.Droid.Views.LoginFormView that is not a Fragment
更多信息:
[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>
答案 0 :(得分:3)
Xamarin.Android 5.1引入了一些重大变化。这意味着您需要在所有片段中添加[Register("app.namespace.FragmentName")]
。
有关更多信息,请参阅: 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
编辑:
OK!这与#990无关。问题是由于某些原因MvxFragment
不能被夸大,因为它不被识别为片段。当改为最小目标15和FullFragging MvvmCross nuget时,它开始工作。
还要记住,所有 Java命名空间必须是小写的。所以当你使用[Register]
时,你必须遵守它。
您的示例还有两个MainLauncher
个活动,因此我必须删除默认的MainActivity
。您的FirstViewFragment
也定义了Activity
,为什么会这样做?
所以我做了简短的改变:
[Activity]
FirstViewFragment
个标记
[Register]
FirstViewFragment
中的小写命名空间
FirstView
从MvxFragmentActivity
更改为MvxActivity
MainActivity
SupportFragmentManager
替换为FragmentManager
class
FirstView.axml
中的小写命名空间
然后它有效。为什么MvvmCross中的支持版本的框架不起作用需要进行调查。