0

This part of my code for creating custom dialog..

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

I get SetOnClickListener error!!

Chirag
  • 56,621
  • 29
  • 151
  • 198
Goran
  • 1,239
  • 4
  • 23
  • 36
  • 1
    Paste the stack trace please. What are your errors? – Richard Sep 26 '12 at 11:42
  • What's the error? If you hover your mouse over the red x it should give you more details on the error. – Billdr Sep 26 '12 at 11:42
  • Error is -> The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (new OnClickListener(){}) – Goran Sep 26 '12 at 12:23

2 Answers2

5

1. Try this

       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();
  }
  });
mukesh
  • 4,140
  • 5
  • 29
  • 40
  • 2
    I had this same problem and i was using `(TextView)findviewById(...)` instead of `(TextView)dialog.findviewById(...)`. Makes all the difference since you are telling the system where your resource is. – Totalys Feb 22 '14 at 17:15
0

Is it Null pointer exception? Are you sure you have button with id=dialogButtonOK in customdialog layout? Check it.

  • Yes i have this button, error i get is -> The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (new OnClickListener(){}) – Goran Sep 26 '12 at 12:24