java.lang.NoClassDefFoundError的:android.os.AsyncTask [英] java.lang.NoClassDefFoundError: android.os.AsyncTask

查看:479
本文介绍了java.lang.NoClassDefFoundError的:android.os.AsyncTask的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android 2.2的设备上奇怪的错误。在所有的设备以下的作品,我们从来没有遇到过这种错误,直到最近在GT-I5510。我们的应用程序支持SDK最小级别8从设置.Clearing应用数据和启动应用程序解决了该问题,但我不明白为什么它不能找到class..Android支持库被添加。

  java.lang.NoClassDefFoundError的:android.os.AsyncTask
        在com.example.android.library.stTest.stController.runTests(stController.java:228)
        在com.example.android.myapp.Fragments.Connection.ConnectionFragment $ 1.run(ConnectionFragment.java:69)
        在android.os.Handler.handleCallback(Handler.java:587)
        在android.os.Handler.dispatchMessage(Handler.java:92)
        在android.os.Looper.loop(Looper.java:123)
        在android.app.ActivityThread.main(ActivityThread.java:4628)
        在java.lang.reflect.Method.invokeNative(本机方法)
        在java.lang.reflect.Method.invoke(Method.java:521)
        在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:858)
        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
        在dalvik.system.NativeStart。
        //stController.java线是这个
            PTEST =新的测试(背景下,这一点);
//做一个异步任务
            pTest.execute(pTestData);    公共类测试扩展AbstractTest< TESTDATA,太虚,TESTDATA> {        私有静态最后弦乐LOG_TAG = Debug.LOG_TAG _ +测试;        私人TestListener回调; //调用实现回调监听对象        / **
         *构造
         * @参数方面
         * @参数回调 - 实现TestListener对结果的对象。
         * /
        公开测试(最终上下文的背景下,最终TestListener回调){
            超级(上下文);
    // Debug.v(LOG_TAG,所谓的构造函数。);
            this.callback =回调;
        }
        @覆盖
        保护TESTDATA doInBackground(最终TESTDATA参数... args){
            TESTDATA TESTDATA = ARGS [0];            如果(!isTestPossible()){                TestData.setTestState(TestState.FAILED_TO_RUN);
            }其他{
                TestData.setLocalIpAddress(Utils.getLocalIpAddress());                尝试{
                    TestData.setTestState(TestState.RUNNING);
                    的runTest(TESTDATA);
                    TestData.setTestState(TestState.FINISHED);
                }赶上(例外五){
                    Debug.e(LOG_TAGdoInBackground()的runTest()异常:+ E);
                    TestData.setTestState(TestState.FAILED_TO_RUN);
                }
            }            返回TESTDATA;
        }
        @覆盖
        保护无效onPostExecute(TESTDATA结果){
            如果(回调== NULL){
                Debug.e(LOG_TAGonPostExecute():侦听器回调为空!);
            }其他{
                Debug.v(LOG_TAGonPostExecute()调用结果:+ results.toString());
                callback.onTestComplete(结果);
                回调= NULL; //释放参考监听器
            }
        }        @覆盖
        保护无效onCancelled(TESTDATA结果){
            如果(回调== NULL){
                Debug.e(LOG_TAG,onCancelled(结果):侦听器回调为空!);
            }其他{
                callback.onTestCancelled(结果);
                回调= NULL; //释放参考监听器
            }
        }        //还需要对旧操作系统此版本。
        @覆盖
        保护无效onCancelled(){
            如果(回调== NULL){
                Debug.e(LOG_TAGonCancelled():侦听器回调为空!);
            }其他{
                Debug.v(LOG_TAGonCancelled()调用。);
                callback.onTestCancelled(NULL);
                回调= NULL; //释放参考监听器
            }
        }
        // C ++函数。        //这个函数运行实际的测试。
        私人本地无效的runTest(TESTDATA TESTDATA);    }
    //抽象测试
    公共类AbstractTest&下; T1,T2,T3>延伸的AsyncTask&下; T1,T2,T3> {        保护TelephonyManager teleMan;
        保护ConnectivityManager康曼;
        民营背景的TestContext;        公共AbstractTest(最终上下文的背景下){
            teleMan =(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
            康曼=(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
            的TestContext =背景;
        }
        保护布尔isTestPossible(){
            如果(!Utils.isNetworkAvailable(的TestContext))
            {
                返回false;
            }
            串localIpAddress = Utils.getLocalIpAddress();
            如果(localIpAddress == NULL || localIpAddress ==){
                Debug.w(LOG_TAGisTestPossible():无本地IP地址!);
                返回false;
            }
            返回true;
        }        @覆盖
        保护T3 doInBackground(T1 ... PARAMS){
            返回null;
        }    }


解决方案

在你的onCreate应用程序类写这个()

  {尝试
  的Class.forName(android.os.AsyncTask);
}
赶上(Throwable的忽略){
  //忽略
}

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

    }

解决方案

Write this in your Application class in onCreate()

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

这篇关于java.lang.NoClassDefFoundError的:android.os.AsyncTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