我的屏幕上有3个按钮(重新启动,上一个,下一个)和一个1 imageView。 android studio在java和xml中没有显示错误。然后为什么这个应用程序无法正常工作
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:id="@+id/i1"
android:layout_width="fill_parent"
android:layout_height="407dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:src="@drawable/i1" />
<Button android:id="@+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Prev"
android:layout_gravity="bottom"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<Button android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Res"
android:id="@+id/restart"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
这是我的java代码:
public class MainActivity extends Activity implements View.OnClickListener {
ImageView image = (ImageView) findViewById(R.id.i1);
Button next;
int a = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
next = (Button) findViewById(R.id.next);
next.setOnClickListener(this);
Button restart = (Button) findViewById(R.id.restart);
restart.setOnClickListener(this);
Button previous = (Button) findViewById(R.id.previous);
previous.setOnClickListener(this);
}
public void onClick(View view) {
switch (view.getId())
{
case R.id.restart:
image.setImageResource(R.drawable.i1);
a = 0;
break;
case R.id.next:
if (a == 0)
{
image.setImageResource(R.drawable.i2);
a = 1;
}
else if (a == 1)
{
image.setImageResource(R.drawable.i3);
a = 2;
}
else if (a == 2)
{
image.setImageResource(R.drawable.i4);
a = 3;
}
else if (a == 3)
{
image.setImageResource(R.drawable.i5);
a = 4;
}
else if (a == 4)
{
image.setImageResource(R.drawable.i6);
a = 5;
}
else if (a == 5)
{
image.setImageResource(R.drawable.i7);
a = 6;
}
else if (a == 6)
{
image.setImageResource(R.drawable.i8);
a = 7;
}
else if (a == 7)
{
image.setImageResource(R.drawable.i9);
image.setClickable(false);
}
break;
case R.id.previous:
a--;
next.performClick();
break;
}
}
}
答案 0 :(得分:2)
ImageView image = (ImageView) findViewById(R.id.i1);
应该在
之后setContentView(R.layout.activity_main);
onCreate方法中的
由于NullPointerException,应用程序崩溃。
答案 1 :(得分:2)
为什么图像是从onCreate()中初始化的?在setContentView()
之后立即在onCreate()方法中执行答案 2 :(得分:0)
Button next;
int a = 1;
ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) findViewById(R.id.i1);
next = (Button) findViewById(R.id.next);
next.setOnClickListener(this);
Button restart = (Button) findViewById(R.id.restart);
restart.setOnClickListener(this);
Button previous = (Button) findViewById(R.id.previous);
previous.setOnClickListener(this);
}