在android studio中实现Runnable类的NoClassDefFoundError

时间:2015-08-31 04:01:26

标签: java android android-studio noclassdeffounderror runnable

我一直在我的代码上得到NoClassDefFoundError。这是代码:

handler.postDelayed(check = new Runnable() {
        @Override
        public void run() {
            if (foreground && paused) {
                foreground = false;
                Log.i(TAG, "went background");
                for (Listener l : listeners) {
                    try {
                        l.onBecameBackground();
                    } catch (Exception exc) {
                        Log.e(TAG, "Listener threw exception!", exc);
                    }
                }
            } else {
                Log.i(TAG, "still foreground");
            }
        }
    }
    , CHECK_DELAY);

new Runnable()分配给check

时发生错误

我尝试将Runnable分开,并试了一下

final Runnable temp = new Runnable() {
        @Override
        public void run() {
            if (foreground && paused) {
                foreground = false;
                Log.i(TAG, "went background");
                for (Listener l : listeners) {
                    try {
                        l.onBecameBackground();
                    } catch (Exception exc) {
                        Log.e(TAG, "Listener threw exception!", exc);
                    }
                }
            } else {
                Log.i(TAG, "still foreground");
            }
        }
    };

    handler.postDelayed(check = new Runnable() {
        @Override
        public void run() {
            if (foreground && paused) {
                foreground = false;
                Log.i(TAG, "went background");
                for (Listener l : listeners) {
                    try {
                        l.onBecameBackground();
                    } catch (Exception exc) {
                        Log.e(TAG, "Listener threw exception!", exc);
                    }
                }
            } else {
                Log.i(TAG, "still foreground");
            }
        }
    }
    , CHECK_DELAY);

错误现在显示在第final Runnable temp = new Runnable() {

发生什么事了?我检查编译的类,文件在那里。类和$ class。有解决方案或替代方案吗?

编辑(添加错误日志):

08-31 10:54:51.672  17887-17887/com.travelio.traveliochatapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.travelio.traveliochatapp, PID: 17887
java.lang.NoClassDefFoundError: com.travelio.traveliochatapp.misc.ForegroundHelper$1
        at com.travelio.traveliochatapp.misc.ForegroundHelper.onActivityPaused(ForegroundHelper.java:159)
        at android.app.Application.dispatchActivityPaused(Application.java:217)
        at android.app.Activity.onPause(Activity.java:1287)
        at com.travelio.traveliochatapp.SplashActivity.onPause(SplashActivity.java:79)
        at android.app.Activity.performPause(Activity.java:5335)
        at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1233)
        at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3138)
        at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3107)
        at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3085)
        at android.app.ActivityThread.access$1000(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1257)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:212)
        at android.app.ActivityThread.main(ActivityThread.java:5137)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:718)
        at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

啊,我明白了。我在我的应用程序中使用了multidex,但忘记在<backup-count>6</backup-count>中添加android:name="android.support.multidex.MultiDexApplication"。添加它可以解决问题,虽然我选择修剪我的代码,以便我不必使用multidex