Answer the question
In order to leave comments, you need to log in
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question