无法在Android API> = 24上使用java.lang.IllegalArgumentException恢复活动


72

有一个在我上运行的版本设备的应用程序崩溃7.07.1.18.0.0具有下列堆栈跟踪:

Fatal Exception: java.lang.RuntimeException: Unable to resume activity {xxx/xxx.views.activities.HomeActivity}: java.lang.IllegalArgumentException
   at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3788)
   at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3828)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2991)
   at android.app.ActivityThread.-wrap14(ActivityThread.java)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1635)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:154)
   at android.app.ActivityThread.main(ActivityThread.java:6692)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
Caused by java.lang.IllegalArgumentException
   at android.os.Parcel.readException(Parcel.java:1697)
   at android.os.Parcel.readException(Parcel.java:1646)
   at android.app.ActivityManagerProxy.isTopOfTask(ActivityManagerNative.java:6600)
   at android.app.Activity.isTopOfTask(Activity.java:6142)
   at android.app.Activity.onResume(Activity.java:1331)
   at android.support.v4.app.FragmentActivity.onResume(FragmentActivity.java:485)
   at xxx.views.activities.BaseActivity.onResume(BaseActivity.java:50)
   at xxx.views.activities.HomeActivity.onResume(HomeActivity.java:364)
   at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1277)
   at android.app.Activity.performResume(Activity.java:7058)
   at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3765)
   at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3828)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2991)
   at android.app.ActivityThread.-wrap14(ActivityThread.java)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1635)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:154)
   at android.app.ActivityThread.main(ActivityThread.java:6692)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)

我在报告行中的代码很简单:

// HomeActivity class
@Override
public void onResume() {
    mPresenter.onResume();
    super.onResume();

    renderView();
}

// BaseActivity class
@Override
protected void onResume() {
    super.onResume();
    // some other code
}

我对此进行了一些挖掘,发现了类似的问题

由于IllegalArgumentException抛出的消息不包含任何详细信息,因此我将尝试按照此处的建议放入额外的日志。

同时,请感谢任何帮助!


1
在您的onResume方法调用中,应首先调用super.onResume。
JoxTraex

@JoxTraex onResume是onResume方法中的第一件事。但这并不能解决。我的项目中也面临着同样的问题。
阿妮

您测试过android L 5.1.x吗?
JoxTraex

1
在Android 7和8上也是如此。找到解决方案了吗?
Zhanbolat Raimbekov

1
我有同样的问题,有人有其他解决方案吗?
阿德里安娜·卡雷利

Answers:


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

这是一个连锁的例子,所以检查e.getCause()stacktrace->直到cause为空

如果找不到,请查看活动线程中调用try块的方法:

try {
  r.activity.onStateNotSaved();
  r.activity.mFragments.noteStateNotSaved();

  if (r.pendingIntents != null) {
    deliverNewIntents(r, r.pendingIntents);
    r.pendingIntents = null;
  }

  if (r.pendingResults != null) {
    deliverResults(r, r.pendingResults);
    r.pendingResults = null;
  }

  r.activity.performResume();

  // If there is a pending local relaunch that was requested 
  // when the activity was
  // paused, it will put the activity into paused state
  // when it finally happens.
  // Since the activity resumed before being relaunched, 
  // we don't want that to happen,
  // so we need to clear the request to relaunch paused.

  for (int i = mRelaunchingActivities.size() - 1; i >= 0; i--) {
    final ActivityClientRecord relaunching =
    mRelaunchingActivities.get(i);

    if (relaunching.token == r.token
        && relaunching.onlyLocalRequest &&
        relaunching.startsNotResumed) {
            relaunching.startsNotResumed = false;
    }
 }
}

您需要在以下位置搜索原因:

  • Activity.onStateNotSaved();
  • Activity.mFragments.noteStateNotSaved();
  • Activity.performResume();
  • 和最后的Activity.onNewIntent()
    Caused by java.lang.IllegalArgumentException
        at android.os.Parcel.readException(Parcel.java:1697)
        at android.os.Parcel.readException(Parcel.java:1646)
        at android.app.ActivityManagerProxy.isTopOfTask (ActivityManagerNative.java:6600)
        at android.app.Activity.isTopOfTask(Activity.java:6142)
        at android.app.Activity.onResume(Activity.java:1331)

最好的解决方法是此问题的答案:

在startActivity(intent,bundle animantion)上出现rjava.lang.IllegalArgumentException


-1

问题出在您的包裹上。您需要查找活动,片段,视图将宗地保存到何处以保存状态。会有错误的顺序。例如

@Override
protected Parcelable onSaveInstanceState() {
    Parcelable superState = super.onSaveInstanceState();

    final CharSequence textFromEditText = mTextView.getText();
    if (textFromEditText != null) {
        SavedState savedState = new SavedState(superState);
        savedState.text = textFromEditText.toString();
        return savedState;
    }
    return superState;
}

@Override
protected void onRestoreInstanceState(Parcelable state) {
    if (!(state instanceof SavedState)) {
        super.onRestoreInstanceState(state);
        return;
    }

    SavedState savedState = (SavedState) state;
    super.onRestoreInstanceState(savedState.getSuperState());
    if (!TextUtils.isEmpty(savedState.text)) {
        mTextView.setText(savedState.text);
    }
}

private static class SavedState extends BaseSavedState {
    String text;

    private SavedState(Parcelable superState) {
        super(superState);
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        super.writeToParcel(dest, flags);
        dest.writeString(text);
    }

    private SavedState(Parcel source) {
        super(source);
        text = source.readString();
    }

    public static final Creator<SavedState> CREATOR = new Creator<SavedState>() {

        @Override
        public SavedState createFromParcel(Parcel source) {
            return new SavedState(source);
        }

        @Override
        public SavedState[] newArray(int size) {
            return new SavedState[size];
        }
    };
}

所以问题可能在哪里

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        super.writeToParcel(dest, flags);
        dest.writeString(text);
    }

    private SavedState(Parcel source) {
        super(source);
        text = source.readString();
    }

当宗地货源读取顺序错误时。


问题在于onResume,而不是onRestoreInstanceState
肺炎

我有类似的问题,您知道其他解决方案吗?
阿德里安娜·卡雷利

什么样的问题?
Vytautas Berankis

-1

我的项目中也遇到了同样的问题。在研究过程中,我发现如果您尝试static从Activity的onResume()方法访问任何变量或方法,也有可能。


3
您能否解释一下,访问static变量或方法如何onResume()导致此问题?
Darshna Desai

1
我清楚地提到,在研究过程中,我找到了解决方案。我还提到过,如果您尝试在onResume()方法上访问静态变量,也可能会出现此问题。
阿贾伊·梅塔

巫毒教徒不是答案。
NateS
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.