自定义对话框中的SetOnClickListener错误

时间:2012-09-26 11:38:40

标签: android customdialog

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

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错误!!

2 个答案:

答案 0 :(得分:4)

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();
  }
  });

答案 1 :(得分:0)

Null指针异常吗?你确定你在customdialog布局中有id = dialogBu​​ttonOK的按钮吗?检查一下。