D
D
Denis Karakchiev2015-08-31 20:43:32
Android
Denis Karakchiev, 2015-08-31 20:43:32

Assigning a single clickListener to multiple buttons and a TextView. Why isn't it alive?

public class MainActivity extends Activity {

    TextView textView1;
    TextView textView2;
    TextView textView3;
    TextView textView4;

    Button button1;
    Button button2;
    Button button3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        View.OnClickListener myClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch(view.getId()){
                    case R.id.button1:
                        textView1.setText(R.string.textOne);
                        break;
                    case R.id.button2:
                        textView2.setText(R.string.textTwo);
                        break;
                    case R.id.button3:
                        textView3.setText(R.string.textThre);
                        textView1.setText(R.string.textOneClick3);
                        textView4.setText(R.string.textClickable);
                        break;
                    case R.id.textView4:
                        textView4.setText(R.string.textClickable2);
                        break;
                }

            }
        };

        button1.setOnClickListener(myClickListener);
        button2.setOnClickListener(myClickListener);
        button3.setOnClickListener(myClickListener);
        textView4.setOnClickListener(myClickListener);

Immediately "Unfortunately, %appName% was stopped"

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
belozerow, 2015-08-31
@Satori_Kanzo

button1 = findViewById(R.id.button1) forgot

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question