0

我用于创建自定义对话框的这部分代码..

final Dialog dialog = new Dialog(preferences.this);
dialog.setContentView(R.layout.customdialog);
dialog.setTitle("Title...");

TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Bonboniziraj se i ti :)");

Button button = (Button) dialog.findViewById(R.id.dialogButtonOK);
button.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
   finish();
   }
 });

dialog.show();
}
});

我收到 SetOnClickListener 错误!!

4

2 回答 2

5

1.试试这个

       final Dialog dialog = new Dialog(preferences.this);
              LayoutInflater inflater = getLayoutInflater();
           View layout = inflater.inflate(R.layout.customdialog, null);
        dialog.setContentView(layout);

        dialog.setTitle("Title...");

  TextView text = (TextView) dialog.findViewById(R.id.text);
  text.setText("Bonboniziraj se i ti :)");

  Button button = (Button) dialog.findViewById(R.id.dialogButtonOK);
button.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
   finish();
   }
 });

  dialog.show();
  }
  });
于 2012-09-26T11:57:14.217 回答
0

是空指针异常吗?您确定在 customdialog 布局中有带有 id=dialogBu​​ttonOK 的按钮吗?核实。

于 2012-09-26T11:44:27.450 回答