3

我对 Android/Java 编程相当陌生,并且正在使用我正在使用的非常非常基本的应用程序寻求一些帮助。我纯粹是为了习惯 Java 编码和为 Droids 构建应用程序而构建它,所以请耐心等待。

我有 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
4

4 回答 4

1

我不知道你的问题是什么,但你可以改进你的代码:使用“switch - case”构造。

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();       
        }
于 2012-11-14T18:59:26.650 回答
1

强制关闭将始终在您的 logcat 文件中放置异常堆栈跟踪。请将此放在以后的帖子中,它会告诉我们哪条线路崩溃以及为什么。

我从您的代码和对 Android 的新颖性的猜测 - 您可能没有将 CorrectAnswer 活动添加到您的清单中。

于 2012-11-14T18:43:57.473 回答
0

似乎这条线可能是导致您的问题的原因。

MainQuiz.this.startActivity(myIntent);

删除 MainQuiz,它应该可以工作

this.startActivity(myIntent);
于 2012-11-14T19:01:08.023 回答
0
MainQuiz.this.startActivity(myIntent);

替换为

startActivity(myIntent);
于 2014-05-30T19:10:54.790 回答