我是Android / Java编程的新手,并且正在寻找一些我正在使用的非常非常基本的应用程序的帮助。我纯粹是为了习惯Java编码和为机器人构建应用程序而构建这个,所以请耐心等待。
我有10个单选按钮组合在一起,其中只有一个是正确答案(按钮#10)。如果用户点击它们中的任何一个1-9我会显示一条消息“对不起再试”,但是如果他们点击#10,我希望它带他们去一个新的活动,即我将要的页面提出一些图形和文字,说明这种性质的混合物和事物。这些按钮适用于1-9,但是当我点击#10时,我得到一个强制关闭错误。
这是我到目前为止所拥有的:
public class MainQuiz extends Activity implements OnCheckedChangeListener, android.widget.RadioGroup.OnCheckedChangeListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_quiz);
((RadioGroup)findViewById(R.id.radio_group)).setOnCheckedChangeListener(this);}
public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
String numeral = null;
if (checkedId == R.id.button1) {
numeral = "Nope, try again!";
} else if (checkedId == R.id.button2) {
numeral = "Nope, try again!";
} else if (checkedId == R.id.button3) {
numeral = "Nope, try again!";
}
else if (checkedId == R.id.button4) {
numeral = "Nope, try again!";
}
else if (checkedId == R.id.button5) {
numeral = "Nope, try again!";
}
else if (checkedId == R.id.button6) {
numeral = "Nope, try again!";
}
else if (checkedId == R.id.button7) {
numeral = "Nope, try again!";
}
else if (checkedId == R.id.button8) {
numeral = "Nope, try again!";
}
else if (checkedId == R.id.button9) {
numeral = "Nope, try again!";
}
else if (checkedId == R.id.button10) {
Intent myIntent = new Intent(MainQuiz.this, CorrectAnswer.class);
MainQuiz.this.startActivity(myIntent);
}
Toast.makeText(getApplicationContext(), ""+numeral+"",
Toast.LENGTH_SHORT).show(); }
任何帮助都会受到赞赏,我确信这是一个简单的解决方案,我确信它在语法和代码方面有些明显,我已经搞砸了,但我又是新的:)
谢谢!
这是强制关闭后的日志:
11-14 13:56:48.982: D/AndroidRuntime(862): Shutting down VM
11-14 13:56:48.982: W/dalvikvm(862): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
11-14 13:56:48.992: E/AndroidRuntime(862): FATAL EXCEPTION: main
11-14 13:56:48.992: E/AndroidRuntime(862): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.test.quiz/com.test.quiz.CorrectAnswer}: java.lang.ClassCastException: com.test.quiz.CorrectAnswer
11-14 13:56:48.992: E/AndroidRuntime(862): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
11-14 13:56:48.992: E/AndroidRuntime(862): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-14 13:56:48.992: E/AndroidRuntime(862): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-14 13:56:48.992: E/AndroidRuntime(862): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-14 13:56:48.992: E/AndroidRuntime(862): at android.os.Handler.dispatchMessage(Handler.java:99)
11-14 13:56:48.992: E/AndroidRuntime(862): at android.os.Looper.loop(Looper.java:123)
11-14 13:56:48.992: E/AndroidRuntime(862): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-14 13:56:48.992: E/AndroidRuntime(862): at java.lang.reflect.Method.invokeNative(Native Method)
11-14 13:56:48.992: E/AndroidRuntime(862): at java.lang.reflect.Method.invoke(Method.java:521)
11-14 13:56:48.992: E/AndroidRuntime(862): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-14 13:56:48.992: E/AndroidRuntime(862): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-14 13:56:48.992: E/AndroidRuntime(862): at dalvik.system.NativeStart.main(Native Method)
11-14 13:56:48.992: E/AndroidRuntime(862): Caused by: java.lang.ClassCastException: com.test.quiz.CorrectAnswer
11-14 13:56:48.992: E/AndroidRuntime(862): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
11-14 13:56:48.992: E/AndroidRuntime(862): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
11-14 13:56:48.992: E/AndroidRuntime(862): ... 11 more
答案 0 :(得分:1)
强制关闭将始终在logcat文件中放置异常堆栈跟踪。请把它放在以后的帖子中,它告诉我们哪条线路崩溃了,为什么。
我猜您的代码和Android的新功能 - 您可能没有将CorrectAnswer活动添加到您的清单。
答案 1 :(得分:1)
我不知道你的问题是什么,但你可以改进你的代码: 使用“开关 - 箱”结构。
switch (chekedId) {
case R.id.button10:
Intent myIntent = new Intent(MainQuiz.this, CorrectAnswer.class);
startActivity(myIntent);
break;
default:
Toast.makeText(getApplicationContext(), "Nope, try again!",
Toast.LENGTH_SHORT).show();
}
答案 2 :(得分:0)
看起来这条线可能是造成问题的原因。
MainQuiz.this.startActivity(myIntent);
删除MainQuiz,它应该可以正常工作
this.startActivity(myIntent);
答案 3 :(得分:0)
MainQuiz.this.startActivity(myIntent);
替换为
startActivity(myIntent);