[Solved] Android, NullPointerException


You shouldn’t be setting the onClickListener for your button within the button itself, instead it should be set in the Activity that makes use of both of your buttons. Likewise, findViewById() in your example can’t find the TextView as it’s not within the same scope as your button. In absence of code from your activity, here’s an example of what you should do:

public void MyActivity extends Activity {
     TextView textView;
     MyButton myButton;

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

         textView = (TextView) findViewById(R.id.textBtn);
         myButton = (MyButton) findViewById(R.id.myBtn);

         myButton.setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View v) {
                  textView.setText("It Works!");
              }
         }
     }    

3

solved Android, NullPointerException