简体   繁体   English

变量包含意外值

[英]A variable holds an unexpected value

I'm building a module for checking an answer in my Android java app and it doesn't work.我正在构建一个模块来检查我的 Android java 应用程序中的答案,但它不起作用。 While debugging it shows that the variable holds a value that is completely unexpected for it to hold.调试时它显示该变量持有一个完全出乎意料的值。 Could please someone explain what the problem might be?请有人解释可能是什么问题? Here is the module:这是模块:

private void QuizOperations() {

        Toast.makeText(QuizActivity.this,"quizOperation", Toast.LENGTH_SHORT).show();
        answered = true; // when the question is already answered Set bool to true
        RadioButton rbSelected = findViewById(rbGroup.getCheckedRadioButtonId());
        int indexofchild =rbGroup.indexOfChild(rbSelected) +1;
        int answerNr = indexofchild  +1;
        checkSolution(answerNr,rbSelected);// method checks if the answer that is selected by the user corresponds to the answer in the database
    }

The intended way was rbselected getting the index of the RadioButton pressed by the user, and than answerNr gets the index of this button as int.预期的方式是 rbselected 获取用户按下的 RadioButton 的索引,而不是 answerNr 获取此按钮的索引作为 int。 Than it passes in to the checksolution() function which checks if the AnswerNr corresponds to the right answer in the database.然后它传递给 checksolution() function 检查 AnswerNr 是否对应于数据库中的正确答案。 However, while debugging answerNr holds the value of 5 whichever button I press.但是,在调试时,无论我按下哪个按钮,answerNr 的值都为 5。

Debug screenshot调试截图

Let me know if any additional code needed.让我知道是否需要任何其他代码。 Thanks a lot非常感谢

Here is a very minimalistic example on how to detect the view is being pressed, set/get id from tag.这是一个非常简单的示例,说明如何检测正在按下的视图,从标签设置/获取 id。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:background="#333333">
    <RadioButton
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/radioButton1"/>
    <RadioButton
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/radioButton2"/>
    <RadioButton
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/radioButton3"/>
</LinearLayout>
public class MainActivity extends AppCompatActivity implements View.OnClickListener
{
    private String[] answers = {
        "January",
        "February",
        "March",
    };
    
    private RadioButton rb1, rb2, rb3;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        rb1 = findViewById(R.id.radioButton1);
        rb2 = findViewById(R.id.radioButton2);
        rb3 = findViewById(R.id.radioButton3);
        
        rb1.setTag("0");
        rb2.setTag("1");
        rb3.setTag("2");
        
        rb1.setOnClickListener(this);
        rb2.setOnClickListener(this);
        rb3.setOnClickListener(this);
    }

    @Override
    public void onClick(View view)
    {
        final int index = Integer.parseInt((String)view.getTag());
        switch(view.getId()) {
            case R.id.radioButton1:
                rb2.setChecked(false);
                rb3.setChecked(false);
                Toast.makeText(MainActivity.this, answers[index], Toast.LENGTH_SHORT).show();
            break;
            case R.id.radioButton2:
                rb1.setChecked(false);
                rb3.setChecked(false);
                Toast.makeText(MainActivity.this, answers[index], Toast.LENGTH_SHORT).show();
            break;
            case R.id.radioButton3:
                rb1.setChecked(false);
                rb2.setChecked(false);
                Toast.makeText(MainActivity.this, answers[index], Toast.LENGTH_SHORT).show();
            break;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM