0
            public void name() {
           AlertDialog.Builder builder = new AlertDialog.Builder(this);
           LayoutInflater inflater = this.getLayoutInflater();
           builder.setView(inflater.inflate(R.layout.unit null));
                    builder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getApplicationContext(), "yes", Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }
        }); 

        final  AlertDialog dialog = builder.create();

        Button dialogButton = (Button) dialog.findViewById(R.id.i_pharmacy);
        // if button is clicked, close the custom dialog
        dialogButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            //   dialog.dismiss();
            }
        });

        dialog.show(); 


    }

当我运行上面的代码时,我得到了 NULL 点异常。使用上面的代码在对话框上显示一个按钮。单击对话框按钮后,应该会发生一些事件。

4

2 回答 2

1

尝试这样的事情:

            // custom dialog
            final Dialog dialog = new Dialog(context);
            dialog.setContentView(R.layout.custom);
            dialog.setTitle("Title...");

            // set the custom dialog components - text, image and button
            TextView text = (TextView) dialog.findViewById(R.id.text);
            text.setText("Android custom dialog example!");
            ImageView image = (ImageView) dialog.findViewById(R.id.image);
            image.setImageResource(R.drawable.ic_launcher);

            Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
            // if button is clicked, close the custom dialog
            dialogButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });

            dialog.show();

布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dp" />

    <TextView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#FFF" 
        android:layout_toRightOf="@+id/image"/>/>

     <Button
        android:id="@+id/dialogButtonOK"
        android:layout_width="100px"
        android:layout_height="wrap_content"
        android:text=" Ok "
        android:layout_marginTop="5dp"
        android:layout_marginRight="5dp"
        android:layout_below="@+id/image"
        />

</RelativeLayout>
于 2013-08-03T09:59:02.877 回答
0
builder.setView(inflater.inflate(R.layout.unit null));

在这部分。你不是在null之前缺少一个逗号吗?

于 2013-08-03T11:06:29.987 回答