代码无效

时间:2011-11-01 18:10:30

标签: android string button arraylist

我的应用程序有一个问题列表。当调用functoion getfirstquestion时,它会随机选择一个问题,然后随机地将文本放在4个按钮中的每个按钮上(其中一个是问题的答案)。 / p>

每次按下按钮,函数checkanswer将验证按钮文本是否与正确问题的正确答案相等。如果是,则删除当前问题,并调用getfirstquestion()函数来更改问题。 / p>

程序运行正常,但是当我按下正确的答案按钮时没有任何反应。任何人都可以告诉我原因,并给我一个解决方案?我在2小时前搜索代码....

    public class startgame extends Activity implements OnClickListener{

final Random rgenerator = new Random();

List<String> questionss1 = new ArrayList<String>();
String thequestion;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.level1);

    questionss1.add("Who is the actual CEO at Apple?");
    questionss1.add("Who is the actual CEO at Microsoft?");
    questionss1.add("Who is the actual CEO at Google?");
    getfirstquestion();

    Button button1 = (Button)findViewById(R.id.button1);
    button1.setOnClickListener(this);

    Button button2 = (Button)findViewById(R.id.button2);
    button2.setOnClickListener(this);
    Button button3 = (Button)findViewById(R.id.button3);
    button3.setOnClickListener(this);
    Button button4 = (Button)findViewById(R.id.button4);
    button4.setOnClickListener(this);
    }
    }

public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()){
    case R.id.button1:  

        Button button1 = (Button)findViewById(R.id.button1);
        checkanswer((String) button1.getText());

    case R.id.button2:
       Button button2 = (Button)findViewById(R.id.button2);
           checkanswer((String) button2.getText());

    case R.id.button3:
        Button button3 = (Button)findViewById(R.id.button2);
    checkanswer((String) button3.getText());

    case R.id.button4:
        Button button4 = (Button)findViewById(R.id.button2);
       checkanswer((String) button4.getText());
    }
}
public int checkanswer(String buttontext) {
    if (thequestion.equals("Who is the actual CEO at Apple?") && buttontext == "Tim Cook"){
        questionss1.remove("Who is the actual CEO at Apple?");
        getfirstquestion();
    }

    if (thequestion.equals("Who is the actual CEO at Microsoft?") && buttontext  == "Steve Ballmer"){
        questionss1.remove("Who is the actual CEO at Microsoft?");
        getfirstquestion();
    }

    if (thequestion.equals("Who is the actual CEO at Google?") && buttontext == "Eric Schmidt"){
        questionss1.remove("Who is the actual CEO at Google?");
        getfirstquestion();
    }
    return 0;
}

public void getfirstquestion(){
    //create the buttons
    Button button1 = (Button)findViewById(R.id.button1);
    Button button2 = (Button)findViewById(R.id.button2);
    Button button3 = (Button)findViewById(R.id.button3);
    Button button4 = (Button)findViewById(R.id.button4);

    thequestion = questionss1.get(rgenerator.nextInt(questionss1.size()));
    TextView question = (TextView)findViewById(R.id.textView1);
    question.setText(thequestion);
    questionss1.remove(thequestion);

    if (thequestion.equals("Who is the actual CEO at Apple?")){
        List<String> questions1res = new ArrayList<String>();
        questions1res.add("Eric Schmidt");
        questions1res.add("Steven Sinofsky");
        questions1res.add("Tim Cook");
        questions1res.add("Steve Ballmer");

        button1.setText(questions1res.get(rgenerator.nextInt(questions1res.size())));
        questions1res.remove(button1.getText());
        button2.setText(questions1res.get(rgenerator.nextInt(questions1res.size())));
        questions1res.remove(button2.getText());
        button3.setText(questions1res.get(rgenerator.nextInt(questions1res.size())));
        questions1res.remove(button3.getText());
        button4.setText(questions1res.get(rgenerator.nextInt(questions1res.size())));
        questions1res.remove(button4.getText());
    }

    if (thequestion.equals("Who is the actual CEO at Microsoft?")){
        List<String> questions1res = new ArrayList<String>();
        questions1res.add("Eric Schmidt");
        questions1res.add("Steven Sinofsky");
        questions1res.add("Tim Cook");
        questions1res.add("Steve Ballmer");

        button1.setText(questions1res.get(rgenerator.nextInt(questions1res.size())));
        questions1res.remove(button1.getText());
        button2.setText(questions1res.get(rgenerator.nextInt(questions1res.size())));
        questions1res.remove(button2.getText());
        button3.setText(questions1res.get(rgenerator.nextInt(questions1res.size())));
        questions1res.remove(button3.getText());
        button4.setText(questions1res.get(rgenerator.nextInt(questions1res.size())));
        questions1res.remove(button4.getText());
    }

    if (thequestion.equals("Who is the actual CEO at Google?")){
        List<String> questions1res = new ArrayList<String>();
        questions1res.add("Eric Schmidt");
        questions1res.add("Steven Sinofsky");
        questions1res.add("Tim Cook");
        questions1res.add("Steve Ballmer");

        button1.setText(questions1res.get(rgenerator.nextInt(questions1res.size())));
        questions1res.remove(button1.getText());
        button2.setText(questions1res.get(rgenerator.nextInt(questions1res.size())));
        questions1res.remove(button2.getText());
        button3.setText(questions1res.get(rgenerator.nextInt(questions1res.size())));
        questions1res.remove(button3.getText());
        button4.setText(questions1res.get(rgenerator.nextInt(questions1res.size())));
        questions1res.remove(button4.getText());
    }
}
    }

2 个答案:

答案 0 :(得分:3)

在方法checkanswer中,您应该使用equals方法,而不是==运算符,因为它会比较引用而不是对象的内容。即代替buttontext == "Tim Cook",它应该是buttontext.equals("Tim Cook")等。

此外,再次查看代码,您应该在交换机中添加break;语句:

switch (v.getId()){
case R.id.button1:
    Button button1 = (Button)findViewById(R.id.button1);
    checkanswer((String) button1.getText());
    break;
case R.id.button2:
    Button button2 = (Button)findViewById(R.id.button2);
    checkanswer((String) button2.getText());
    break;
case R.id.button3:
    Button button3 = (Button)findViewById(R.id.button2);
    checkanswer((String) button3.getText());
    break;
case R.id.button4:
    Button button4 = (Button)findViewById(R.id.button2);
    checkanswer((String) button4.getText());
    break;
}

答案 1 :(得分:1)

button1.setOnClickListener(本);

此代码用于将OnClickListener设置为按钮。您需要在类中实例化OnClickListener并使用它。传入(此)将传递活动。在这种情况下,按钮单击将不执行任何操作。

这样的事情:

button1.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        // do your checking here
    }
};

试试看,让我知道它是如何运作的。