使用Xamarin对绑定库进行膨胀时的ClassNotFoundException

时间:2015-01-23 15:04:50

标签: android xamarin

我为twoway-view库制作了自定义绑定。

DLL生成似乎没问题,我可以毫无问题地执行以下操作:

var twoWayView = new Org.Lucasr.TwoWayView.Widget.TwoWayView(this);

但是如果尝试使用java反射实例化该类:

Java.Lang.Class.ForName("Org.Lucasr.TwoWayView.Widget.TwoWayView")

我遇到了一个ClassNotFoundException,它与尝试使用TwoWayView的布局一样膨胀时发生了同样的异常:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    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" />
    <Org.Lucasr.Twowayview.Widget.TwoWayView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:scrollbars="vertical"
        app:twowayview_layoutManager="StaggeredGridLayoutManager"
        app:twowayview_numColumns="2"
        app:twowayview_numRows="1" />
</LinearLayout>

修改

如上所述使用dexdump here我可以看到该类在我的apk中:

Class #1172            -
  Class descriptor  : 'Lorg/lucasr/twowayview/widget/TwoWayView;'
  Access flags      : 0x0001 (PUBLIC)
  Superclass        : 'Landroid/support/v7/widget/RecyclerView;'
  Interfaces        -

修改2

使用org.lucasr.twowayview.widget.TwoWayView给我同样的错误。

完整的例外是:

