【发布时间】:2011-09-30 12:00:47
【问题描述】:
我已经定义了以下自定义对话框视图:
public class MyDialog extends Dialog {
public MyDialog(Context context) {
super(context);
}
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.my_dialog);
getWindow().setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
}
}
对话框使用以下布局,其中仅包含一个“关闭我”按钮:
<?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"
android:layout_gravity="center_horizontal" >
<Button
android:id="@+id/dismiss_btn"
android:layout_width="100dip"
android:layout_height="30dip"
android:layout_centerHorizontal="true"
android:text="Dismiss me"
android:textSize="8dip"
android:textColor="#ffffff" />
</RelativeLayout>
我的MainActivity 显示一个按钮triggerDialogBtn,按下该按钮将显示我的对话框。我还为对话框上的按钮 dismiss_btn 定义了一个处理方法,该方法旨在在按下时关闭我的对话框。
public class MainActivity extends Activity{
private Button triggerDialogBtn;
private MyDialog myDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//initialize my dialog
myDialog = new MyDialog(this);
//the button which will trigger the dialog to pop up
Button triggerDialogBtn = (Button)findViewById(R.id.trigger_btn);
triggerDialogBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
myDialog.show(); //my dialog pop up
}
});
// I would like to dismiss my dialog when the "Dismiss me" button on the dialog is pressed
/**** BUT I GOT NULL POINTER EXCEPTION HERE ******/
//This is the "Dismiss me" button defined on dialog layout
Button dismissMeBtn = (Button)findViewById(R.id.dismiss_btn);
dismissMeBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
myDialog.dismiss();
}
});
}
}
main.xml的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- The trigger button which will pop up my dialog when pressed -->
<Button
android:id="@+id/trigger_btn"
android:layout_width="80dip"
android:layout_height="20dip"
android:text="Trigger dialog" />
</LinearLayout>
问题是,如果我在MainActivity 内定义dismiss_btn(位于我的对话框中)的点击处理程序,则在按下dismiss_btn 时我无法关闭对话框;它总是抛出NullPointerException。
那么,当我按下对话框上的按钮时,我可以在哪里以及如何定义处理程序以关闭我的对话框?
更新
我还尝试将dismiss_btn 处理程序放在我的自定义对话框视图类中,如下所示:
public class MyDialog extends Dialog {
public MyDialog(Context context) {
super(context);
}
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.my_dialog);
Button dismissMeBtn = (Button)findViewById(R.id.dismiss_btn);
//ERROR: The method setOnClickListener(View.OnClickListener) in the
// type View is not applicable
// for the arguments (new DialogInterface.OnClickListener(){})
dismissMeBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
???.dismiss(); // What should replace "???"
}
});
getWindow().setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
}
}
我收到指示的错误,不知道用什么替换问号。
【问题讨论】:
标签: android android-layout android-emulator android-widget android-manifest