2012-02-02 107 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