-1

I'm creating a new Android application in which i'm using two dialog boxes that contain two buttons each. At first, I had no problems with the buttons included in the first box, but with the second, I had an annoying one: Every time I implement the method public void onClick(View v) the program compiles just fine, but when I launch the application a message appear: "Unfortunately app_name has stopped".

I thought that there was a problem with the implementation inside that method, but after testing it, I've discovered that if I change the id of the button into another one that isn't related to the layout of the second dialog box, the program works just fine.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
ta7foun
  • 63
  • 7
  • Can you show the exception in the logcat? Also, show the code for the button click handling. – Al Lelopath Jul 01 '15 at 12:57
  • the button click handling: nop.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { d_box.dismiss(); Toast.makeText(getApplicationContext(), "person has not been deleted", Toast.LENGTH_SHORT).show(); } }); the exception: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference – ta7foun Jul 01 '15 at 13:01
  • Add this to the original question – Al Lelopath Jul 01 '15 at 13:17

1 Answers1

0

You can't set component of view that isn't setContentView of the current activity

Button nop = (Button) findViewById(R.id.cancel5);

nop = null, in your case.

For your requirements, you need to create a custom dialog, i think. See the aswer of this post :

How to create a Custom Dialog box in android?

Hope it will help you.

Update from 07/26/2015

I think you should use this : Button btn = (Button) d.findViewById(R.id.xxxxx);

d correspond to the dialog boxes.

Community
  • 1
  • 1
  • sure i did: Button nop=(Button)findViewById(R.id.cancel5); If i replace the id "cancel5" by another button id that isn't related to the dialog box layout, everything works just fine. – ta7foun Jul 01 '15 at 13:10
  • Have you used AlertDialog or a things like this ? because if so i'm not sur you can use button. –  Jul 01 '15 at 13:14
  • No i didn't use an AlertDialog, and the problem is that i did almost the same things with the first dialog box(the two buttons worked just fine with it), but with the second one it didn't. – ta7foun Jul 01 '15 at 13:17
  • Same problem occured. – ta7foun Jul 01 '15 at 13:35
  • ok. Can you show the piece of code where the error is present ? –  Jul 01 '15 at 13:49
  • final Dialog d_box=new Dialog(this); d_box.setContentView(R.layout.effacer); d_box.setTitle("delete a person"); Button nop=(Button)findViewById(R.id.cancel5); nop.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getApplicationContext(), "person has not been deleted", Toast.LENGTH_SHORT).show(); } }); – ta7foun Jul 01 '15 at 14:12