I tried to develop a small game: clicker . The compilation is successful, the installation on the device is also successful. However, after clicking on the button, the application crashes. Please help. Here is the whole code:

package com.arslee07.eliteclicker; import android.annotation.SuppressLint; 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 { public int clicks = 0; Button btnClick; TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnClick = (Button) findViewById(R.id.btnClick); View.OnClickListener oclBtnClick = new View.OnClickListener() { @SuppressLint("SetTextI18n") @Override public void onClick(View v) { clicks++; //clicks = Integer.parseInt(textView.getText().toString()); textView.setText("ELITE CLICKS: "+ clicks); } }; btnClick.setOnClickListener(oclBtnClick); } } 
  • If the error is not related to compilation, then you will not see it when compiling. See application stack trace in logcat - gil9red
  • one
    To see the errors, you need to learn something about logcat and stacktrace. There is no such thing that the application does not work and there is no error at the same time. In particular, you now get the Null Pointer Exeption on the line with textView.setText() , since the object has not been initialized. The fact that you do not see an error or do not know where to look does not mean that it does not exist - pavlofff

1 answer 1

Try as you tied

 btnClick = (Button) findViewById(R.id.btnClick); 

tie and

 textView = (TextView) findViewById(R.id.textview);