01-23 13:02:04.837 I/MonoDroid(17779): UNHANDLED EXCEPTION: 
01-23 13:02:04.861 I/MonoDroid(17779): Android.Views.InflateException: Exception of type 'Android.Views.InflateException' was thrown. 
01-23 13:02:04.861 I/MonoDroid(17779): at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () <IL 0x00011, 0x00068> 
01-23 13:02:04.861 I/MonoDroid(17779): at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (intptr,intptr,intptr,Android.Runtime.JValue[]) [0x00084] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.21-series/49a04b96/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:896 
01-23 13:02:04.861 I/MonoDroid(17779): at Android.App.Activity.SetContentView (int) [0x00070] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.21-series/49a04b96/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.App.Activity.cs:4253 
01-23 13:02:04.861 I/MonoDroid(17779): at Org.Lucasr.TwoWayView.Sample.MainActivity.OnCreate (Android.OS.Bundle) [0x0001d] in c:\Users\guilherme\Documents\Visual Studio 2013\Projects\Org.Lucasr.TwoWayView\Org.Lucasr.TwoWayView.Sample\MainActivity.cs:25 
01-23 13:02:04.862 I/MonoDroid(17779): at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) [0x00011] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.21-series/49a04b96/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.App.Activity.cs:2179 
01-23 13:02:04.862 I/MonoDroid(17779): at (wrapper dynamic-method) object.e4603f87-2e86-4a8a-9cd2-213f87378350 (intptr,intptr,intptr) <IL 0x00017, 0x00043> 
01-23 13:02:04.862 I/MonoDroid(17779):   --- End of managed exception stack trace --- 
01-23 13:02:04.862 I/MonoDroid(17779): android.view.InflateException: Binary XML file line #1: Error inflating class Org.Lucasr.Twowayview.Widget.TwoWayView 
01-23 13:02:04.862 I/MonoDroid(17779):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:757) 
01-23 13:02:04.862 I/MonoDroid(17779):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 
01-23 13:02:04.862 I/MonoDroid(17779):  at android.view.LayoutInflater.inflate(LayoutInflater.java:504) 
01-23 13:02:04.862 I/MonoDroid(17779):  at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
01-23 13:02:04.862 I/MonoDroid(17779):  at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
01-23 13:02:04.862 I/MonoDroid(17779):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377) 
01-23 13:02:04.862 I/MonoDroid(17779):  at android.app.Activity.setContentView(Activity.java:2144) 
01-23 13:02:04.862 I/MonoDroid(17779):  at org.lucasr.twowayview.sample.MainActivity.n_onCreate(Native Method) 
01-23 13:02:04.862 I/MonoDroid(17779):  at org.lucasr.twowayview.sample.MainActivity.onCreate(MainActivity.java:28) 
01-23 13:02:04.862 I/MonoDroid(17779):  at android.app.Activity.performCreate(Activity.java:5933) 
01-23 13:02:04.862 I/MonoDroid(17779):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 
01-23 13:02:04.862 I/MonoDroid(17779):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251) 
01-23 13:02:04.862 I/MonoDroid(17779):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
01-23 13:02:04.862 I/MonoDroid(17779):  at android.app.ActivityThread.access$800(ActivityThread.java:144) 
01-23 13:02:04.862 I/MonoDroid(17779):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
01-23 13:02:04.862 I/MonoDroid(17779):  at android.os.Handler.dispatchMessage(Handler.java:102) 
01-23 13:02:04.862 I/MonoDroid(17779):  at android.os.Looper.loop(Looper.java:135) 
01-23 13:02:04.862 I/MonoDroid(17779):  at android.app.ActivityThread.main(ActivityThread.java:5221) 
01-23 13:02:04.862 I/MonoDroid(17779):  at java.lang.reflect.Method.invoke(Native Method) 
01-23 13:02:04.862 I/MonoDroid(17779):  at java.lang.reflect.Method.invoke(Method.java:372) 
01-23 13:02:04.862 I/MonoDroid(17779):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
01-23 13:02:04.862 I/MonoDroid(17779):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
01-23 13:02:04.862 I/MonoDroid(17779): Caused by: java.lang.ClassNotFoundException: Didn't find class "Org.Lucasr.Twowayview.Widget.TwoWayView" on path: DexPathList[[zip file "/data/app/Org.Lucasr.TwoWayView.Sample-1/base.apk"],nativeLibraryDirectories=[/data/app/Org.Lucasr.TwoWayView.Sample-1/lib/arm, /vendor/lib, /system/lib]] 
01-23 13:02:04.862 I/MonoDroid(17779):  at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56) 
01-23 13:02:04.862 I/MonoDroid(17779):  at java.lang.ClassLoader.loadClass(ClassLoader.java:511) 
01-23 13:02:04.862 I/MonoDroid(17779):  at java.lang.ClassLoader.loadClass(ClassLoader.java:469) 
01-23 13:02:04.862 I/MonoDroid(17779):  at android.view.LayoutInflater.createView(LayoutInflater.java:571) 
01-23 13:02:04.862 I/MonoDroid(17779):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743) 
01-23 13:02:04.862 I/MonoDroid(17779):  ... 21 more 
01-23 13:02:04.862 I/MonoDroid(17779):  Suppressed: java.lang.ClassNotFoundException: Org.Lucasr.Twowayview.Widget.TwoWayView 
01-23 13:02:04.862 I/MonoDroid(17779):      at java.lang.Class.classForName(Native Method) 
01-23 13:02:04.862 I/MonoDroid(17779):      at java.lang.BootClassLoader.findClass(ClassLoader.java:781) 
01-23 13:02:04.863 I/MonoDroid(17779):      at java.lang.BootClassLoader.loadClass(ClassLoader.java:841) 
01-23 13:02:04.863 I/MonoDroid(17779):      at java.lang.ClassLoader.loadClass(ClassLoader.java:504) 
01-23 13:02:04.863 I/MonoDroid(17779):      ... 24 more 
01-23 13:02:04.863 I/MonoDroid(17779):  Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available An unhandled exception occured.  
01-23 13:02:05.453 E/mono    (17779):  
01-23 13:02:05.453 E/mono    (17779): Unhandled Exception: 
01-23 13:02:05.453 E/mono    (17779): Android.Views.InflateException: Exception of type 'Android.Views.InflateException' was thrown. 
01-23 13:02:05.453 E/mono (17779): at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () <IL 0x00011, 0x00068> 
01-23 13:02:05.453 E/mono    (17779): at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (intptr,intptr,intptr,Android.Runtime.JValue[]) [0x00084] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.21-series/49a04b96/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:896 
01-23 13:02:05.453 E/mono    (17779): at Android.App.Activity.SetContentView (int) [0x00070] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.21-series/49a04b96/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.App.Activity.cs:4253 
01-23 13:02:05.453 E/mono    (17779): at Org.Lucasr.TwoWayView.Sample.MainActivity.OnCreate (Android.OS.Bundle) [0x0001d] in c:\Users\guilherme\Documents\Visual Studio 2013\Projects\Org.Lucasr.TwoWayView\Org.Lucasr.TwoWayView.Sample\MainActivity.cs:25 
01-23 13:02:05.453 E/mono    (17779): at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) [0x00011] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.21-series/49a04b96/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.App.Activity.cs:2179 
01-23 13:02:05.453 E/mono    (17779): at (wrapper dynamic-method) object.e4603f87-2e86-4a8a-9cd2-213f87378350 (intptr,intptr,intptr) <IL 0x00017, 0x00043> 
01-23 13:02:05.453 E/mono    (17779):  
01-23 13:02:05.453 E/mono    (17779):   --- End of managed exception stack trace --- 
01-23 13:02:05.453 E/mono    (17779): android.view.InflateException: Binary XML file line #1: Error inflating class Org.Lucasr.Twowayview.Widget.TwoWayView 
01-23 13:02:05.453 E/mono    (17779):   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:757) 
01-23 13:02:05.453 E/mono    (17779):   at android.view.LayoutInflater.inflate(LayoutInflater.java:504) 
01-23 13:02:05.453 E/mono    (17779):   at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
01-23 13:02:05.453 E/mono    (17779):   at android.view.LayoutInflater.inflate(LayoutInflater.java:365) referenceTable GDEF length=670 1 referenceTable GSUB length=7202 1 referenceTable GPOS length=24560 1 
01-23 13:02:05.453 E/mono    (17779):   at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377) 
01-23 13:02:05.453 E/mono    (17779):   at android.app.Activity.setContentView(Activity.java:2144) 
01-23 13:02:05.453 E/mono    (17779):   at org.lucasr.twowayview.sample.MainActivity.n_onCreate(Native Method) 
01-23 13:02:05.453 E/mono    (17779):   at org.lucasr.twowayview.sample.MainActivity.onCreate(MainActivity.java:28) 
01-23 13:02:05.453 E/mono    (17779):   at android.app.Activity.performCreate(Activity.java:5933) 
01-23 13:02:05.453 E/mono    (17779):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 
01-23 13:02:05.453 E/mono    (17779):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251) 
01-23 13:02:05.453 E/mono    (17779):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
01-23 13:02:05.453 E/mono    (17779):   at android.app.ActivityThread.access$800(ActivityThread.java:144) 
01-23 13:02:05.453 E/mono    (17779):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
01-23 13:02:05.453 E/mono    (17779):   at android.os.Handler.dispatchMessage(Handler.java:102) 
01-23 13:02:05.453 E/mono    (17779):   at android.os.Looper.loop(Looper.java:135) 
01-23 13:02:05.453 E/mono    (17779):   at android.app.ActivityThread.main(ActivityThread.java:5221) 
01-23 13:02:05.453 E/mono    (17779):   at java.lang.reflect.Method.invoke(Native Method)

