java.lang.NoClassDefFoundError:android.os.AsyncTask

Android 2.2设备上的奇怪错误.以下适用于所有设备,直到最近才在GT-I5510中遇到此错误.我们的应用程序支持min sdk-level 8.从设置中清除应用程序数据并启动应用程序修复了问题但是我不明白为什么它不能找到类..添加了Android支持库.

    java.lang.NoClassDefFoundError: android.os.AsyncTask
        at com.example.android.library.stTest.stController.runTests(stController.java:228)
        at com.example.android.myapp.Fragments.Connection.ConnectionFragment$1.run(ConnectionFragment.java:69)
        at android.os.Handler.handleCallback(Handler.java:587)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:123)
        at android.app.ActivityThread.main(ActivityThread.java:4628)
        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:858)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
        at dalvik.system.NativeStart.


        //stController.java line is this
            pTest = new Test(context, this);
//does an async task
            pTest.execute(pTestData);







    public class Test extends AbstractTest<TestData, Void, TestData> {

        private static final String LOG_TAG = Debug.LOG_TAG_+"Test";

        private TestListener callback;  // Invoking object that implements the callback listeners

        /**
         * Constructor
         * @param context
         * @param callback  - the object that implements TestListener for the results.
         */
        public Test(final Context context, final TestListener callback) {
            super(context);
    //      Debug.v(LOG_TAG, "Constructor called.");
            this.callback = callback;
        }   


        @Override
        protected TestData doInBackground(final TestData... args) {
            TestData TestData = args[0];    

            if (!isTestPossible()) {

                TestData.setTestState(TestState.FAILED_TO_RUN);
            } else {
                TestData.setLocalIpAddress(Utils.getLocalIpAddress());

                try {
                    TestData.setTestState(TestState.RUNNING);
                    runTest(TestData);
                    TestData.setTestState(TestState.FINISHED);
                } catch (Exception e) {
                    Debug.e(LOG_TAG, "doInBackground(): runTest() exception: " + e);
                    TestData.setTestState(TestState.FAILED_TO_RUN);
                }
            }

            return TestData;
        }


        @Override
        protected void onPostExecute(TestData results) {
            if (callback == null) {
                Debug.e(LOG_TAG, "onPostExecute(): listener callback is null!!");
            } else {
                Debug.v(LOG_TAG, "onPostExecute() called. Results: " + results.toString());
                callback.onTestComplete(results);
                callback = null;            // Release reference to listener
            }
        }   

        @Override
        protected void onCancelled(TestData results) {
            if (callback == null) {
                Debug.e(LOG_TAG, "onCancelled(results): listener callback is null!!");
            } else {
                callback.onTestCancelled(results);
                callback = null;            // Release reference to listener
            }
        }

        // This version is also needed for older OS.
        @Override
        protected void onCancelled() {
            if (callback == null) {
                Debug.e(LOG_TAG, "onCancelled(): listener callback is null!!");
            } else {
                Debug.v(LOG_TAG, "onCancelled() called.");
                callback.onTestCancelled(null);
                callback = null;            // Release reference to listener
            }
        }


        // C++ functions.

        // This function runs the actual test.
        private native void runTest(TestData TestData);

    } 


    //Abstract Test
    public class AbstractTest <T1, T2, T3> extends AsyncTask<T1, T2, T3> {



        protected TelephonyManager    teleMan;
        protected ConnectivityManager connMan;
        private Context testContext;

        public AbstractTest(final Context context) {
            teleMan = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
            connMan = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
            testContext=context;
        }


        protected boolean isTestPossible() {
            if (!Utils.isNetworkAvailable(testContext))
            {
                return false;
            }
            String localIpAddress = Utils.getLocalIpAddress();
            if (localIpAddress == null || localIpAddress == "") {
                Debug.w(LOG_TAG, "isTestPossible(): no local IP address!!");
                return false;       
            }
            return true;
        }

        @Override
        protected T3 doInBackground(T1... params) {
            return null;
        }

    }

最佳答案 在onCreate()的Application类中写下这个

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