Android类投射异常与无线电组小部件?

时间:2014-01-26 20:43:40

标签: java android xml radio-group

构建一个小的测验应用程序,并且代码存在一些问题,它在以前的版本上运行但在全新安装的Windows中删除了,当我尝试打开一个测验的活动时,我不断收到意外错误并且在几行之后记录了日志

Java.lang.classexception:android.widget.Radio Group

活动的代码如下所示

    package com.example.quizzards;
import java.io.FileNotFoundException;
import java.util.Arrays;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;


@SuppressLint("NewApi")
public class GamesActivity extends Activity {

    boolean rightWrong;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_games);
        try {
            @SuppressWarnings("unused")
            Questions questions = new Questions(getApplicationContext());
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        update();

        Button answer = (Button) findViewById(R.id.AnswerButton);

        answer.setOnClickListener(new OnClickListener()
        {
            public void onClick(View V)
            {
                RadioGroup allOptions = (RadioGroup) findViewById(R.id.questions);
                int toCheck = allOptions.getCheckedRadioButtonId();
                if (toCheck == -1) {
                    Toast.makeText(GamesActivity.this, "Please select an option.",
                            Toast.LENGTH_SHORT).show();
                } else {
                    RadioButton selected = (RadioButton) findViewById(allOptions
                            .getCheckedRadioButtonId());
                    rightWrong = Questions.checkAnswer(selected.getText()
                            .toString());
                    if (rightWrong == true) {
                        Toast.makeText(GamesActivity.this, "Right!!!",
                                Toast.LENGTH_SHORT).show();
                        Questions.addPoint();
                        update();
                    } else {
                        Questions.incorrectTry();
                        int remainingTries = Questions.getRemainingTries();
                        if (remainingTries == 0) {
                            //                      Toast.makeText(MainActivity.this,
                            //                              "Your final score is " + Questions.getScore() + ".",
                            //                              Toast.LENGTH_SHORT).show();
                            Intent i = new Intent(GamesActivity.this,
                                    ResultsActivity.class);
                            startActivity(i);
                        } else {
                            Toast.makeText(
                                    GamesActivity.this,
                                    "Wrong!!! " + remainingTries
                                            + " tries left.",
                                    Toast.LENGTH_SHORT).show();
                        }
                    }
                    allOptions.clearCheck();
                }
            }
        });

    }

    @Override
    public void onBackPressed() {
        Intent i = new Intent(GamesActivity.this, PlayActivity.class);
        startActivity(i);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.games, menu);
        return true;
    }

    public void update()
    {
        if (Questions.finished()) {
            Intent i = new Intent(GamesActivity.this,
                    ResultsActivity.class);
            startActivity(i);
        } else {
            String[] aQuestion = Questions.getNextQuestion();
            String[] temp = Arrays.copyOfRange(aQuestion, 1, aQuestion.length);
            for (int i = 0; i < temp.length; i++) {
                int r = (int) (Math.random() * (i + 1));
                String swap = temp[r];
                temp[r] = temp[i];
                temp[i] = swap;
            }
            TextView textViewQuestion = (TextView) findViewById(R.id.questions);
            textViewQuestion.setText(aQuestion[0]);
            TextView textViewOption1 = (TextView) findViewById(R.id.option1);
            textViewOption1.setText(temp[0]);
            TextView textViewOption2 = (TextView) findViewById(R.id.option2);
            textViewOption2.setText(temp[1]);
            TextView textViewOption3 = (TextView) findViewById(R.id.option3);
            textViewOption3.setText(temp[2]);
            TextView textViewOption4 = (TextView) findViewById(R.id.option4);
            textViewOption4.setText(temp[3]);
            TextView runningTotal = (TextView) findViewById(R.id.runningTotal);
            runningTotal.setText("Total: " + Questions.getScore());
        }
    }
}

XML文件包含一个广播组和几个单选按钮,当我尝试运行没有任何单选按钮的代码并注释掉代码时,效果很好

1 个答案:

答案 0 :(得分:1)

  1. 确保您的所有RadioButtonsRadioGroup在布局文件中都有不同的ID。
  2. 另一个小优化是在RadioButtons内寻找RadioGroup,如下所示。

    RadioButton selected = (RadioButton) allOptions.findViewById(
        allOptions.getCheckedRadioButtonId());