getIntent()始终返回null [英] getIntent() always returns null

查看:106
本文介绍了getIntent()始终返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码开始活动:

Intent intent = new Intent(context, GameActivity.class);
    intent.putExtra("load", true);
    startActivity(intent);

这是我尝试获得新活动意图的方法:

This is how I try to get the intent in the new activity:

private Intent intent = this.getIntent();

有人告诉我尝试包括以下内容:

I was told to try and include:

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
}

但这没有帮助.在几次崩溃之后,我添加了:

But it doesn|t help. After several crashes I added:

if (intent == null) Log.w("Intent", "Intent is null");

我在做什么错了?

推荐答案

private Intent intent = this.getIntent();

onCreate 内或生命周期的另一个回调中调用 getIntent().

Call getIntent() inside onCreate or in another callback of the life cycle.

这篇关于getIntent()始终返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