1

I'm trying to learn custom dialogs. I made one with a button in it and it comes up fine and I can hit breakpoints in the constructor and onCreate method, but when I click the button it crashes without ever getting to the button handler.

The dialog layout XML (my_dialog_layout.xml) is:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">  
  <Button
     android:id="@+id/AButton"
     android:layout_width="100px"
     android:layout_height="wrap_content"
     android:layout_alignParentRight="true"
     android:layout_marginLeft="10px"
     android:onClick="AButtonHandler"
     android:text="Click Me"
  /> 
  <TextView android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_toLeftOf="@id/AButton"
    android:text="Click this button: " 
    /> 
  />

... and the Dialog's java file is:

import android.os.Bundle;
import android.widget.Toast;
import android.view.View;
import android.content.Context; 
import android.app.Dialog;

public class MyDialog extends Dialog {

    public MyDialog(Context context) {
        super(context);
        setContentView(R.layout.my_dialog_layout);
   }

    public void AButtonHandler(View target) {
        int i = 0;   // just a placeholder to set a breakpoint at
        i++;         //   "                 "
//       Toast.makeText(this, "in AButtonHandler", Toast.LENGTH_LONG).show();
        MyDialog.this.dismiss();
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
  //      Toast.makeText(this, "Dialog onCreate", Toast.LENGTH_LONG).show();
    }   
}

When I click AButton it crashes in the debugger before getting to my breakpoint in AButtonHandler with " Thread [<1> main] (Suspended (exception IllegalStateException))
View$1.onClick(View) line: 2059 Button(View).performClick() line: 2408
..."

Also notice the commented-out Toast's. I wanted to put Toasts in but thye compiler gives me: The method makeText(Context, CharSequence, int) in the type Toast is not applicable for the arguments (MyDialog, String, int) What am I doing wrong?

Thanks in advance!

Peter Nelson
  • 5,759
  • 17
  • 41
  • 61

2 Answers2

3

Second problem: A Dialog is not a Context. It has a Context. Use Toast.makeText(getContext(), ...).

First problem: Same thing. The method needs to be in the activity, not the dialog. (I should mention that I never tried onClick in a dialog. You may need to use a traditional OnClickListener.)

Side note: Function names should start with lower case.

EboMike
  • 72,990
  • 14
  • 152
  • 157
  • Thanks for the tip on the Toasts! (it worked) But I don't understand the button handler part. I didn't know in Android the button handlers for dialogs are not in the dialog class. So I moved AButtonHandler into the Activity and I got the same crash. In View$1 (which I assume is the AButton because idText says "with id'AButton'") the error is NoSuchMethodEcxeption. Does this mean I can't do android:onClick in a dialog layout?? – Peter Nelson Nov 30 '10 at 21:44
  • Never tried it, but it sounds like having it in the dialog actually would have worked (since you got a different exception before). However, the docs for android:onClick specifically say "a method of your context" - the Dialog is not a context. Personally, I always use `OnClickListener` for dialogs. – EboMike Nov 30 '10 at 21:59
0

After doing some more searching I discovered this:

Using onClick attribute in layout xml causes a NoSuchMethodException in Android dialogs

...exactly the same symptoms as I had! ... so apparently what I'm trying to do won't work. It's too bad - android:onClick - is very convenient.

I'll give eBoMike the answer credit on this because he straightened me out on the Toast and he at least alluded to the possibility that android:onClick was questionable.

Community
  • 1
  • 1
Peter Nelson
  • 5,759
  • 17
  • 41
  • 61