2012-02-02 111 views
1

我试图从警报对话框中的按钮启动自定义对话框。用户在打开redeemAlertDialog的主UI中按下一个按钮,此对话框会询问用户是否确定要继续此操作。如果他们点击“是”,那么我想打开我的自定义对话框。但是,启动我的自定义对话框会导致应用程序崩溃。 Logcat告诉我,我有一个空指针错误的行* text.setText(“Blah Blah”/ merchantName /); *,但如果我注释掉这一行,我得到了同样的错误button.setOnClickListener (新的OnClickListener(){ 如果我注释掉了这两行,那么它就可以工作了。在深入挖掘后,我认为我的问题与上下文有关,我将自定义对话框与创建它时关联起来,牛逼能解决它。如果有人能指出我要去哪里错了,我将不胜感激。 我的代码如下。从警报对话框启动自定义对话框 - NullPointer错误

解决 在我的onCreate方法的M改变mContext的我的定义Context = getApplicationContext();到mContext = this; 由于某些原因couponDialog = new Dialog(mContext);不喜欢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(); 
} 
+0

findViewById(...)返回null,这是造成NullPointerException异常。你可以发布更多来自你调用这个对话框的代码吗? – kosa 2012-02-02 16:59:16

+0

我正在从redeemAlertDialog调用couponDialog。 你可以在redeemAlertDialog.setPositiveButton的末尾看到couponDialog.show() – Roardog 2012-02-02 17:41:37

回答

3

相反的:

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

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

使用

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

希望这个作品

+0

谢谢Chris,但它没有工作我害怕。它确实将我得到的错误更改为“无法添加窗口 - 令牌null不适用于应用程序” – Roardog 2012-02-02 17:40:36

+0

couponDialog = new Dialog(mContext); mContext在哪里,它对你有什么影响,应该使用这个或基本活动 – Chris 2012-02-02 20:04:22

+0

解决它。出于某种原因,getApplicationContext()没有返回新的Dialog()可以使用的东西。在我的onCreate()方法中,我改变了mContext = getApplicationContext();到mContext = this;现在它可以工作 – Roardog 2012-02-03 15:13:35