Xamarin Android Fragment ZXing - ClassCastException

Posted

技术标签:

【中文标题】Xamarin Android Fragment ZXing - ClassCastException【英文标题】: 【发布时间】:2015-07-09 20:27:07 【问题描述】:

我们正在尝试使用 ZXingScannerFragment 将 ZXing 条码扫描器嵌入到我们的主要活动中。

我们的布局快照:

...

<fragment
    class="ZXing.Mobile.ZXingScannerFragment"
    android:id="@+id/asasast"
    android:layout_
    android:layout_ />

...

我们在运行时遇到的异常:

    Android.Views.InflateException: Binary XML file line #1: Error inflating class fragment
      at at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () <IL 0x00011, 0x00068>
      at Android.Runtime.JNIEnv.CallObjectMethod (intptr,intptr,Android.Runtime.JValue[]) [0x00064] in /Users/builder/data/lanes/1353/86274adf/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:195
      at Android.Views.LayoutInflater.Inflate (int,Android.Views.ViewGroup,bool) [0x00043] in /Users/builder/data/lanes/1353/86274adf/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.Views.LayoutInflater.cs:609
      at at Cirrious.MvvmCross.Binding.Droid.BindingContext.MvxAndroidBindingContext.CommonInflate (int,Android.Views.ViewGroup,Cirrious.MvvmCross.Binding.Droid.Binders.IMvxLayoutInfactorFactory,bool) <IL 0x00033, 0x00213>
      at at Cirrious.MvvmCross.Binding.Droid.BindingContext.MvxAndroidBindingContext.BindingInflate (int,Android.Views.ViewGroup,bool) <IL 0x00015, 0x00107>
      at at Cirrious.MvvmCross.Binding.Droid.BindingContext.MvxAndroidBindingContext.BindingInflate (int,Android.Views.ViewGroup) <IL 0x00004, 0x00063>
      at at Cirrious.MvvmCross.Binding.Droid.BindingContext.MvxBindingContextOwnerExtensions.BindingInflate (Cirrious.MvvmCross.Binding.BindingContext.IMvxBindingContextOwner,int,Android.Views.ViewGroup) <IL 0x0000f, 0x00157>
      at at Cirrious.MvvmCross.Droid.Views.MvxActivity.SetContentView (int) <IL 0x00003, 0x00053>
      at SuperSmartV1.MainActivity.OnViewModelSet () [0x00008] in c:\Dropbox\SuperSmart\baraka\Xamarin\Projects\SuperSmart\AndroidDemo\MainActivity.cs:35
      at at Cirrious.MvvmCross.Droid.Views.MvxActivity.set_ViewModel (Cirrious.MvvmCross.ViewModels.IMvxViewModel) <IL 0x00008, 0x0006f>
      at at Cirrious.MvvmCross.Views.MvxViewExtensionMethods.OnViewCreate (Cirrious.MvvmCross.Views.IMvxView,System.Func`1<Cirrious.MvvmCross.ViewModels.IMvxViewModel>) <IL 0x0003f, 0x0023f>
      at at Cirrious.MvvmCross.Droid.Views.MvxActivityViewExtensions.OnViewCreate (Cirrious.MvvmCross.Droid.Views.IMvxAndroidView,Android.OS.Bundle) <IL 0x00073, 0x00317>
      at at Cirrious.MvvmCross.Droid.Views.MvxActivityAdapter.EventSourceOnCreateCalled (object,Cirrious.CrossCore.Core.MvxValueEventArgs`1<Android.OS.Bundle>) <IL 0x0000c, 0x0008f>
      at at (wrapper delegate-invoke) System.EventHandler`1<Cirrious.CrossCore.Core.MvxValueEventArgs`1<Android.OS.Bundle>>.invoke_void_object_TEventArgs (object,Cirrious.CrossCore.Core.MvxValueEventArgs`1<Android.OS.Bundle>) <0x00067>
      at at Cirrious.CrossCore.Core.MvxDelegateExtensionMethods.Raise<Android.OS.Bundle> (System.EventHandler`1<Cirrious.CrossCore.Core.MvxValueEventArgs`1<Android.OS.Bundle>>,object,Android.OS.Bundle) <0x000c3>
      at at Cirrious.CrossCore.Droid.Views.MvxEventSourceActivity.OnCreate (Android.OS.Bundle) <IL 0x0001c, 0x000a7>
      at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) [0x00011] in /Users/builder/data/lanes/1353/86274adf/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.App.Activity.cs:2179
      at at (wrapper dynamic-method) object.d9e98bb6-3579-4bde-b37e-c62fa5dacd65 (intptr,intptr,intptr) <IL 0x00017, 0x00043>
      at --- End of managed exception stack trace ---
      at android.view.InflateException: Binary XML file line #1: Error inflating class fragment
      at at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
      at at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
      at at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
      at at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
      at at cirrious.crosscore.droid.views.MvxEventSourceActivity.n_onCreate(Native Method)
      at at cirrious.crosscore.droid.views.MvxEventSourceActivity.onCreate(MvxEventSourceActivity.java:38)
      at at android.app.Activity.performCreate(Activity.java:5933)
      at at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
      at at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2282)
      at at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
      at at android.app.ActivityThread.access$900(ActivityThread.java:147)
      at at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296)
      at at android.os.Handler.dispatchMessage(Handler.java:102)
      at at android.os.Looper.loop(Looper.java:135)
      at at android.app.ActivityThread.main(ActivityThread.java:5254)
      at at java.lang.reflect.Method.invoke(Native Method)
      at at java.lang.reflect.Method.invoke(Method.java:372)
      at at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
      at at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
      at Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class zxing.mobile.ZXingScannerFragment that is not a Fragment
      at at android.app.Fragment.instantiate(Fragment.java:606)
      at at android.app.Fragment.instantiate(Fragment.java:582)
      at at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2108)
      at at android.app.Activity.onCreateView(Activity.java:5282)
      at at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
      at ... 18 more
      at Caused by: java.lang.ClassCastException
      at ... 23 more

我们对这个错误有点不知所措:“尝试实例化一个不是片段的类 zxing.mobile.ZXingScannerFragment”,因为 ZXingScannerFragment 显然是一个片段......

    public class ZXingScannerFragment : Fragment

感谢任何提示:)

(使用 ZXing.Net.Mobile 1.4.7.2 和 Android 目标版本 4 及更高版本)

谢谢!

【问题讨论】:

嘿。你有想过这个吗?我目前正在处理这个问题。 没有 :( 将在接下来的几周内重试。如果您有见解,我会很感激让我知道 :) 【参考方案1】:

我们是这样整合的

在axml文件中定义了一个FrameLayout如下图

<FrameLayout android:id="@+id/fragment_container" android:layout_ android:layout_above="@+id/layoutBottom" android:layout_ />

在我们的活动中

var needsPermissionRequest = ZXing.Net.Mobile.Android.PermissionsHandler.NeedsPermissionRequest(this);

        if (needsPermissionRequest)
            ZXing.Net.Mobile.Android.PermissionsHandler.RequestPermissionsAsync(this);

        if (scanFragment == null)
        
            scanFragment = new ZXingScannerFragment();
            SupportFragmentManager.BeginTransaction()
                .Replace(Resource.Id.fragment_container, scanFragment)
                .Commit();
            SupportFragmentManager.ExecutePendingTransactions();
        

【讨论】:

以上是关于Xamarin Android Fragment ZXing - ClassCastException的主要内容,如果未能解决你的问题,请参考以下文章

Xamarin Android 片段库

无法将类型“Android.Support.V4.App.Fragment”转换为“Android.Gms.Maps.MapFragment”

MVP架构在xamarin android中的简单使用

如何将 PageRenderer/Fragment/View 中的 ZXing 扫描仪嵌入 Xamarin.Forms ContentPage?

Xamarin 表单 - 侧边菜单左侧栏、片段和列表视图

Xamarin AAR结合。某些类不包含在绑定中