1

I'm trying to launch a custom dialog box from a button press in an Alert Dialog. The user presses a button in the main UI which opens the redeemAlertDialog, this dialog asks the user if they are sure they want to continue with this action. If they click 'Yes' then I want to open my custom dialog. However, launching my custom dialog causes the app to crash. Logcat is telling me I have a null pointer error on the line *text.setText("Blah Blah"/merchantName/);*, but if I comment out this line I get the same error on the line button.setOnClickListener(new OnClickListener() { If I comment out both of these lines then it works. After digging around I think my problem is something to do with the context I am associating my custom dialog with when I am creating it but I haven't been able to fix it. If someone could point out where I'm going wrong I would appreciate it. My code is below.

SOLVED In my onCreate method changed my definition of mContext from mContext = getApplicationContext(); to mContext = this; For some reasons couponDialog = new Dialog(mContext); did not like what it was being given by getApplicationContect();

    private void redeem() {
    AlertDialog.Builder redeemAlerDialogBuilder = new AlertDialog.Builder(this);
    redeemAlerDialogBuilder.setMessage("Are you sure you want to redeem?")
           .setCancelable(false) //User must select a button, can't use the back button
           .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   //Do something to launch a redeem dialog
                   //openCouponDialog();
                   couponDialog = new Dialog(mContext);
                   couponDialog.setContentView(R.layout.redeem_layout);
                   couponDialog.setTitle("Freebie Coupon");
                   couponDialog.setCancelable(false); //User should only be able to exit dialog by clicking done

                   TextView text = (TextView) findViewById(R.id.redeemMerchantName);
                   text.setText("Blah Blah"/*merchantName*/);

                   ImageView image = (ImageView) findViewById(R.id.couponImage);
                   //Set merchant coupon image here - need to download this from server when merchant is first added

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

                   couponDialog.show();
               }
           })
           .setNegativeButton("No", new DialogInterface.OnClickListener() {             
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel(); //Cancel redeem                
            }
        });
    redeemAlertDialog = redeemAlerDialogBuilder.create();
    redeemAlertDialog.show();
}
2
  • findViewById(...) returning null which is causing nullpointerexception. can you post more code from where you are invoking this dialog? Commented Feb 2, 2012 at 16:59
  • I am invoking couponDialog from redeemAlertDialog. You can see couponDialog.show() at the end of redeemAlertDialog.setPositiveButton Commented Feb 2, 2012 at 17:41

1 Answer 1

3

Instead of :

Button button = (Button) findViewById(R.id.redeemDialogCloseButton);

TextView text = (TextView) findViewById(R.id.redeemMerchantName);

use

Button button = (Button) couponDialog.findViewById(R.id.redeemDialogCloseButton);
TextView text = (TextView) couponDialog.findViewById(R.id.redeemMerchantName);

Hope this works

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Chris but it didn't work I'm afraid. It did change the error I got though to "Unable to add window -- token null is not for an application"
couponDialog = new Dialog(mContext); Where is mContext and what is it reffering to you should either use this or the base activity
Solved it. For some reason getApplicationContext() wasn't returning something that new Dialog() could use. In my onCreate() method I changed mContext = getApplicationContext(); to mContext = this; and now it works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.