【问题标题】:NoClassDefFoundError on implementing Runnable class in android studioNoClassDefFoundError 在 android studio 中实现 Runnable 类
【发布时间】:2015-08-31 04:01:26
【问题描述】:

我的代码中不断出现 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() {行上

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

编辑(添加错误日志):

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)

【问题讨论】:

  • 介意发布错误日志吗?
  • 认为定位前景对象是不可行的。
  • 发布你的 ForegroundHelper 类代码
  • 我使用它,但我将它的精确副本创建到一个名为 ForegroundHelper 的新类中(将 Foreground 引用更改为 ForegroundHelper):gist.github.com/steveliles/11116937

标签: java android android-studio noclassdeffounderror runnable


【解决方案1】:

啊,我明白了。我在我的应用程序中使用了 multidex,但我忘记在 AndroidManifest.xml 中添加 android:name="android.support.multidex.MultiDexApplication"。添加它会解决问题,尽管我选择修剪我的代码以便我不必使用 multidex

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-04
    • 1970-01-01
    • 2017-07-28
    • 1970-01-01
    相关资源
    最近更新 更多