开发者

How to configure custom button in dialog?

开发者 https://www.devze.com 2023-02-19 16:06 出处:网络
here I have a custom dialog with background 2 ImageButton inside it. the problem is, when I try to set onclick listener to that buttons, the program will return NullPointerException. I don\'t know why

here I have a custom dialog with background 2 ImageButton inside it. the problem is, when I try to set onclick listener to that buttons, the program will return NullPointerException. I don't know why is this happen. how to assign operation to button inside dialog anyway??

pause menu xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:id="@+id/linearLayout1" android:layout_width="wrap_content" android:background="@drawable/pause_menu_cropped" android:layout_gravity="center" android:gravity="center|center_horizontal">
    <TableLayout android:layout_width="wrap_content" android:id="@+id/tableLayout1" android:layout_height="wrap_content">
        <ImageButton android:src="@drawable/pause_button_option" android:layout_width="wrap_content" android:background="@drawable/pause_button_option" android:layout_height="wrap_content" android:id="@+id/btn_pause_option"></ImageButton>
        <ImageButton android:src="@drawable/pause_button_quit" android:layout_width="wrap_content" android:background="@drawable/pause_button_quit" android:layout_height="wrap_content" android:id="@+id/btn_pause_quit"></ImageButton>
    </TableLayout>
</LinearLayout>

dialog code

        Dialog pauseMenu = new Dialog(this, R.style.NewDialog);
    pauseMenu.setContentView(R.layout.pause_menu);

  ImageButton quit = (ImageButton)findViewById(R.id.btn_pause_quit);
  quit.开发者_StackOverflow中文版setOnClickListener(
          new OnClickListener() {

      @Override
      public void onClick(View v) {
          TestActivity.this.finish();
      }
  });
    return pauseMenu;

the code is returning error in line

quit.setOnClickListener();


ImageButton quit = (ImageButton)findViewById(R.id.btn_pause_quit);

should be

ImageButton quit = (ImageButton)pauseMenu.findViewById(R.id.btn_pause_quit);

This happens because findViewById is invoked for the activity, and it doesn't have btn_pause_quit button in it's layout. But your dialog has.


U can use this custom dialog and onclicklistener..

public class CustomizeDialog extends Dialog implements OnClickListener {

Button okButton;

public CustomizeDialog(Context context) {
super(context);
/** 'Window.FEATURE_NO_TITLE' - Used to hide the title */
requestWindowFeature(Window.FEATURE_NO_TITLE);
/** Design the dialog in main.xml file */
setContentView(R.layout.main);
okButton = (Button) findViewById(R.id.OkButton);

okButton.setOnClickListener(this);
}


@Override

public void onClick(View v) {
/** When OK Button is clicked, dismiss the dialog */
if (v == okButton)
dismiss();
}

}


I think your onClickListener should be DialogInterface.OnClickListener

0

精彩评论

暂无评论...
验证码 换一张
取 消