Question

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

No correct solution

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top