-1

It's just a simple text with two buttons that creates toasts and change the text. I'm very new to this AS so I really don't know how to even attempt to fix this.. Any help is very appreciated...

MainActivity.java:

package com.example.david.davidisawesome;

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

public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.d(TAG, "onCreate: Started.");

    final TextView firstText = (TextView) findViewById(R.id.firstText);
    Button firstButton = (Button) findViewById(R.id.firstBtn);
    Button secondButton = (Button) findViewById(R.id.secondBtn);

    firstButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.d(TAG, "onClick: First Button Clicked.");
            toastMessage("You Clicked the first button");
            firstText.setText("Nice Job.");
        }
    });

    secondButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.d(TAG, "onClick: Second Button Clicked.");
            toastMessage("You Clicked the second button");
            firstText.setText("Good Effort.");
        }
    });

}

private void toastMessage(String message){
    Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
}
}

In picture:

enter image description here

activity_main.xml:

enter image description here enter image description here

Error: enter image description here

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Ke Ke
  • 63
  • 1
  • 7
  • I create an app like yours and have no problems to execute. Maybe you could clean your project and try again.. – Rafaela Lourenço Aug 08 '18 at 19:40
  • @Rafaela Lourenço I've already tried to clean the project like hundred times, but no change :((( – Ke Ke Aug 08 '18 at 19:44
  • remove the styles and try it –  Aug 08 '18 at 19:50
  • @mTak still no change with the removal of the styles.. ughhhh – Ke Ke Aug 08 '18 at 19:55
  • Looks like IDE issue, do **invalidate cache and restart**. I can also see in the crash log NPE encountered on line number 33 but in mainactivity cause of crash is line number 32. – Krishna Sharma Aug 08 '18 at 20:12
  • @KrishnaSharma thanks for the reply, the restart did not work so I just deleted the program and downloading it again.. hope it works :).. – Ke Ke Aug 08 '18 at 20:15
  • @KeKe one more try: remove the listener and set it by xml `android:onClick="doClick"` and in the activity: `public void doClick(View v) { Log.d(TAG, "onClick: Second Button Clicked."); toastMessage("You Clicked the second button"); firstText.setText("Good Effort."); }` –  Aug 08 '18 at 21:07

1 Answers1

0

It is not finding your xml elements. The error shows that you have a null exception on setting the onclick interface to the button. So start by confirming that you are inflating the proper xml and finding the proper id.

More specifically your secondBtn is not found.

Sam
  • 5,342
  • 1
  • 23
  • 39
  • how is secondBtn not found? Can you elaborate more on it..? – Ke Ke Aug 08 '18 at 19:54
  • Sure happy to help. Post your "real xml" and i'll take a look. As a quick experiment though you could simply give it a different name, maybe you are having a caching issue on your build somewhere as the screenshot you provided looks fine, but would need to see the actual xml to know for sure. – Sam Aug 09 '18 at 04:18
  • Hey @Sam thanks for the offer to help me but I just reinstalled the software and its working perfectly fine :) I am very happy. Have a good night! – Ke Ke Aug 09 '18 at 04:34
  • ok great, must of been a caching issue of some sort then. Glad you got it resolved. – Sam Aug 09 '18 at 16:49