【发布时间】:2020-03-27 22:13:47
【问题描述】:
我的应用在生产中发生意外崩溃。我有一个活动,我总是开始使用 newInstance 方法,为意图添加一些额外内容:
public static Intent newInstance(final Context context, @NonNull final Location location) {
final Intent intent = new Intent(context, LocationActivity.class);
final Bundle extras = new Bundle();
extras.putParcelable(BUNDLE_ITEM_LOCATION, Location);
intent.putExtras(extras);
return intent;
}
这是我的 onCreate 方法:
@Override
@CallSuper
public void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Bundle extras = getIntent().getExtras();
if (extras == null || !extras.containsKey(BUNDLE_ITEM_LOCATION)) {
throw new IllegalArgumentException(LocationActivity.class.getName()
+ " must be created by using newInstance()");
}
[... Some other code ...]
}
并且这个 IllegalArgumentException 在生产中被抛出。这非常奇怪,因为这种情况不应该发生。知道原因吗?
提前致谢。
【问题讨论】:
-
Location 是你的 Model 还是 android.location?
-
@ParthivMistri 这是我的模特
-
@MohamedAshik 新实例方法和 onCreate 在同一个类中,因此请确保这是相同的最终字符串。
-
是的。你能分享一下你是如何调用这个 newInstance 方法来打开活动的吗?
-
@MohamedAshik 就像 this.startActivity(LocationActivity.newInstance(location));
标签: android android-intent android-activity activity-lifecycle android-bundle