我是android的新手,我正在使用另一个堆栈溢出线程中的代码创建一个小应用程序,所以我可以使用下一个上一个重启按钮点击一些图像。
Changing ImageView on button click
不幸的是,应用程序在启动时不断崩溃。
我认为它是由java.lang.NullPointerException引起的
我用Google搜索并用谷歌搜索,但我也不会修理它。
这是使用
的代码package com.example.android.boobies;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity implements View.OnClickListener {
ImageView image = (ImageView) findViewById(R.id.boobiesOne);
Button next;
int a = 0;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button restart = (Button) findViewById(R.id.restart);
restart.setOnClickListener(this);
Button previous = (Button) findViewById(R.id.previous);
previous.setOnClickListener(this);
next = (Button) findViewById(R.id.next);
next.setOnClickListener(this);
}
@Override
public void onClick(View view)
{
switch (view.getId())
{
case R.id.restart:
image.setImageResource(R.drawable.image1);
a = 0;
break;
case R.id.next:
if (a == 0)
{
image.setImageResource(R.drawable.image2);
a = 1;
}
else if (a == 1)
{
image.setImageResource(R.drawable.image3);
a = 2;
}
else if (a == 2)
{
image.setImageResource(R.drawable.image4);
a = 3;
}
else if (a == 3)
{
image.setImageResource(R.drawable.image5);
a = 4;
}
else if (a == 4)
{
image.setImageResource(R.drawable.image6);
a = 5;
}
else if (a == 5)
{
image.setImageResource(R.drawable.image7);
a = 6;
}
else if (a == 6)
{
image.setImageResource(R.drawable.image8);
a = 7;
}
else if (a == 7)
{
image.setImageResource(R.drawable.image9);
image.setClickable(false);
}
break;
case R.id.previous:
a--;
next.performClick();
break;
}
}
}
<LinearLayout
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:background="#000000"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:id="@+id/boobiesOne"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scaleType="centerCrop"
android:src="@drawable/image10" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="previous" />
<Button
android:id="@+id/restart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="restart" />
<Button
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|left"
android:layout_weight="1"
android:onClick="onClick"
android:text="next" />
</LinearLayout>
</LinearLayout>