Android 测试仪器期间出现 ClassNotFoundException

发布于 2024-12-03 12:09:18 字数 3661 浏览 0 评论 0原文

我见过很多处理此错误的主题,但找不到有效的解决方案。所以我会尝试完整地描述这个问题。

我有一个项目和一个项目测试。我的项目清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.toto.app"
  android:versionCode="3"
  android:versionName="0.3.1">
  ...
    <application android:icon="@drawable/icon" android:label="@string/app_name">
    ...
    </application>
</manifest>

和我的测试项目清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.toto.app.test"
  android:versionCode="1"
  android:versionName="1.0">
  <uses-sdk android:minSdkVersion="4" />

  <instrumentation android:targetPackage="com.toto.app" android:name="android.test.InstrumentationTestRunner" />

  <application android:icon="@drawable/icon" android:label="@string/app_name">
    <uses-library android:name="android.test.runner" />
  </application>
</manifest>

这是我要测试的活动:src/com/toto/app/activity/ListActivity,我有一个启动此活动的 LaunchActivity (在清单中声明为启动活动)。

在测试中,我的ListActivityTest位于同一路径(src/com/toto/app/activity)。我也尝试过 src/com/toto/app/test。来源是:

public class ListActivityTest extends  ActivityInstrumentationTestCase2<LaunchActivity>{

  private Solo solo;

  public ListActivityTest() {
    super(LaunchActivity.class);
  }

  public void setUp() throws Exception {
    solo = new Solo(getInstrumentation(), getActivity());
    solo.wait(2000);
  } 
}

似乎找到了 LaunchActivity,但是当它启动 ListActivity 时,我收到错误:

% adb shell am Instrument -w com.toto.app.test/android.test.InstrumentationTestRunner

com.toto.app.activity.ListActivityTest:INSTRUMENTATION_RESULT:shortMsg = java.lang.ClassNotFoundException INSTRUMENTATION_RESULT:longMsg = java.lang.ClassNotFoundException:加载器dalvik.system.PathClassLoader中的com.toto.app.activity.ListActivity [/system/framework/android.test.runner.jar:/data/app/com.toto.app .test-2.apk:/data/app/com.toto.app-2.apk]

我不明白为什么找到一个类而另一个没有找到......在 Eclipse 中也以同样的方式失败。我尝试在测试用例中直接使用 ListActivity 但出现同样的错误。它如何构建这个 apk 以避免某些类?


这是使用 adb 时的完整堆栈。

eclipse logcat 中的完整堆栈:

java.lang.NoClassDefFoundError: com.toto.app.activity.ListActivity
 at com.toto.app.activity.LaunchActivity.onCreate(LaunchActivity.java:19)
 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1794)
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1851)
 at android.app.ActivityThread.access$1500(ActivityThread.java:132)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038)
 at android.os.Handler.dispatchMessage(Handler.java:99)
 at android.os.Looper.loop(Looper.java:143)
 at android.app.ActivityThread.main(ActivityThread.java:4293)
 at java.lang.reflect.Method.invokeNative(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:507)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
 at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: com.toto.app.activity.ListActivity in loader dalvik.system.PathClassLoader[/system/framework/android.test.runner.jar:/data/app/com.toto.app.test-2.apk:/data/app/com.toto.app-1.apk]
 at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
 ... 14 more

I've seen many topics dealing with this error but could not found a working solution. So I'll try to describe the problem completely.

I've got a project and a project test. My project manifest :

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.toto.app"
  android:versionCode="3"
  android:versionName="0.3.1">
  ...
    <application android:icon="@drawable/icon" android:label="@string/app_name">
    ...
    </application>
</manifest>

And my test project manifest :

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.toto.app.test"
  android:versionCode="1"
  android:versionName="1.0">
  <uses-sdk android:minSdkVersion="4" />

  <instrumentation android:targetPackage="com.toto.app" android:name="android.test.InstrumentationTestRunner" />

  <application android:icon="@drawable/icon" android:label="@string/app_name">
    <uses-library android:name="android.test.runner" />
  </application>
</manifest>

Here is the activity i want to test : src/com/toto/app/activity/ListActivity, I've got a LaunchActivity (declared as start activity in manifest) that launch this activity.

And in the test, my ListActivityTest is in the same path (src/com/toto/app/activity). I've also tried with src/com/toto/app/test. The source is :

public class ListActivityTest extends  ActivityInstrumentationTestCase2<LaunchActivity>{

  private Solo solo;

  public ListActivityTest() {
    super(LaunchActivity.class);
  }

  public void setUp() throws Exception {
    solo = new Solo(getInstrumentation(), getActivity());
    solo.wait(2000);
  } 
}

The LaunchActivity seems to be found but when it launch the ListActivity, i've got the error :

% adb shell am instrument -w com.toto.app.test/android.test.InstrumentationTestRunner

com.toto.app.activity.ListActivityTest:INSTRUMENTATION_RESULT: shortMsg=java.lang.ClassNotFoundException
INSTRUMENTATION_RESULT: longMsg=java.lang.ClassNotFoundException: com.toto.app.activity.ListActivity in loader dalvik.system.PathClassLoader[/system/framework/android.test.runner.jar:/data/app/com.toto.app.test-2.apk:/data/app/com.toto.app-2.apk]

I don't understand why one class is found and the other is not... It fails in the same way in Eclipse. I tried to use directly ListActivity in the testcase but same error. How does it build this apk to avoid some class ?


This is the full stack when using adb.

The full stack in eclipse logcat :

java.lang.NoClassDefFoundError: com.toto.app.activity.ListActivity
 at com.toto.app.activity.LaunchActivity.onCreate(LaunchActivity.java:19)
 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1794)
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1851)
 at android.app.ActivityThread.access$1500(ActivityThread.java:132)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038)
 at android.os.Handler.dispatchMessage(Handler.java:99)
 at android.os.Looper.loop(Looper.java:143)
 at android.app.ActivityThread.main(ActivityThread.java:4293)
 at java.lang.reflect.Method.invokeNative(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:507)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
 at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: com.toto.app.activity.ListActivity in loader dalvik.system.PathClassLoader[/system/framework/android.test.runner.jar:/data/app/com.toto.app.test-2.apk:/data/app/com.toto.app-1.apk]
 at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
 ... 14 more

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野の 2024-12-10 12:09:18

这是因为我的Activity扩展了一个FragmentActivity,而不是一个简单的Activity。

为了解决这个问题,我必须在主项目中导出 android-support-v4.jar 。

希望能帮助其他人...

export jar

It is because my activity extends a FragmentActivity, not a simple Activity.

To resolve the problem, I had to export android-support-v4.jar in the main project.

Hope that will help others...

export jar

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文