获取 NoClassDefFoundError:android.os.AsyncTask

发布于 2024-11-28 09:40:03 字数 1880 浏览 0 评论 0原文

这个问题困扰了我一段时间,一直没有找到满意的解决方案。

很多时候(并非总是)在 Android 模拟器上创建特定对象时,我得到 NoClassDefFoundError: android.os.AsynchTask。

我已经尝试了很多方法,多次删除和重新添加外部库,擦除模拟器,重新启动 IntelliJ(工作过一次,但随后得到了相同的错误。)此时我要做的就是构造最准系统的 AsyncTask,我仍然得到错误。

这是整个错误消息:

08-06 16:24:43.546: ERROR/AndroidRuntime(331): FATAL EXCEPTION: main
java.lang.NoClassDefFoundError: android.os.AsyncTask
at com.myapp.activity.StatisticsActivity.onCreate(StatisticsActivity.java:79)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)

模拟器运行 2.2,我的 minSDK 是 8。这是有问题的代码:

public class StatisticsActivity extends TabActivity implements AsynchDataDisplay{
...
    public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.statistics);

    StatisticsProvider sp = new StatisticsProvider();
...
    }
}

StatisticsProvider 对象是:

    private class StatisticsProvider extends AsyncTask{

    @Override
    protected Object doInBackground(Object... objects) {
        return null;
    }
}

This problem has been bothering me for a bit and I cannot find a satisfactory solution.

Many times (not always) in the creation of a specific object on the Android emulator I get NoClassDefFoundError: android.os.AsynchTask.

I have tried many approaches removing and re-adding external libraries multiple times, wiping the emulator, restarting IntelliJ (worked once but then got the same error.) At this point all I am trying to do is construct the most barebones AsyncTask and I still get the error.

Here is the whole error message:

08-06 16:24:43.546: ERROR/AndroidRuntime(331): FATAL EXCEPTION: main
java.lang.NoClassDefFoundError: android.os.AsyncTask
at com.myapp.activity.StatisticsActivity.onCreate(StatisticsActivity.java:79)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)

The emulator runs 2.2, my minSDK is 8. Here is the offending code:

public class StatisticsActivity extends TabActivity implements AsynchDataDisplay{
...
    public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.statistics);

    StatisticsProvider sp = new StatisticsProvider();
...
    }
}

And the StatisticsProvider object is:

    private class StatisticsProvider extends AsyncTask{

    @Override
    protected Object doInBackground(Object... objects) {
        return null;
    }
}

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

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

发布评论

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

评论(3

忘东忘西忘不掉你 2024-12-05 09:40:03

好的,看起来这是 Google Play 服务版本之一的问题。请参阅 https://code.google.com/p/android/issues/ detail?id=81083

看起来解决方法可能是添加:

try {
      Class.forName("android.os.AsyncTask");
}
catch(Throwable ignore) {
      // ignored
}

到您的 Application#onCreate()

如果您还没有 Application 类,则创建一个扩展自的类android.app.Application 并将

  1. 上面的代码添加到其 #onCreate()
  2. 中 在 AndroidManifest 的 application 元素中添加对应用程序类的引用。
<application android:icon="@drawable/icon_game_launcher"
             android:label="@string/app_name"
             android:name="au.com.mycompany.myapp.MyApplication">

OK, looks like it is a problem with one of the versions of Google play Services. See https://code.google.com/p/android/issues/detail?id=81083

Looks like a work around might be to add:

try {
      Class.forName("android.os.AsyncTask");
}
catch(Throwable ignore) {
      // ignored
}

into your Application#onCreate()

If you don't already have a Application class then create a class that extends from android.app.Application and

  1. Add the code above into its #onCreate()
  2. Add a reference to your application class in the application element of your AndroidManifest.
<application android:icon="@drawable/icon_game_launcher"
             android:label="@string/app_name"
             android:name="au.com.mycompany.myapp.MyApplication">
清眉祭 2024-12-05 09:40:03

我想我已经破解了它。

用于在我的应用程序中加载广告的线程会在早期抛出一个未捕获的异常,以某种我不能 100% 理解的方式,导致工作线程停止响应。当程序尝试创建 AsyncTask 时,类加载器无法工作。

寓意是,去度假,回来时会焕然一新。

I think I have cracked it.

The thread used to load ads in my app would throw an uncaught exception early on which, in a way I don't 100% understand, caused the worker thread to stop responding. When the program tried to create the AsyncTask the class loader was not working.

Moral, take a vacation and come back with fresh eyes.

水溶 2024-12-05 09:40:03

尝试在StatisticsProvider中添加一个简单的构造函数并检查。

Try adding a simple constructor in StatisticsProvider and check.

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