Answer the question
In order to leave comments, you need to log in
Android app crashes. Where can be an error?
Hello. Started learning how to develop for android and ran into a small problem.
I did a task in which, when you alternately press different buttons, pictures change. When you click on the second and third buttons, everything is fine, the picture above the buttons changes, but once you click on the first one, the application crashes. I can't figure out where I screwed up.
Rendering proceeds normally, without errors.
package ru.paint_master.p0011;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
Button button1;
Button button2;
Button button3;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView) findViewById(R.id.img1);
button1=(Button) findViewById(R.id.button1);
button2=(Button) findViewById(R.id.button2);
button3=(Button) findViewById(R.id.button3);
View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
switch (view.getId() ){
case R.id.button1:
imageView.setImageResource(R.drawable.mail);
break;
case R.id.button2:
imageView.setImageResource(R.drawable.zero);
break;
case R.id.button3:
imageView.setImageResource(R.drawable.logo);
break;
}
}
};
button1.setOnClickListener(onClickListener);
button2.setOnClickListener(onClickListener);
button3.setOnClickListener(onClickListener);
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/hor_margin"
android:layout_marginBottom="@dimen/vert_margin"
tools:context="ru.paint_master.p0011.MainActivity">
<LinearLayout
android:layout_width="253dp"
android:layout_height="386dp"
android:orientation="vertical"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">
<ImageView
android:id="@+id/img1"
android:layout_width="match_parent"
android:layout_height="120dp"
android:contentDescription="@string/andr"
app:srcCompat="@mipmap/ic_launcher_round" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/bt1"
android:textColor="#006600"/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/bt2"
android:textColor="#006600"/>
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/bt3"
android:textColor="#006600"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question