getBooleanExtra()仅使用默认参数,而不使用putExtra()传递的参数 [英] getBooleanExtra() using only the default argument and not the one passed by putExtra()

查看:2850
本文介绍了getBooleanExtra()仅使用默认参数,而不使用putExtra()传递的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是代码.在这部分中,应该将answerIsTrue变量初始化为true,正确地将其初始化(我已调试并检查),并且还正确地将其传递到了putExtra()中(同样,我已调试并检查了).

Here's the code. In this part, the answerIsTrue variable should be initialized to true, which it rightly does (I debugged and checked) and is rightly also passed into putExtra() (again, I debugged and checked).

mCheatButton.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            Intent i = new Intent(QuizActivity.this, CheatActivity.class);
            boolean answerIsTrue = mQuestionBank[mCurrentIndex].isTrueQuestion();
            i.putExtra(CheatActivity.EXTRA_ANSWER_IS_TRUE, answerIsTrue);
            startActivity(i);
        }
    });

但是进入另一个类时,尽管由putExtra()传递的参数为true,但变量mAnswerIsTrue被分配为false(可能是由于默认参数).这是代码.

But coming to a different class, the variable mAnswerIsTrue gets assigned to false (probably due to the default argument) despite the argument being passed by putExtra() is true. Here's the code.

mAnswerIsTrue = getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false);

我也调试了这一行,并且确实将其分配为false.有什么事吗

I debugged this line as well, and it does get assigned to false. What could be wrong?

这是完整的CheatActivity类:

public class CheatActivity extends Activity {
public static final String EXTRA_ANSWER_IS_TRUE = "com.bignerdranch.android.geoquiz.answer_is_true";
private Button mShowAnswerButton;
private boolean mAnswerIsTrue;
private TextView mAnswerTextView;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cheat);
    mAnswerIsTrue = getIntent().getBooleanExtra(CheatActivity.EXTRA_ANSWER_IS_TRUE, false);
    mAnswerTextView = (TextView)findViewById(R.id.answerTextView);
    mShowAnswerButton = (Button)findViewById(R.id.showAnswerButton);
    mShowAnswerButton.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            if (mAnswerIsTrue){
                mAnswerTextView.setText(R.id.true_button);
            }else{
                mAnswerTextView.setText(R.id.false_button);
            }
        }
    });
}

}

注意:我是一个完整的初学者,他刚刚学习调试.

Note: I'm a complete beginner, who just learnt debugging.

推荐答案

我不确定,但是我最好的猜测是getBooleanExtra()不好.我建议使用简单的getExtras,然后获取您的价值.

I cannot be sure, but my best guess that, getBooleanExtra() is not good. I suggest using simple getExtras and then getting your value.

 i.putExtra(EXTRA_ANSWER_IS_TRUE, value);

 Bundle args = MyActivity.getIntent().getExtras();
 boolean istrue= args.getBoolean(EXTRA_ANSWER_IS_TRUE, false);

这篇关于getBooleanExtra()仅使用默认参数,而不使用putExtra()传递的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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