码迷,mamicode.com
首页 > 移动开发 > 详细

Getting NoClassDefFoundError: android.os.AsyncTask

时间:2015-03-19 20:27:38      阅读:365      评论:0      收藏:0      [点我收藏+]

标签:asynctask

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;
    }
}
shareimprove this question
 
 
hmm... how come the NoClassFoundDefError is for AsyncTask and not StatisticsProvider ? –  David T. Aug 31 ‘13 at 0:47

3 Answers

OK, looks like it is a problem with one of the versions of Google play Services. Seehttps://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">

Getting NoClassDefFoundError: android.os.AsyncTask

标签:asynctask

原文地址:http://blog.csdn.net/u010739551/article/details/44462015

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!