Building a small quiz application and have some issues with the code, it worked on a previous version but that got delete during a fresh install of windows, when i try open up the a activity for a quiz i keep getting an unexpected error and the log says after a few lines
Java.lang.classexception : android.widget.Radio Group
the code for the activity looks like this
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());
}
}
}
The XML file contains a radio group and few radio buttons, when i try to run the code without any sort of radio buttons and comment out the code, works perfectly
RadioButtons
and RadioGroup
have different id's in the layout file. Another small optimization would be to look for the RadioButtons
inside RadioGroup
as following.
RadioButton selected = (RadioButton) allOptions.findViewById( allOptions.getCheckedRadioButtonId());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.