这里我有一个自定义对话框,里面有背景 2 ImageButton。
问题是,当我尝试为该按钮设置 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();
图像按钮退出 =
(ImageButton)findViewById(R.id.btn_pause_quit);
应该
ImageButton quit = (ImageButton)pauseMenu.findViewById(R.id.btn_pause_quit);
发生这种情况是因为findViewById
被该活动调用,并且它没有btn_pause_quit
其布局中的按钮。但你的对话框有。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)