0

我在自定义控件内膨胀 MvxImageView 时遇到问题。

从我所做的研究中,我发现了几个嫌疑人

  1. 如果是这样,我应该添加什么?(我在 android 地图视图膨胀错误方面看到了这一点。
  2. 我创建的用户控件都错了吗?我看到了使用 SetContentView() 而不是 inflate 的建议,因为它通过 mvvmcross 机制传递 indlate 请求,但自定义视图中没有这种方法。

    这是自定义控件布局:

    <?xml version="1.0" encoding="utf-8"?>    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:local="http://schemas.android.com/apk/res/Com.TasteITAndroidClient"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent" >
    <Mvx.MvxImageView android:scaleType="centerCrop"
                      android:id="@+id/Image"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content" />
    

这是 UserControl 的实现:

public class UserControl : LinearLayout
{
    public UserControl(Context context, IAttributeSet attrs) : base(context,attrs)
    {
        LayoutInflater inflater = LayoutInflater.From(context);
        inflater.Inflate (Resource.Layout.UserControl, this);
    }
}

这是我使用它的地方:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
    android:id="@+id/myButton"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />
    <TestApp.UserControl 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

我收到以下异常:

Android.Views.InflateException: Binary XML file line #1: Error inflating class Mvx.MvxImageView at Android.Runtime.JNIEnv.CallObjectMethod (intptr,intptr,Android.Runtime.JValue[]) [0x00024] in /Users/builder/data /lanes/monodroid-mlion-monodroid-4.8.0-branch/9486a4ec/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:145 在 Android.Views.LayoutInflater.Inflate (int,Android .Views.ViewGroup) [0x00043] 在 /Users/builder/data/lanes/monodroid-mlion-monodroid-4.8.0-branch/9486a4ec/source/monodroid/src/Mono.Android/platforms/android-8/src/在 c:\Users\Amit\Documents\Projects\TestApp\TestApp\ 中的 TestApp.UserControl..ctor (Android.Content.Context,Android.Util.IAttributeSet) [0x00011] 处生成/Android.Views.LayoutInflater.cs:422 UserControl.cs:20 at at (wrapper dynamic-method) object.bd915d4e-f205-49cd-a1e8-a0532b2db313 (intptr,object[]) 在 Java.Interop.TypeManager.n_Activate (intptr,intptr,intptr,intptr,intptr,intptr) [0x00070] 在 /Users/builder/data/lanes/monodroid-mlion-monodroid-4.8.0-branch/ 9486a4ec/source/monodroid/src/Mono.Android/src/Java.Interop/TypeManager.cs:141 在(包装器原生托管)Java.Interop.TypeManager.n_Activate(intptr,intptr,intptr,intptr,intptr ,intptr) at (wrapper managed-to-native) object.wrapper_native_0x40819b9d (intptr,intptr,intptr,Android.Runtime.JValue[]) <0x00047> at (wrapper delegate-invoke) .invoke_intptr_n_Activate (intptr,intptr,intptr,intptr,intptr,intptr) at (wrapper managed-to-native) object.wrapper_native_0x40819b9d (intptr,intptr,intptr,Android.Runtime.JValue[]) <0x00047> at (wrapper delegate -invoke) .invoke_intptr_n_Activate (intptr,intptr,intptr,intptr,intptr,intptr) at (wrapper managed-to-native) object.wrapper_native_0x40819b9d (intptr,intptr,intptr,Android.Runtime.JValue[]) <0x00047> at (wrapper delegate -invoke) .invoke_intptr_这个__intptr_intptr_intptr_JValue[] (intptr,intptr,intptr,Android.Runtime.JValue[]) 在 Android.Runtime.JNIEnv.CallObjectMethod (intptr,intptr,Android.Runtime.JValue[]) [0x00005] 在 /Users/builder/data/车道/monodroid-mlion-monodroid-4.8.0-branch/9486a4ec/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:141 在 Android.Views.LayoutInflater.Inflate (int,Android. Views.ViewGroup) [0x00043] 在 /Users/builder/data/lanes/monodroid-mlion-monodroid-4.8.0-branch/9486a4ec/source/monodroid/src/Mono.Android/platforms/android-8/src/generated /Android.Views.LayoutInflater.cs:422 在 Cirrious.MvvmCross.Binding.Droid.BindingContext.MvxAndroidBindingContext.CommonInflate (int,Android.Views.ViewGroup,Cirrious.MvvmCross.Binding.Droid.Binders.MvxBindingLayoutInflatorFactory) [0x0003c] 在 c :\Projects\Misc\MVVMCROSS\Cirrious\Cirrious.MvvmCross.Binding。Droid\BindingContext\MvxAndroidBindingContext.cs:58 在 Cirrious.MvvmCross.Binding.Droid.BindingContext.MvxAndroidBindingContext.BindingInflate (int,Android.Views.ViewGroup) [0x00001] 在 c:\Projects\Misc\MVVMCROSS\Cirrious\Cirrious.MvvmCross .Binding.Droid\BindingContext\MvxAndroidBindingContext.cs:37 在 Cirrious.MvvmCross.Binding.Droid.BindingContext.MvxBindingContextOwnerExtensions.BindingInflate (Cirrious.MvvmCross.Binding.BindingContext.IMvxBindingContextOwner,int,Android.Views.ViewGroup) [0x0000d] 在 c :\Projects\Misc\MVVMCROSS\Cirrious\Cirrious.MvvmCross.Binding.Droid\BindingContext\MvxBindingContextOwnerExtensions.cs:18 在 Cirrious.MvvmCross.Droid.Views.MvxActivity.SetContentView (int) [0x00001] 在 c:\Projects\Misc \MVVMCROSS\Cirrious\Cirrious.MvvmCross.Droid\Views\MvxActivity.cs:51 在 TestApp.Main.InnerViewModelSet () [0x00001] 在 c:\Users\Amit\Documents\Projects\TestApp\TestApp\MainActivity.cs:21 在 TestApp.BaseActivity1<TestApp.LoginPageViewModel>.OnViewModelSet () <0x00087> at Cirrious.MvvmCross.Droid.Views.MvxActivity.set_ViewModel (Cirrious.MvvmCross.ViewModels.IMvxViewModel) [0x00009] in c:\Projects\Misc\MVVMCROSS\Cirrious\Cirrious.MvvmCross.Droid\Views\MvxActivity.cs:38 at Cirrious.MvvmCross.Views.MvxViewExtensionMethods.OnViewCreate (Cirrious.MvvmCross.Views.IMvxView,System.Func1) [0x00055] 在 c:\Projects\Misc\MVVMCROSS\Cirrious\Cirrious.MvvmCross\Views\MvxViewExtensionMethods.cs:35 在 Cirrious.MvvmCross.Droid.Views.MvxActivityViewExtensions.OnViewCreate (Cirrious.MvvmCross.Droid.Views.IMvxAndroidView ,Android.OS.Bundle) [0x0006b] 在 c:\Projects\Misc\MVVMCROSS\Cirrious\Cirrious.MvvmCross.Droid\Views\MvxActivityViewExtensions.cs:50 在 Cirrious.MvvmCross.Droid.Views.MvxActivityAdapter.EventSourceOnCreateCalled (对象, Cirrious.CrossCore.Core.MvxValueEventArgs 1<Android.OS.Bundle>) [0x00001] in c:\Projects\Misc\MVVMCROSS\Cirrious\Cirrious.MvvmCross.Droid\Views\MvxActivityAdapter.cs:85 at at (wrapper delegate-invoke) System.EventHandler11<Android.OS.Bundle>>.invoke_void__this___object_TEventArgs (object,Cirrious.CrossCore.Core.MvxValueEventArgs1) <0x00067> at Cirrious.CrossCore.Droid.Views.MvxDelegateExtensionMethods.Raise (System.EventHandler1<Cirrious.CrossCore.Core.MvxValueEventArgs1>,object,Android.OS.Bundle) <0x00113> 在 Cirrious.CrossCore.Droid.Views.MvxEventSourceActivity.OnCreate (Android.OS.Bundle) [0x00009] 在 c:\Projects\Misc\MVVMCROSS\CrossCore\Cirrious。 CrossCore.Droid\Views\MvxEventSourceActivity.cs:23 at TestApp.BaseActivity`1.OnCreate (Android.OS.Bundle) <0x0005b> at TestApp.Main.OnCreate (Android.OS.Bundle) [0x00001] in c:\ Users\Amit\Documents\Projects\TestApp\TestApp\MainActivity.cs:16 在 Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) [0x00010] 在 /Users/builder/data/lanes/monodroid-mlion-monodroid -4.8.0-branch/9486a4ec/source/monodroid/src/Mono.Android/platforms/android-8/src/generated/Android.App.Activity.cs:1561 at (wrapper dynamic-method) object.302eab70- b743-4322-bd6e-c04ded2426ec (intptr,intptr,intptr) at --- End of managed exception stack trace --- at android.view.InflateException: Binary XML file line #1: Error inflating class Mvx.MvxImageView at at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java: 698) 在 android.view.LayoutInflater.rInflate(LayoutInflater.java:746) 在 android.view.LayoutInflater.inflate(LayoutInflater.java:489) 在 android.view.LayoutInflater.inflate(LayoutInflater.java:396)在 android.view.LayoutInflater.inflate(LayoutInflater.java:352) 在 mono.android.TypeManager.n_activate(Native Method) 在 mono.android.TypeManager.Activate(TypeManager.java:7) 在 testapp.UserControl .(UserControl.java:29) 在 java.lang.reflect.Constructor.constructNative(Native Method) 在 java.lang.reflect.Constructor.newInstance(Constructor.java:417) 在 android.view.LayoutInflater.createView(LayoutInflater.java:587) 在 android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687) 在 android.view.LayoutInflater.rInflate(LayoutInflater.java:746)在 android.view.LayoutInflater.inflate(LayoutInflater.java:489) 在 android.view.LayoutInflater.inflate(LayoutInflater.java:396) 在 android.view.LayoutInflater.inflate(LayoutInflater.java:352) 在testapp.Main.n_onCreate(Native Method) at testapp.Main.onCreate(Main.java:28) at android.app.Activity.performCreate(Activity.java:5104) at android.app.Instrumentation.callActivityOnCreate(Instrumentation .java:1080) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 在 android.app.ActivityThread。handleLaunchActivity(ActivityThread.java:2230) 在 android.app.ActivityThread.access$600(ActivityThread.java:141) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 在 android.os.Handler .dispatchMessage(Handler.java:99) 在 android.os.Looper.loop(Looper.java:137) 在 android.app.ActivityThread.main(ActivityThread.java:5039) 在 java.lang.reflect.Method .invokeNative(Native Method) 位于 java.lang.reflect.Method.invoke(Method.java:511) 位于 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 位于 com。 android.internal.os.ZygoteInit.main(ZygoteInit.java:560) at dalvik.system.NativeStart.main(Native Method) at Caused by: java.lang.ClassNotFoundException: 找不到类“Mvx.MvxImageView”路径:/data/app/TestApp。TestApp-1.apk at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65) at java.lang.ClassLoader.loadClass(ClassLoader.java:501) at java.lang.ClassLoader.loadClass(ClassLoader.java :461) at android.view.LayoutInflater.createView(LayoutInflater.java:552) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687) at ... 31 更多

4

1 回答 1

2

我对你在做什么感到困惑 - 你似乎试图在非数据绑定环境中膨胀数据绑定对象。

这两个关于使用子控件的教程可能会有所帮助:

或者,由于您的布局没有自定义功能 - 它只是 XML - 那么也许您可以考虑只使用 Android <include>xml 块?见http://developer.android.com/training/improving-layouts/reusing-layouts.html

于 2013-07-25T17:46:28.827 回答