Crashes after startActivityForResult in API 27

别说谁变了你拦得住时间么 提交于 2019-12-11 10:27:57

问题


After updating to API 27 and Support library 27.0.2 suddenly I get a lot of these stack traces in Crashlytics:

Fatal Exception: java.lang.IllegalArgumentException
    at android.os.Parcel.readException(Parcel.java:1544)
    at android.os.Parcel.readException(Parcel.java:1493)
    at android.app.ActivityManagerProxy.isTopOfTask(ActivityManagerNative.java:5108)
    at android.app.Activity.isTopOfTask(Activity.java:5688)
    at android.app.Activity.startActivityForResult(Activity.java:3973)
    at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(Source:54)
    at android.support.v4.app.FragmentActivity.startActivityForResult(Source:67)

I call this like:

ActivityOptions options = ActivityOptions.makeCustomAnimation(activity, R.anim.slide_in_from_right, R.anim.fade_out);
startActivityForResult( intent, REQ_ACTION, options.toBundle());

I cannot read the source code as it is not released yet. I even tried to replace and use android-26 code, but it's different.

There is a warning for the above call saying that BaseFragmentActivityApi16.startActivityForResult can only called from the same library group, so I fixed it by using ActivityCompat, but I don't think it will solve the crash problem.

Is this a platform issue or can I fix this?

Edit

if (Build.VERSION.SDK_INT >= 21) {
    ActivityOptions options = ActivityOptions.makeCustomAnimation(activity, R.anim.slide_in_from_right, R.anim.fade_out);
    startActivityForResult(intent, REQ_ACTION, options.toBundle());
} else {
    ActivityOptions options = ActivityOptions.makeCustomAnimation(activity, R.anim.slide_in_from_right, R.anim.fade_out);
    ActivityCompat.startActivityForResult(this, intent, REQ_ACTION, options.toBundle());
}

If I change it to the above according to the link in my comment, Android Studio complaining like above. This might be related to the problem.


回答1:


You can try this code.

startActivityForResult( intent, REQ_ACTION)
overridePendingTransition(R.anim.slide_in_from_right,  R.anim.fade_out);



回答2:


Old post but unanswered, so here what I found in 21+

Make sure you are looking for startActivityForResult under activity object. Under Context object you can find startActvity but you will not see startActivityForResult method.

If your context is Context class but is an activity then make sure you cast it to Activity.

Context context = ...;
context.startActivityForResult(...); // this method will not exist
((Activity)context).startActivityForResult(...); // this method should be ok



回答3:


Use ActivityOptionsCompat instead of ActivityOptions for below api 21.

ActivityOptionsCompat is a helper class for accessing features in ActivityOptions in a backwards compatible fashion.

if (Build.VERSION.SDK_INT >= 21) {
  ActivityOptions options = ActivityOptions.makeCustomAnimation(activity, 
  R.anim.slide_in_from_right, R.anim.fade_out);
  startActivityForResult(intent, REQ_ACTION, options.toBundle()); 
  } else {
   ActivityOptionsCompat options = ActivityOptionsCompat
  .makeCustomAnimation(activity,R.anim.slide_in_from_right,R.anim.fade_out);
   ActivityCompat.startActivity(this, intent, options.toBundle());
}

Hope this will help you.



来源:https://stackoverflow.com/questions/47639597/crashes-after-startactivityforresult-in-api-27

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!