【问题标题】:Activity bundle missing extras even though they should always be added in the newIntance method活动包缺少附加功能,即使它们应始终添加到 newIntance 方法中
【发布时间】: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


【解决方案1】:

试试这个:

public static Intent newInstance(final Context context, @NonNull final Location location {
        final Intent intent = new Intent(context, LocationActivity.class);
        intent.putExtras(BUNDLE_ITEM_LOCATION, Location);
        return intent;
    }

【讨论】:

  • 所以它可能来自捆绑包?我会试试这个谢谢!
  • @DenisPinna 如果您仍然面临同样的问题,请告诉我。
  • @Patrhiv Mistri 显然这个变化仍然存在问题..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-13
  • 1970-01-01
  • 1970-01-01
  • 2019-05-17
  • 2011-10-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多