我正在尝试创建一个ImageView控件数组并为其添加可绘制资源。我正在
引起:java.lang.NullPointerException:尝试调用虚拟 方法'void android.widget.ImageView.setBackgroundResource(int)'上 null对象引用
以下是代码:
public class MultipleChoice extends ActionBarActivity {
TextView Left;
TextView Operator;
TextView Right;
TextView GameTimer;
TextView CorrectAnswers;
ImageView imageView[] = new ImageView[11];
int imageViewNumber;
Button btnStart;
int intLeft;
int intOperator;
int intRight;
int intCorrectAnswer;
int intCorrectAnswers;
String strCorrectAnswer;
CountDownTimer problemtimer = new CountDownTimer(10000,1000) {
@Override
public void onTick(long millisUntilFinished) {
}
@Override
public void onFinish() {
NextProblem();
}
};
CountDownTimer gametimer = new CountDownTimer(60000,1000) {
@Override
public void onTick(long millisUntilFinished) {
GameTimer.setText("Time Left: " + millisUntilFinished / 1000);
}
@Override
public void onFinish() {
problemtimer.cancel();
gametimer.cancel();
GameTimer.setText("Time Left: 0");
btnStart.setEnabled(true);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_multiple_choice);
SetStartupValues();
}
private void SetStartupValues(){
Left = (TextView) findViewById(R.id.Left);
Operator = (TextView) findViewById(R.id.Operator);
Right = (TextView) findViewById(R.id.Right);
GameTimer = (TextView) findViewById(R.id.Timeleft);
CorrectAnswers = (TextView) findViewById(R.id.CorrectAnswers);
btnStart = (Button) findViewById(R.id.btnStart);
Left.setText("");
Operator.setText("");
Right.setText("");
GameTimer.setText("Time Left: 60");
CorrectAnswers.setText("Correct: 0");
intCorrectAnswers = 0;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void btnStart_Click(View view) {
// Do something in response to button click
SetStartupValues();
NextProblem();
gametimer.start();
btnStart.setEnabled(false);
}
public void NextProblem(){
intLeft = new Random().nextInt(100);
intRight= new Random().nextInt(100);
intOperator= new Random().nextInt(3);
Left.setText(String.valueOf(intLeft));
Right.setText(String.valueOf(intRight));
imageViewNumber=0;
switch(intOperator){
case 0:
Operator.setText("+");
intCorrectAnswer = intLeft + intRight;
break;
case 1:
Operator.setText("-");
intCorrectAnswer=intLeft-intRight;
break;
case 2:
Operator.setText("X");
intCorrectAnswer=intLeft*intRight;
break;
}
strCorrectAnswer = String.valueOf(intCorrectAnswer);
GetNumbers(strCorrectAnswer);
problemtimer.start();
}
private void GetNumbers(String strNumber){
LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout1);
int L = strNumber.length();
String c;
for (int i = 0;i<L-1;i++){
c=strNumber.substring(i,i+1);
switch (c){
case "1":
imageView[imageViewNumber].setBackgroundResource(R.drawable.one);
ll.addView(imageView[imageViewNumber]);
break;
case "2":
imageView[imageViewNumber].setBackgroundResource(R.drawable.two);
ll.addView(imageView[imageViewNumber]);
break;
case "3":
imageView[imageViewNumber].setBackgroundResource(R.drawable.three);
ll.addView(imageView[imageViewNumber]);
break;
case "4":
imageView[imageViewNumber].setBackgroundResource(R.drawable.four);
ll.addView(imageView[imageViewNumber]);
break;
case "5":
imageView[imageViewNumber].setBackgroundResource(R.drawable.five);
ll.addView(imageView[imageViewNumber]);
break;
case "6":
imageView[imageViewNumber].setBackgroundResource(R.drawable.six);
ll.addView(imageView[imageViewNumber]);
break;
case "7":
imageView[imageViewNumber].setBackgroundResource(R.drawable.seven);
ll.addView(imageView[imageViewNumber]);
break;
case "8":
imageView[imageViewNumber].setBackgroundResource(R.drawable.eight);
ll.addView(imageView[imageViewNumber]);
break;
case "9":
imageView[imageViewNumber].setBackgroundResource(R.drawable.nine);
ll.addView(imageView[imageViewNumber]);
break;
case "0":
imageView[imageViewNumber].setBackgroundResource(R.drawable.zero);
ll.addView(imageView[imageViewNumber]);
break;
}
}
}
错误发生在
之类的行上的Switch语句中imageView[imageViewNumber].setBackgroundResource(R.drawable.zero);
我做错了什么?
这是我的布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MultipleChoice"
android:minWidth="30dp"
android:focusableInTouchMode="false">
<TextView android:text="@string/LeftText" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Left"
android:layout_below="@+id/Timeleft"
android:layout_toLeftOf="@+id/Operator"
android:layout_toStartOf="@+id/Operator" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/OperatorText"
android:id="@+id/Operator"
android:layout_below="@+id/Timeleft"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/RightText"
android:id="@+id/Right"
android:layout_below="@+id/Timeleft"
android:layout_toRightOf="@+id/Operator"
android:layout_toEndOf="@+id/Operator" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/StartButton"
android:id="@+id/btnStart"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="btnStart_Click"
android:enabled="true"
android:focusable="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/TimeLeft"
android:id="@+id/Timeleft"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Correct: 0"
android:id="@+id/CorrectAnswers"
android:layout_alignTop="@+id/Timeleft"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/LinearContainer"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:baselineAligned="false">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/linearLayout1"
android:layout_weight="1"
android:gravity="center_horizontal">
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/linearLayout2"
android:gravity="center_horizontal">
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/linearLayout3"
android:gravity="center_horizontal">
</LinearLayout>
</LinearLayout>
</RelativeLayout>
答案 0 :(得分:0)
你的&#34; imageView&#34;中没有物体。阵列。
使用值初始化它:
for(int i = 0; i < imageView.lenght; i++) imageView[i] = new ImageView(context);
所以你可以正确使用。