我正在尝试创建一个简单的android测验应用程序。我已经成功创建了活动类和xml文件,该文件未显示任何错误。但是,当我在模拟器中运行该程序时,一旦到达具有多个图像按钮的特定活动类,程序就会意外停止。我不知道该怎么办。需要帮忙!

这是我的logcat的样本。
![在此处输入图片描述] [1]

这是我的代码

package com.example.brainlab;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;

import android.view.Menu;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
//import android.widget.TextView;
import android.widget.Toast;

public class Question9 extends Activity implements OnClickListener{
   private ImageButton button1;
   private ImageButton button2;
   private ImageButton button3;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_queston9);

    button1 = (ImageButton) findViewById(R.id.imageButton1);
    button1.setOnClickListener(this);
    button2 = (ImageButton) findViewById(R.id.imageButton2);
    button2.setOnClickListener(this);
    button3 = (ImageButton) findViewById(R.id.imageButton3);
    button3.setOnClickListener(this);
}

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

@Override
public void onClick(View v) {

    Intent intent = new Intent(Question9.this, Question2.class);
            switch(v.getId()){

            case (R.id.imageButton1):{
                                    Toast.makeText(getApplicationContext(), "Answer     saved.", Toast.LENGTH_SHORT).show();
                startActivity(intent);

            }
            break;

            case (R.id.imageButton2):
            {

                Toast.makeText(getApplicationContext(), "Answer       saved.", Toast.LENGTH_SHORT).show();
                startActivity(intent);
            }

            break;

            case (R.id.imageButton3):
            {

Toast.makeText(getApplicationContext(), "Answer saved.", Toast.LENGTH_SHORT).show();
                startActivity(intent);
            }
            }

}
}

最佳答案

我认为您的Switch Case是错误的。尝试与以下更正

 switch(v.getId()){

        case R.id.imageButton1:

         //Do your job
         break;
        case R.id.imageButton2:

         //Do your job
         break;
        case R.id.imageButton3:

         //Do your job
         break;

        default:
     break;
     }

09-13 05:25