对源文件的Android的ClassNotFoundException在不同的包 [英] Android ClassNotFoundException on source file in a different package
问题描述
我已经得到了一个的NoClassDefFoundError
引起的的ClassNotFoundException
试图访问一个单独的类包。
I've managed to get a NoClassDefFoundError
caused by a ClassNotFoundException
by trying to access a class in a separate package.
我已经写了code重现由$ P $这个问题pssing一个按钮调用下面的堆栈跟踪的功能。
I have written the code to reproduce this problem by pressing a button that calls the function below the stack traces.
什么是没有任何意义的是,即使我在该项目中的源文件,它没有找到类。
What doesn't make any sense is that even though I have the source file in the project, it doesn't find the class.
堆栈跟踪:
07-13 15:58:40.317: E/X(2517): x- java.lang.NoClassDefFoundError: Failed resolution of: Lobd/ObdGatewayService;
07-13 15:58:40.317: E/XX(2517): xx-
07-13 15:58:40.317: E/XX(2517): java.lang.NoClassDefFoundError: Failed resolution of: Lobd/ObdGatewayService;
07-13 15:58:40.317: E/XX(2517): at piccolo.android.PANDA18.OBDTaximeter.doBindService(OBDTaximeter.java:856)
07-13 15:58:40.317: E/XX(2517): at piccolo.android.PANDA18.OBDTaximeter.startLiveData(OBDTaximeter.java:823)
07-13 15:58:40.317: E/XX(2517): at piccolo.android.PANDA18.OBDTaximeter.access$8(OBDTaximeter.java:818)
07-13 15:58:40.317: E/XX(2517): at piccolo.android.PANDA18.OBDTaximeter$3.onClick(OBDTaximeter.java:313)
07-13 15:58:40.317: E/XX(2517): at android.view.View.performClick(View.java:5217)
07-13 15:58:40.317: E/XX(2517): at android.view.View$PerformClick.run(View.java:20983)
07-13 15:58:40.317: E/XX(2517): at android.os.Handler.handleCallback(Handler.java:739)
07-13 15:58:40.317: E/XX(2517): at android.os.Handler.dispatchMessage(Handler.java:95)
07-13 15:58:40.317: E/XX(2517): at android.os.Looper.loop(Looper.java:145)
07-13 15:58:40.317: E/XX(2517): at android.app.ActivityThread.main(ActivityThread.java:6117)
07-13 15:58:40.317: E/XX(2517): at java.lang.reflect.Method.invoke(Native Method)
07-13 15:58:40.317: E/XX(2517): at java.lang.reflect.Method.invoke(Method.java:372)
07-13 15:58:40.317: E/XX(2517): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
07-13 15:58:40.317: E/XX(2517): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
07-13 15:58:40.317: E/XX(2517): Caused by: java.lang.ClassNotFoundException: Didn't find class "obd.ObdGatewayService" on path: DexPathList[[zip file "/system/framework/com.google.android.maps.jar", zip file "/data/app/piccolo.android.PANDA18-2/base.apk"],nativeLibraryDirectories=[/data/app/piccolo.android.PANDA18-2/lib/arm, /vendor/lib, /system/lib]]
07-13 15:58:40.317: E/XX(2517): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
07-13 15:58:40.317: E/XX(2517): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
07-13 15:58:40.317: E/XX(2517): at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
07-13 15:58:40.317: E/XX(2517): ... 14 more
07-13 15:58:40.317: E/XX(2517): Suppressed: java.lang.NoClassDefFoundError: obd.ObdGatewayService
07-13 15:58:40.317: E/XX(2517): at dalvik.system.DexFile.defineClassNative(Native Method)
07-13 15:58:40.317: E/XX(2517): at dalvik.system.DexFile.defineClass(DexFile.java:226)
07-13 15:58:40.317: E/XX(2517): at dalvik.system.DexFile.loadClassBinaryName(DexFile.java:219)
07-13 15:58:40.317: E/XX(2517): at dalvik.system.DexPathList.findClass(DexPathList.java:321)
07-13 15:58:40.317: E/XX(2517): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:54)
07-13 15:58:40.317: E/XX(2517): ... 16 more
07-13 15:58:40.317: E/XX(2517): Suppressed: java.lang.ClassNotFoundException: obd.ObdGatewayService
07-13 15:58:40.317: E/XX(2517): at java.lang.Class.classForName(Native Method)
07-13 15:58:40.317: E/XX(2517): at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
07-13 15:58:40.317: E/XX(2517): at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
07-13 15:58:40.317: E/XX(2517): at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
07-13 15:58:40.317: E/XX(2517): ... 15 more
07-13 15:58:40.317: E/XX(2517): Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
请注意,上面的堆栈跟踪只对我第一次preSS按钮产生的。随后的按钮presses导致更短的,略有不同的版本(见下文)。
Note that the above stacktrace is only produced for the first time I press the button. Subsequent button presses result in a shorter, slightly different version of it (See below).
07-13 15:31:59.767: E/X(32078): x- java.lang.NoClassDefFoundError: obd.ObdGatewayService
07-13 15:31:59.767: E/XX(32078): xx-
07-13 15:31:59.767: E/XX(32078): java.lang.NoClassDefFoundError: obd.ObdGatewayService
07-13 15:31:59.767: E/XX(32078): at piccolo.android.PANDA18.OBDTaximeter.doBindService(OBDTaximeter.java:856)
07-13 15:31:59.767: E/XX(32078): at piccolo.android.PANDA18.OBDTaximeter.startLiveData(OBDTaximeter.java:823)
07-13 15:31:59.767: E/XX(32078): at piccolo.android.PANDA18.OBDTaximeter.access$8(OBDTaximeter.java:818)
07-13 15:31:59.767: E/XX(32078): at piccolo.android.PANDA18.OBDTaximeter$3.onClick(OBDTaximeter.java:313)
07-13 15:31:59.767: E/XX(32078): at android.view.View.performClick(View.java:5217)
07-13 15:31:59.767: E/XX(32078): at android.view.View$PerformClick.run(View.java:20983)
07-13 15:31:59.767: E/XX(32078): at android.os.Handler.handleCallback(Handler.java:739)
07-13 15:31:59.767: E/XX(32078): at android.os.Handler.dispatchMessage(Handler.java:95)
07-13 15:31:59.767: E/XX(32078): at android.os.Looper.loop(Looper.java:145)
07-13 15:31:59.767: E/XX(32078): at android.app.ActivityThread.main(ActivityThread.java:6117)
07-13 15:31:59.767: E/XX(32078): at java.lang.reflect.Method.invoke(Native Method)
07-13 15:31:59.767: E/XX(32078): at java.lang.reflect.Method.invoke(Method.java:372)
07-13 15:31:59.767: E/XX(32078): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
07-13 15:31:59.767: E/XX(32078): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
code这是造成错误的部分:
Section of code that's causing the error:
// this is from the OBDTaximeter class, and is called whenever I press a specific button. I have a try/catch where I'm calling it to log the error above
private void doBindService() {
if (!isServiceBound) {
Log.d(TAG, "Binding OBD service..");
if (preRequisites) {
Intent serviceIntent = new Intent(this, ObdGatewayService.class); // Specifically, this line
bindService(serviceIntent, serviceConn, Context.BIND_AUTO_CREATE);
} else {
Intent serviceIntent = new Intent(this, MockObdGatewayService.class);
bindService(serviceIntent, serviceConn, Context.BIND_AUTO_CREATE);
}
}
}
解决方案资源管理器的截图:
Screenshot of solution explorer:
黄色突出显示的部分表示该文件确实是在同一个项目,但在不同的包,红色的线重新presents其中code,它正试图使用缺少的类名为
The yellow highlighted portion shows that the file is indeed in the same project, but in a different package, where the red line represents where the code that's trying to use the missing class is called.
我认为它可能是与我的舱单申报有问题,但我找不到什么毛病 - 我可能是错的,所以我包括含有下面的相关位的精简版
I've considered that it may be a problem with my manifest declaration, but I can't find anything wrong with it - I may be wrong, so I'm including a condensed version containing the relevant bits below.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="piccolo.android.PANDA18"
android:versionCode="1"
android:versionName="1.0">
<activity android:name="OBDTaximeter" />
<activity android:name="obd.ConfigActivity" />
<service android:enabled="true" android:name="obd.MockObdGatewayService" />
<service android:enabled="true" android:name="obd.ObdGatewayService" />
</application>
</manifest>
我也试过刚刚清理和重建(太多次来算)。我确信,构建路径是正确的(在应用程序兼容性库,出口秩序和诸如此类的方面),但为了以防万一,我包括从下面Eclipse中的排序和导出屏幕截图。
I've also tried just cleaning and rebuilding (too many times to count). I made sure that the build path is correct (in terms of appcompat libraries, export order and whatnot), but just in case, I'm including a screenshot of the "Order and Export" screen from Eclipse below.
我已经检查了classes.dex文件,我确实觉得它里面 ObdGatewayService
类上市。
I've checked the classes.dex file, and I do find ObdGatewayService
class listings inside it.
在上市类classes.dex的:
Listing of class in classes.dex:
Class #239 -
Class descriptor : 'Lobd/ObdGatewayService$1;'
Access flags : 0x0000 ()
Superclass : 'Ljava/lang/Object;'
Interfaces -
#0 : 'Ljava/lang/Runnable;'
Static fields -
Instance fields -
#0 : (in Lobd/ObdGatewayService$1;)
name : 'this$0'
type : 'Lobd/ObdGatewayService;'
access : 0x1010 (FINAL SYNTHETIC)
#1 : (in Lobd/ObdGatewayService$1;)
name : 'val$job2'
type : 'Lobd/ObdCommandJob;'
access : 0x1012 (PRIVATE FINAL SYNTHETIC)
Direct methods -
#0 : (in Lobd/ObdGatewayService$1;)
name : '<init>'
type : '(Lobd/ObdGatewayService;Lobd/ObdCommandJob;)V'
access : 0x10000 (CONSTRUCTOR)
code -
registers : 3
ins : 3
outs : 1
insns size : 8 16-bit code units
catches : (none)
positions :
0x0000 line=1
0x0004 line=240
locals :
0x0000 - 0x0008 reg=0 this Lobd/ObdGatewayService$1;
Virtual methods -
#0 : (in Lobd/ObdGatewayService$1;)
name : 'run'
type : '()V'
access : 0x0001 (PUBLIC)
code -
registers : 3
ins : 1
outs : 2
insns size : 12 16-bit code units
catches : (none)
positions :
0x0000 line=243
0x000b line=244
locals :
0x0000 - 0x000c reg=2 this Lobd/ObdGatewayService$1;
source_file_idx : 4574 (ObdGatewayService.java)
为什么会出现这种情况?
Why is this happening?
推荐答案
开始的意图是这样的:
Intent intent = new Intent();
intent.setClassName("obd", "obd.ObdGatewayService");
startService(intent);
这篇关于对源文件的Android的ClassNotFoundException在不同的包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!