Как настроить пользовательскую кнопку в диалоге? - PullRequest
2 голосов
/ 24 марта 2011

здесь у меня есть пользовательский диалог с фоновым изображением 2 внутри кнопки.проблема в том, что, когда я пытаюсь установить прослушиватель onclick для этих кнопок, программа вернет NullPointerException.Я не знаю, почему это случилось.как назначить операцию кнопке внутри диалога ??

меню паузы 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 pauseMenu = new Dialog(this, R.style.NewDialog);
    pauseMenu.setContentView(R.layout.pause_menu);

  ImageButton quit = (ImageButton)findViewById(R.id.btn_pause_quit);
  quit.setOnClickListener(
          new OnClickListener() {

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

код возвращает ошибку в строке

quit.setOnClickListener();

Ответы [ 3 ]

7 голосов
/ 24 марта 2011

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

должно быть

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

Это происходит потому, что findViewById вызывается длядеятельность, и она не имеет кнопки btn_pause_quit в своем макете.Но ваш диалог имеет.

4 голосов
/ 24 марта 2011

U может использовать этот пользовательский диалог и 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();
}

}
0 голосов
/ 24 марта 2011

Я думаю, что ваш onClickListener должен быть DialogInterface.OnClickListener

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...