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

问题描述 投票:48回答:3

我的应用程序在运行版本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
}

我做了一些挖掘这个,发现这个类似的question

由于抛出的IllegalArgumentException不包含任何详细信息,我将尝试按照建议的here添加额外的日志。

同时请欣赏任何帮助!

android android-activity android-8.0-oreo android-7.0-nougat
3个回答
1
投票

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


0
投票

问题在于您的包裹。您需要找到活动,片段,视图保存包裹以保存状态的位置。会有错误的顺序。例如

@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();
    }

当包裹源读数序列不好时。


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

它是一个链式的exeption,所以检查e.getCause()stacktrace - > until cause将为null

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

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)

最好的拍摄它是这个问题的答案:

rjava.lang.IllegalArgumentException on startActivity(intent,bundle animantion)

© www.soinside.com 2019 - 2024. All rights reserved.