java.lang.RuntimeException: Unable to resume activity with java.lang.IllegalArgumentException

后端 未结 2 414
半阙折子戏
半阙折子戏 2021-01-30 05:10

Recently I sometimes got this exception when MainActivity called onResume().

java.lang.RuntimeException: Unable to resume activity {com.qau4d.c35s3.androidapp/co         


        
相关标签:
2条回答
  • 2021-01-30 05:25

    In the method Activity#isTopOfTask we can see:

    private boolean isTopOfTask() {
        if (mToken == null || mWindow == null) {
            return false;
        }
        try {
            return ActivityManager.getService().isTopOfTask(getActivityToken());
        } catch (RemoteException e) {
            return false;
        }
    }
    

    And in ActivityManagerService#isTopOfTask we can found:

    @Override
    public boolean isTopOfTask(IBinder token) {
        synchronized (this) {
            ActivityRecord r = ActivityRecord.isInStackLocked(token);
            if (r == null) {
                throw new IllegalArgumentException();
            }
            return r.task.getTopActivity() == r;
        }
    }
    

    So, I think that ActivityRecord is null.But I don't know why it is null....

    0 讨论(0)
  • 2021-01-30 05:28

    There is insufficient information in your question to determine the cause of the java.lang.IllegalArgumentException, Unfortunately the android ActivityThread doesn't log the stacktrace of that exception, and the exception message appears to be empty.

    However, it looks like there is a way forward. The exception is handled by the following code in the ActivityThread::performResumeActivity method:

            } catch (Exception e) {
                if (!mInstrumentation.onException(r.activity, e)) {
                    throw new RuntimeException(
                        "Unable to resume activity "
                        + r.intent.getComponent().toShortString()
                        + ": " + e.toString(), e);
                }
            }
    

    If you register an Instrumentation class for your activity, it should be possible to use an onException method to log the stacktrace for the causal exception. Another possibility is to use Thread.setUncaughtExceptionHandler to set a handler for the thread in which the IllegalArgumentException is thrown.

    These won't solve the problem (!) but it will get you a step closer to a solution.

    0 讨论(0)
提交回复
热议问题