繁体   English   中英

Android Class广播组小部件的强制转换异常?

[英]Android Class cast exception with radio group widget?

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

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

该活动的代码如下所示

    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. 确保您的所有RadioButtonsRadioGroup在布局文件中具有不同的ID。
  2. 另一个小的优化将是在RadioGroup查找RadioButtons ,如下所示。

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

暂无
暂无

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

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