3 个答案:

答案 0 :(得分:1)

当我在Xamarin应用程序中包含的.dll中引用绑定库时,出现此错误。使用dexdump.exe,可能会发现该类未在dex文件中定义。

当我也将绑定库引用到主要Xamarin应用程序中时,该类已正确生成,并在classes.dex中具有一个条目。

答案 1 :(得分:0)

这个声音类似于我尝试使用反射动态创建对象时遇到的问题。就我而言,Xamarin Linker在构建时删除了该类。

您可以在此处阅读有关链接器的更多信息: https://developer.xamarin.com/guides/android/advanced_topics/linking/

尝试将其设置为&#34;不要链接&#34;,以查看是否删除了错误。如果是这样,您需要静态引用该类或将其标记为Preserve。

答案 2 :(得分:0)

我在Loading Progress Button库中遇到了类似的问题,并且执行了以下步骤来解决该问题:

  1. 创建一个可以通过xml进行充气的自定义视图
  2. 让该视图从您想膨胀的原始视图开始扩展
  3. 请注意Exception,因为库可能使用了您尚未在项目中引用的包,这就是为什么您仍然得到ClassDefNotFoundException的原因。

对我来说,事实证明该库在通货膨胀期间使用了某些类的kotlin stdLib。因为它发生在通货膨胀期间,所以当我通过代码创建视图时并没有抛出异常。将Xamarin.Kotlin.StdLib程序包添加到我的项目中,然后为我解决了这个问题。

我还了解到包名称确实是区分大小写,因此您应该使用与本机android / java中使用的包名称完全相同的包名称。