I am trying to change the background of imageview using the following code.
Here's my xml code.
<?xml version="1.0" encoding="utf-8"?> <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"
tools:context="com.devnami.maya.maya.MainActivity"
> <ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/forest"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:id="@+id/view1"
/> <ImageButton
android:id="@+id/button1"
android:layout_width="45dp"
android:layout_height="45dp"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:background="@drawable/flower"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
/> </RelativeLayout>
And here's my mainactivity.java code.
public class MainActivity extends AppCompatActivity {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton one = (ImageButton) this.findViewById(R.id.button1);
ImageButton one = (ImageButton) this.findViewById(R.id.button1);
one.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ImageView iv =(ImageView)this.findViewById(R.id.view1)
int i = 0;
if (i == 0) {
iv.setImageResource(R.drawable.mountain));
i++;
} else if (i == 1) {
iv.setImageResource(R.drawable.seashore3);
i++;
} else if (i == 2) {
iv.setImageResource(R.drawable.night1);
i++;
} else if (i == 3) {
iv.setImageResource(R.drawable.forest);
i = 0;
}
}
});
I execute this code there seems to be no errors during the build. When I run the app on the phone, it seems the button action leads to crash message and app closes.
Any idea how to resolve this?