【问题标题】:Put Extra boolen in android give wrong value [duplicate]在android中放置额外的布尔值会给出错误的值[重复]
【发布时间】:2017-05-05 02:49:53
【问题描述】:

我很奇怪,为什么我在输入额外的布尔值后得到了错误的值。很奇怪。我知道其他帖子已经回答了关于 put extra 的问题,但是这篇帖子我不知道为什么我得到了错误的值。

这是我的第一个活动。只是简短的代码。

btnActivity.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i = new Intent(getApplicationContext(), ActivitySecond.class);
            startActivityForResult(i, 1);
        }
    });

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode==1)
    {
        boolean thisAnwser = getIntent().getBooleanExtra("thisAnwserBoolean",false);
        Log.i("this boolean is","Answer: "+thisAnwser); //this log, i got false..

        if(thisAnwser){
            Log.i("Good this true","yes");
        }


    }
}

这是第二个活动

Intent intent = new Intent();
intent.putExtra("thisAnwserBoolean", true); // when i try log, i got true.
setResult(1,intent);
finish();

【问题讨论】:

  • 你看错了Intent。您想从onActivityResult() 方法中的Intent data 参数中获取额外内容。
  • 是的,你是对的......谢谢先生
  • Bundle extra=data.getExtras(); boolean thisAnwser = extra.getBoolean("thisAnwserBoolean"); or boolean thisAnwser = data.getExtras().getBoolean("thisAnwserBoolean");

标签: android boolean


【解决方案1】:
 boolean thisAnswer = getIntent().getExtras().getBoolean("thisAnwserBoolean");

您正在添加false,因此您将始终获得false。删除false

【讨论】:

  • 这个我也试了,还是假的
  • 谢谢,但我已经得到答案了,这个Bundle extra=data.getExtras(); boolean thisAnwser = extra.getBoolean("thisAnwserBoolean"); or boolean thisAnwser = data.getExtras().getBoolean("thisAnwserBoolean");
  • 太好了,你解决了。
【解决方案2】:

试试这个:

Boolean yourBool = getIntent().getExtras().getBoolean("yourBoolName");

【讨论】:

  • 谢谢,但我已经得到了答案。这个Bundle extra=data.getExtras(); boolean thisAnwser = extra.getBoolean("thisAnwserBoolean"); or boolean thisAnwser = data.getExtras().getBoolean("thisAnwserBoolean");
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-30
  • 2017-10-14
  • 2014-01-02
  • 2013-04-09
  • 2021-07-03
  • 1970-01-01
相关资源
最近更新 更多