Search code examples
javaandroidandroid-studionoclassdeffounderrorrunnable

NoClassDefFoundError on implementing Runnable class in android studio


I keep getting NoClassDefFoundError on my code. Here is the code:

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

The error occurs on assigning new Runnable() to check

I tried separating the Runnable, and tried this with no success

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

The error now appears on line final Runnable temp = new Runnable() {

What happened? I check the compiled classes and the files are there. The class and the $ class. Is there a solution or an alternative?

EDIT (add error log):

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)

Solution

  • Ah, I got it. I use multidex in my application and I forgot to add android:name="android.support.multidex.MultiDexApplication" in AndroidManifest.xml. Adding it would solve the problem although I chose to trim my codes so that I wouldn't have to use multidex