意图包返回Null每一次

意图包返回Null每一次

本文介绍了意图包返回Null每一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我送到一个新的意图一些额外的。在那里,它抓住包,并测试它是否是。每一次它是即使我能得到传递的值,并利用它们。

有人能看到什么是错的if语句?

 意图I = getIntent();
叠B = i.getExtras();
INT挑= b.getInt(匹克);
诠释正确= b.getInt(CORR);
TYPE = b.getString(兰德);
如果(二== NULL ||!b.containsKey(井)){
    Log.v(捆绑,包子为空);
} 其他 {
    Log.v(捆绑,得到了包子很好);
}
 

编辑:继承人在其中创建包

 意向意图=新的意图(这一点,app.pack.son.class);
叠B =新包();
b.putInt(匹克,挑选);
b.putInt(CORR,科尔);
b.putString(RAND,是);
intent.putExtras(B);
startActivity(意向);
 

解决方案

我不认为这个问题是你的包是。它不可能是因为你会得到一个 NullPointerException异常更快。

现在的问题是,你的错误信息是错误的。更改此设置:

 如果(二== NULL ||!b.containsKey(井)){
    Log.v(捆绑,包子为空);
} 其他 {
    // ...
}
 

要这样:

 如果(!b.containsKey(井)){
    Log.v(捆绑,包不包含关键WELL);
} 其他 {
    // ...
}
 

和为什么包不包含这个关键的原因,是因为你没有添加它。

I have some extras sent to a new intent. There it grabs the bundle and tests if it is null. Every single time it is null even though I am able to get the values passed and use them.

Can anyone see what is wrong with the if statement?

Intent i = getIntent();
Bundle b = i.getExtras();
int picked = b.getInt("PICK");
int correct = b.getInt("CORR");
type = b.getString("RAND");
if(b == null || !b.containsKey("WELL")) {
    Log.v("BUNDLE", "bun is null");
} else {
    Log.v("BUNDLE", "Got bun well");
}

EDIT: Heres where the bundle is created.

Intent intent = new Intent(this, app.pack.son.class);
Bundle b = new Bundle();
b.putInt("PICK", pick);
b.putInt("CORR", corr);
b.putString("RAND", "yes");
intent.putExtras(b);
startActivity(intent);
解决方案

I don't think the problem is that your bundle is null. It can't be because you'd get a NullPointerException much sooner.

The problem is that your error message is wrong. Change this:

if(b == null || !b.containsKey("WELL")) {
    Log.v("BUNDLE", "bun is null");
} else {
    // ...
}

To this:

if (!b.containsKey("WELL")) {
    Log.v("BUNDLE", "bundle does not contain key WELL");
} else {
    // ...
}

And the reason why the bundle doesn't contain this key, is because you didn't add it.

这篇关于意图包返回Null每一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 15:29