R
R
Ramil Khismatullin2018-09-17 15:06:09
Java
Ramil Khismatullin, 2018-09-17 15:06:09

The application pops up with an error, what's wrong?

Everything is being built, launched, two views appear on the layout (textview and button), however, when you click on any of them, the application crashes with an error (and it’s not clear which one).

package com.example.lesson;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private TextView helloTv;
    private Button helloBtn;

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

        TextView helloTv = (TextView) findViewById(R.id.txt_hello_world);
        Button helloBtn = (Button) findViewById(R.id.btn_hello_world);

        helloTv.setOnClickListener(onClickListener);
        helloBtn.setOnClickListener(onClickListener);
    }

    private final View.OnClickListener onClickListener = new View.OnClickListener(){
        @Override
        public void onClick(View v){
            switch (v.getId()){
                case R.id.txt_hello_world:
                    helloTv.setText("Hi!");
                    break;
                case R.id.btn_hello_world:
                    helloTv.setText("Hello World");
                    break;
            }
        }
    };
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg, 2018-09-17
@ramil2321

developer.alexanderklimov.ru/android/debug/basic.php
in your case, I think the run tab will be enough. The text of the last exception will contain the reason for the error.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question