Reputation: 348
I have 3 buttons on my screen (Restart, Previous, Next) and a 1 imageView. no error show by android studio in java and xml. then why this app not working
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>
This is my java code :
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;
}
}
}
Upvotes: 0
Views: 65
Reputation:
Why the image is initialized out of the onCreate()? do it in onCreate() method right after setContentView()
Upvotes: 2
Reputation: 8149
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);
}
Upvotes: 0
Reputation: 326
ImageView image = (ImageView) findViewById(R.id.i1);
should come after
setContentView(R.layout.activity_main);
in onCreate method
App is crashing because of NullPointerException.
Upvotes: 2