最近,当MainActivity调用onResume()时,我有时会遇到这个异常。
java.lang.RuntimeException: Unable to resume activity {com.qau4d.c35s3.androidapp/com.xxx.XXX.XXX.MainActivity}: java.lang.IllegalArgumentException
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3400)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3440)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1510)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.IllegalArgumentException
at android.os.Parcel.readException(Parcel.java:1687)
at android.os.Parcel.readException(Parcel.java:1636)
at android.app.ActivityManagerProxy.isTopOfTask(ActivityManagerNative.java:5475)
at android.app.Activity.isTopOfTask(Activity.java:5961)
at android.app.Activity.onResume(Activity.java:1252)
at com.qau4d.c35s3.androidapp.onResume(XActivity.java:29)
at com.qau4d.c35s3.androidapp.onResume(MainActivity.java:196)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1269)
at android.app.Activity.performResume(Activity.java:6768)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3377)
在MainActivity
和超类XActivity
中,只有super.onResume();
。在很长一段时间的正常开发后得到这个异常真的很奇怪。我查阅了一些相关的参考资料,但什么也没有得到。
发布于 2018-01-16 15:24:42
在方法Activity#isTopOfTask中,我们可以看到:
private boolean isTopOfTask() {
if (mToken == null || mWindow == null) {
return false;
}
try {
return ActivityManager.getService().isTopOfTask(getActivityToken());
} catch (RemoteException e) {
return false;
}
}
在ActivityManagerService#isTopOfTask中,我们可以找到:
@Override
public boolean isTopOfTask(IBinder token) {
synchronized (this) {
ActivityRecord r = ActivityRecord.isInStackLocked(token);
if (r == null) {
throw new IllegalArgumentException();
}
return r.task.getTopActivity() == r;
}
}
所以,我认为ActivityRecord是空的,我不知道为什么它是空的……
发布于 2017-12-12 12:45:31
你的问题中没有足够的信息来确定java.lang.IllegalArgumentException
的原因,不幸的是android ActivityThread
没有记录该异常的堆栈跟踪,并且异常消息看起来是空的。
然而,看起来有一条前进的道路。该异常由ActivityThread::performResumeActivity
方法中的以下代码处理:
} catch (Exception e) {
if (!mInstrumentation.onException(r.activity, e)) {
throw new RuntimeException(
"Unable to resume activity "
+ r.intent.getComponent().toShortString()
+ ": " + e.toString(), e);
}
}
如果您为您的活动注册了一个Instrumentation
类,那么应该可以使用onException
方法来记录因果异常的堆栈跟踪。另一种可能性是使用Thread.setUncaughtExceptionHandler
为抛出IllegalArgumentException
的线程设置一个处理程序。
这些都不能解决问题(!)但它会让你离解决方案更近一步。
https://stackoverflow.com/questions/42758643
复制相似问题