我们正在尝试使用ZXingScannerFragment在我们的主要活动中嵌入ZXing条码扫描仪。
我们的布局快照:
…
<fragment
class="ZXing.Mobile.ZXingScannerFragment"
android:id="@+id/asasast"
android:layout_width="match_parent"
android:layout_height="113.3dp" />
…
我们在运行时遇到的异常:
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及更高版本)
谢谢!
最佳答案
我们是这样整合的
在axml文件中定义了framelayout,如下所示
<FrameLayout android:id="@+id/fragment_container" android:layout_width="wrap_content" android:layout_above="@+id/layoutBottom" android:layout_height="match_parent" />
在我们的活动中
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();
}