自定义AlertDialog.Builder添加自定义按钮事件单击

时间:2012-02-28 20:14:38

标签: android dialog

在我的自定义“警报对话框”中,我遇到了在xml上声明的按钮上设置onclick的困境。

这段代码给了我一个空指针异常,确认在实现后有一个实际的alertDialog

close.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                alertDialog.dismiss();
            }
        });

在ListView上单击项目后的LayoutInflates。

我的问题是“如何将自定义xml onClickListeners设置为自定义AlertDialog上的按钮?”

    LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.custom_dialog, (ViewGroup) findViewById(R.id.layout_root));

    TextView text = (TextView) layout.findViewById(R.id.data1);
    ImageView image = (ImageView) layout.findViewById(R.id.dialog_image);
    image.setImageResource(R.drawable.temp_book);
    Button summary, confirm, close;
    summary = (Button)findViewById(R.id.Summary);
    confirm = (Button)findViewById(R.id.confirmCD);
    close = (Button)findViewById(R.id.closeCD);
    builder = new AlertDialog.Builder(this);
    builder.setView(layout);

    close.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            alertDialog.dismiss();
        }
    });

    alertDialog = builder.create();

    alertDialog.show();

记录错误

02-28 15:13:28.385: E/AndroidRuntime(6763): FATAL EXCEPTION: main

02-28 15:13:28.385: E/AndroidRuntime(6763): java.lang.NullPointerException

02-28 15:13:28.385: E/AndroidRuntime(6763):     at test.list.A_Test4Activity.onListItemClick(A_Test4Activity.java:532)

02-28 15:13:28.385: E/AndroidRuntime(6763):     at android.app.ListActivity$2.onItemClick(ListActivity.java:319)

02-28 15:13:28.385: E/AndroidRuntime(6763):     at android.widget.AdapterView.performItemClick(AdapterView.java:292)

02-28 15:13:28.385: E/AndroidRuntime(6763):     at android.widget.AbsListView.performItemClick(AbsListView.java:1058)

02-28 15:13:28.385: E/AndroidRuntime(6763):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)

02-28 15:13:28.385: E/AndroidRuntime(6763):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)

02-28 15:13:28.385: E/AndroidRuntime(6763):     at android.widget.AbsListView$1.run(AbsListView.java:3168)

02-28 15:13:28.385: E/AndroidRuntime(6763):     at android.os.Handler.handleCallback(Handler.java:605)

02-28 15:13:28.385: E/AndroidRuntime(6763):     at android.os.Handler.dispatchMessage(Handler.java:92)

02-28 15:13:28.385: E/AndroidRuntime(6763):     at android.os.Looper.loop(Looper.java:137)

02-28 15:13:28.385: E/AndroidRuntime(6763):     at android.app.ActivityThread.main(ActivityThread.java:4340)

02-28 15:13:28.385: E/AndroidRuntime(6763):     at java.lang.reflect.Method.invokeNative(Native Method)

02-28 15:13:28.385: E/AndroidRuntime(6763):     at java.lang.reflect.Method.invoke(Method.java:511)

02-28 15:13:28.385: E/AndroidRuntime(6763):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)

02-28 15:13:28.385: E/AndroidRuntime(6763):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)

02-28 15:13:28.385: E/AndroidRuntime(6763):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:3)

如果没有看到任何带有异常的堆栈跟踪,我会说你看不到应该在哪里寻找Button 关闭。如果您将AlertDialog的布局设置为自动膨胀的自定义布局或构建代码View,那么您必须在findViewById(R.id.Id_of_button)上设置为View AlertDialog内容。

修改

关闭 Buttonnull(并在调用方法时抛出NullPointerException),因为您必须搜索{{1}在以前膨胀并用作Button内容的布局中:

AlertDialog

不要忘记摘要确认 close = (Button) layout.findViewById(R.id.closeCD); ,如果它们也处于虚增布局中。

答案 1 :(得分:0)

实际上,alertDialog为空。应该在OnClickListener之前创建它:

alertDialog = builder.create();

close.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        // TODO Auto-generated method stub
        alertDialog.dismiss();
    }
});

alertDialog.show();