我正在尝试创建一个简单的Android测验应用程序。我已经成功创建了活动类和xml文件,它没有显示任何错误。但是当我在我的模拟器中运行程序时,很快它到达我有多个图像按钮的特定活动类,程序意外停止。我不知道该怎么做。需要帮助!
HEre是我的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);
}
}
}
}
答案 0 :(得分:1)
我认为你的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;
}
答案 1 :(得分:0)
感谢。我修好了。我还没有在清单文件中声明我已经完成的活动有问题。现在一切正常..