在 android studio 中实现 Runnable class 时出现 NoClassDefFoundError
NoClassDefFoundError on implementing Runnable class in android studio
我的代码中不断出现 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() {
行
发生了什么事?我检查了编译的 classes 并且文件在那里。 class 和 $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
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.access00(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)
啊,我知道了。我在我的应用程序中使用了 multidex,但我忘记在 AndroidManifest.xml
中添加 android:name="android.support.multidex.MultiDexApplication"
。添加它会解决问题,尽管我选择 trim 我的代码,这样我就不必使用 multidex
我的代码中不断出现 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() {
发生了什么事?我检查了编译的 classes 并且文件在那里。 class 和 $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
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.access00(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)
啊,我知道了。我在我的应用程序中使用了 multidex,但我忘记在 AndroidManifest.xml
中添加 android:name="android.support.multidex.MultiDexApplication"
。添加它会解决问题,尽管我选择 trim 我的代码,这样我就不必使用 multidex