java.lang.NoClassDefFoundError:android.os.AsyncTask

维沙克·阿玛纳特(Vyshakh Amarnath)

Android 2.2设备上出现奇怪的错误。以下内容适用于所有设备,直到最近在GT-I5510中我们才遇到此错误。我们的应用程序支持最低sdk级别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;
        }

    }
巴勃罗·马丁内斯(Pablo Martinez)

在onCreate()的Application类中编写此代码

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

java.lang.NoClassDefFoundError:groovy / lang / GroovyObject

android studio java.lang.NoClassDefFoundError Gson

java.lang.NoClassDefFoundError

AsyncTask中的android.os.NetworkOnMainThreadException

java.lang.NoClassDefFoundError:无法解决以下问题:Landroid / support / v4 / os / BuildCompat

AsyncTask的doInBackground中的android.os.NetworkOnMainThreadException

java.lang.ClassNotFoundException:android.os.AsyncTask是由AdMob / Google Play服务引起的吗?

Android:NoClassDefFoundError android.os.AsyncTask

带有KitKat的Android设备上的java.lang.NoClassDefFoundError

Android:java.lang.NoClassDefFoundError

Android 9 asynctask中的java.lang.NoClassDefFoundError

Android 9(Pie)中的java.lang.NoClassDefFoundError

Android-使用AppCompatActivity时java.lang.NoClassDefFoundError

无法在ListView Java中显示来自json的数据,并且不推荐使用android.os.asynctask

java.lang.NoClassDefFoundError:com.google.android.maps.GeoPoint

“ java.lang.NoClassDefFoundError”

java.lang.NoClassDefFoundError Android Studio

java.lang.NoClassDefFoundError:android + maven + eclipse

Android Showcaseview库java.lang.NoClassDefFoundError

Android 2.2:Guava类的java.lang.NoClassDefFoundError

Java SE Android AsyncTask

Android中的Java.lang.NoClassDefFoundError

Android Studio上的java.lang.NoClassDefFoundError

“ java.lang.NoClassDefFoundError:结果”

Android:发布APK上的java.lang.NoClassDefFoundError

我项目中的类的Android java.lang.NoClassDefFoundError

在android.os.AsyncTask $ 3.done(AsyncTask.java:300)

升级Android studio 3.0后点击java.lang.NoClassDefFoundError

MultiDex 导致 Android 4.4 中的 java.lang.NoClassDefFoundError