如何在对话框中设置自定义按钮? [英] How to configure custom button in dialog?
问题描述
在这里我有一个背景2 ImageButton的里面一个自定义对话框。 问题是,当我尝试设置的onclick监听到该按钮,程序将返回NullPointerException异常。我不知道这是为什么发生。如何分配操作按钮对话框反正里面??
暂停菜单的xml:
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android机器人:layout_height =WRAP_CONTENT机器人:ID =@ + ID / linearLayout1机器人:layout_width =WRAP_CONTENT机器人:后台=@可绘制/ pause_menu_cropped机器人:layout_gravity =中心安卓重力=中心| center_horizontal>
< TableLayout机器人:layout_width =WRAP_CONTENT机器人:ID =@ + ID / tableLayout1机器人:layout_height =WRAP_CONTENT>
< ImageButton的机器人:SRC =@可绘制/ pause_button_option机器人:layout_width =WRAP_CONTENT机器人:背景=@可绘制/ pause_button_option机器人:layout_height =WRAP_CONTENT机器人:ID =@ + ID / btn_pause_option> < / ImageButton的>
< ImageButton的机器人:SRC =@可绘制/ pause_button_quit机器人:layout_width =WRAP_CONTENT机器人:背景=@可绘制/ pause_button_quit机器人:layout_height =WRAP_CONTENT机器人:ID =@ + ID / btn_pause_quit> < / ImageButton的>
< / TableLayout>
< / LinearLayout中>
对话框code
对话pauseMenu =新的对话框(这一点,R.style.NewDialog);
pauseMenu.setContentView(R.layout.pause_menu);
的ImageButton退出=(的ImageButton)findViewById(R.id.btn_pause_quit);
quit.setOnClickListener(
新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
TestActivity.this.finish();
}
});
返回pauseMenu;
在code是在返回路线错误
quit.setOnClickListener();
的ImageButton退出= (的ImageButton)findViewById(R.id.btn_pause_quit);
应
的ImageButton退出=(的ImageButton)pauseMenu.findViewById(R.id.btn_pause_quit);
这是因为 findViewById
被调用的活动,并且它不具有 btn_pause_quit
按钮,在它的布局。但是,你的对话框了。
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.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.
这篇关于如何在对话框中设置自定义按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!