2012-02-02 9 views
1

アラートダイアログのボタンプレスからカスタムダイアログボックスを起動しようとしています。ユーザーはredeemAlertDialogを開くメインUIのボタンを押します。このダイアログでは、このアクションを続行するかどうかをユーザーに確認します。 [はい]をクリックすると、カスタムダイアログが開きます。しかし、私のカスタムダイアログを起動すると、アプリケーションがクラッシュします。 Logcatは、私はライン* text.setText( "Blah Blah"/merchantName /)のnullポインタエラーを持っていると私に言っています*しかし、私はこの行をコメントアウトすると、私は同じエラーを得るbutton.setOnClickListener (新しいOnClickListener(){ 私はこれらの行の両方をコメントアウトするとうまくいきます。掘り下げた後、私の問題はコンテキストと関係していると思います。私のカスタムダイアログを作成しているときに、 tはアラートダイアログからカスタムダイアログを起動 - NullPointerエラー

が私のonCreateメソッドで を解決しました。 私のコードは以下の通りです。私は間違って行くよどこの誰かが指摘することができれば、私はそれをお願い申し上げます。それを修正することができましメートルから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

に動作します。このダイアログを呼び出す場所からより多くのコードを投稿できますか? – kosa

+0

私はredeemAlertDialogからcouponDialogを呼び出しています。 redeemAlertDialog.setPositiveButtonの最後にcouponDialog.show()があります。 – Roardog

答えて

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

希望これは、NullPointerExceptionが発生している、ヌルを返しfindViewById(...)

+0

Chrisに感謝しますが、うまくいきませんでした。私は恐れています。 「ウィンドウを追加できません - トークンnullはアプリケーション用ではありません」 – Roardog

+0

couponDialog = new Dialog(mContext); mContextはどこにありますか、それに何が役立つのかは、このアクティビティまたはベースアクティビティを使用する必要があります – Chris

+0

解決しました。なんらかの理由で、getApplicationContext()は新しいDialog()が使用できるものを返さなかった。私のonCreate()メソッドでは、mContext = getApplicationContext()を変更しました。 〜mContext = this;今は動作します – Roardog

関連する問題