Android引用项目出现ClassNotFoundException

时间:2024-08-17 19:33:50

Android中在引用其他工程,尤其是github中的相关库时,如果引用关系设置的不对,很容易出现ClassNotFoundException,例如下面的异常信息

07-26 12:47:51.549: E/AndroidRuntime(8107): FATAL EXCEPTION: main
07-26 12:47:51.549: E/AndroidRuntime(8107): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.ex.chips.sample/com.android.ex.chips.sample.MainActivity}: android.view.InflateException: Binary XML file line #23: Error inflating class com.android.ex.chips.RecipientEditTextView
07-26 12:47:51.549: E/AndroidRuntime(8107): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2081)
07-26 12:47:51.549: E/AndroidRuntime(8107): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2106)
07-26 12:47:51.549: E/AndroidRuntime(8107): at android.app.ActivityThread.access$600(ActivityThread.java:137)
07-26 12:47:51.549: E/AndroidRuntime(8107): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202)
07-26 12:47:51.549: E/AndroidRuntime(8107): at android.os.Handler.dispatchMessage(Handler.java:99)
07-26 12:47:51.549: E/AndroidRuntime(8107): at android.os.Looper.loop(Looper.java:137)
07-26 12:47:51.549: E/AndroidRuntime(8107): at android.app.ActivityThread.main(ActivityThread.java:4897)
07-26 12:47:51.549: E/AndroidRuntime(8107): at java.lang.reflect.Method.invokeNative(Native Method)
07-26 12:47:51.549: E/AndroidRuntime(8107): at java.lang.reflect.Method.invoke(Method.java:511)
07-26 12:47:51.549: E/AndroidRuntime(8107): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:808)
07-26 12:47:51.549: E/AndroidRuntime(8107): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:575)
07-26 12:47:51.549: E/AndroidRuntime(8107): at dalvik.system.NativeStart.main(Native Method)
07-26 12:47:51.549: E/AndroidRuntime(8107): Caused by: android.view.InflateException: Binary XML file line #23: Error inflating class com.android.ex.chips.RecipientEditTextView
07-26 12:47:51.549: E/AndroidRuntime(8107): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:768)
07-26 12:47:51.549: E/AndroidRuntime(8107): at android.view.LayoutInflater.rInflate(LayoutInflater.java:816)
07-26 12:47:51.549: E/AndroidRuntime(8107): at android.view.LayoutInflater.inflate(LayoutInflater.java:559)
07-26 12:47:51.549: E/AndroidRuntime(8107): at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
07-26 12:47:51.549: E/AndroidRuntime(8107): at android.view.LayoutInflater.inflate(LayoutInflater.java:417)
07-26 12:47:51.549: E/AndroidRuntime(8107): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:334)
07-26 12:47:51.549: E/AndroidRuntime(8107): at android.app.Activity.setContentView(Activity.java:1942)
07-26 12:47:51.549: E/AndroidRuntime(8107): at com.android.ex.chips.sample.MainActivity.onCreate(MainActivity.java:34)
07-26 12:47:51.549: E/AndroidRuntime(8107): at android.app.Activity.performCreate(Activity.java:5084)
07-26 12:47:51.549: E/AndroidRuntime(8107): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
07-26 12:47:51.549: E/AndroidRuntime(8107): at com.lbe.security.service.core.client.b.x.callActivityOnCreate(Unknown Source)
07-26 12:47:51.549: E/AndroidRuntime(8107): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2045)
07-26 12:47:51.549: E/AndroidRuntime(8107): ... 11 more
07-26 12:47:51.549: E/AndroidRuntime(8107): Caused by: java.lang.ClassNotFoundException: com.android.ex.chips.RecipientEditTextView
07-26 12:47:51.549: E/AndroidRuntime(8107): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
07-26 12:47:51.549: E/AndroidRuntime(8107): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
07-26 12:47:51.549: E/AndroidRuntime(8107): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
07-26 12:47:51.549: E/AndroidRuntime(8107): at android.view.LayoutInflater.createView(LayoutInflater.java:622)
07-26 12:47:51.549: E/AndroidRuntime(8107): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:757)
07-26 12:47:51.549: E/AndroidRuntime(8107): ... 22 more

需要注意的是Android SDK首先要指定,其次,需要在主项目Properties中的Android下引用其他项目,如下图所示

Android引用项目出现ClassNotFoundException

其他的引用方式,如果被引用项目不是纯Java代码(包含Android的drawable、layout等资源)时,程序是运行不起来的,如下的引用方式是错误的

Android引用项目出现ClassNotFoundException

网上的许多解决方法说,采用上图引用资源后,调整Order and Export,其实,如果引用其他资源采用的是Android引用方式,是不会在这个标签下出现被引项目的,如下图所示

Android引用项目出现ClassNotFoundException

最后谈下感受,其实稍微开发过点Android的都应该知道怎么引用Android项目,但从github上下载了整个样例后,觉得都引用好了,也就忘了这一茬,一运行发现不对,就开始病急乱投医,各种导入,各种上网搜,其实冷静下来分析一下,马上就能发现问题。所以,遇到问题后的第一反应不应该是马上想解决方案,而是要学会先分析问题。