1

创建构建器的代码:

    builder = new AlertDialog.Builder(this);
    builder.setPositiveButton("connect",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int     id) {
                    devices.get(currentPos).setConnected(true);
                }
            });
    builder.setNegativeButton("dismiss",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // User cancelled the dialog
                }
            });
    builder.setView(getLayoutInflater().inflate(
            (R.layout.activity_device_details), null));
    builder.setTitle("more information");

请注意:builder.setView() 到 R.layout.activity_device_details,其中有一些我想在使用以下代码创建对话框时填充的 TextView:

    BPDevice dev = new BPDevice();
    dev = devices.get(position);

    AlertDialog dialog = builder.create();

    ((TextView) dialog.findViewById(R.id.name)).setText(dev.getName());

    dialog.show();

由于这一行,我得到了一个 nullpointerException: ((TextView) dialog.findViewById(R.id.name)).setText(dev.getName());

¿ 如何正确填充 TextViews?

4

1 回答 1

1

首先膨胀具有文本视图的根布局。然后你的孩子意见。然后设置数据并将根设置为警报对话框的视图。

View root = getLayoutInflater().inflate(
        (R.layout.activity_device_details),null);
 
TextView textView =(TextView)root.findViewById(R.id.your_textview_layout_name);
textView.setText("Your data");
builder.setView(root);
于 2013-10-08T10:19:23.767 回答