java.lang.NoClassDefFoundError: android.os.AsyncTask

廉价感情. 提交于 2019-12-06 05:41:35

问题


Weird error on Android 2.2 device . The following works in all devices and we never encountered this error until recently in GT-I5510 .Our app supports min sdk-level 8 .Clearing the app data from settings and starting the app fixed the issue but what i dont understand why its not able to find the class..Android support library is added .

    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;
        }

    }

回答1:


Write this in your Application class in onCreate()

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


来源:https://stackoverflow.com/questions/24776134/java-lang-noclassdeffounderror-android-os-asynctask

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!