您好我正在尝试创建一个简单的旗帜测验应用程序,其中出现一个标志图像,用户从3个选项中选择正确的答案,但是在显示两个标志后应用程序会继续崩溃。我认为这可能与图像阵列中的内存泄漏有关,但我无法弄清楚如何阻止它。我试图将图像设置为null,但是当我尝试mapPics [i] = null时它会显示错误;如下所示。该错误表示图像不能设置为null,因为它是一个整数。任何帮助将不胜感激!
public class game1 extends Activity implements View.OnClickListener
{
static Button next;
static ImageView mainpic;
static RadioGroup radioGroup;
static RadioButton option1;
static RadioButton option2;
static RadioButton option3;
static int[] mapPics = new int[]{R.drawable.america, R.drawable.england, R.drawable.australia, R.drawable.poland, R.drawable.sweden, R.drawable.spain};
static String [] answers= new String[]{"Spain", "Poland", "Sweden", "America", "England", "Australia"};
static int [] correctAnswer= new int[]{2, 1};
static int score=0;
static int i=0;
static int a=0;
static int b=1;
static int c=2;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.game1);
next= (Button)findViewById(R.id.nextButton);
mainpic= (ImageView)findViewById(R.id.imageView);
mainpic.setImageResource(mapPics[i]);
next.setOnClickListener(this);
addListenerRadioGroup();
option1.setText(String.valueOf(answers[a]));
option2.setText(String.valueOf(answers[b]));
option3.setText(String.valueOf(answers[c]));
}//On Create
public void addListenerRadioGroup(){
option1= (RadioButton)findViewById(R.id.radioButton);
option2= (RadioButton)findViewById(R.id.radioButton2);
option3= (RadioButton)findViewById(R.id.radioButton3);
radioGroup = (RadioGroup)findViewById(R.id.radioGroupAnswers);
radioGroup.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
radioGroup.getCheckedRadioButtonId();
}
});
}
@Override
public void onClick(View v)
{
getSelectedAnswer();
i++;
a=a+3;
b=b+3;
c=c+3;
if(i >=1) {
Intent myIntent = new Intent(this, scores.class);
myIntent.putExtra("scores", score);
startActivity(myIntent);
}//if
mainpic.setImageResource(mapPics[i]);
option1.setText(String.valueOf(answers[a]));
option2.setText(String.valueOf(answers[b]));
option3.setText(String.valueOf(answers[c]));
mapPics[i]=null;
}//onClick
public void getSelectedAnswer() {
int index = radioGroup.indexOfChild(findViewById(radioGroup.getCheckedRadioButtonId()));
if (index==correctAnswer[i])
score++;
}//get selected answer
}//class
答案 0 :(得分:4)
您收到的错误消息确切地告诉您问题所在
您已将mapPics
声明为int
:
static int[] mapPics = new int[]{R.drawable.america, R.drawable.england, R.drawable.australia, R.drawable.poland, R.drawable.sweden, R.drawable.spain};
int
是基本类型,因此您无法将其设置为null。如果您想要可以为空的类型,请改用Integer[]
。