2012-02-02 3 views
1

경고 대화 상자의 단추 누르기에서 사용자 지정 대화 상자를 시작하려고합니다. 사용자가 redeemAlertDialog를 여는 기본 UI의 버튼을 누르면이 대화 상자에서 사용자에게이 작업을 계속할지 묻는 메시지가 표시됩니다. '예'를 클릭하면 사용자 정의 대화 상자를 열고 싶습니다. 그러나 내 사용자 지정 대화 상자를 시작하면 응용 프로그램이 중단됩니다. Logcat은 * text.setText ("Blah Blah"/ merchantName /); *에 null 포인터 오류가 있음을 알려주고 있지만이 줄을 주석 처리하면 줄에 동일한 오류가 표시됩니다. button.setOnClickListener (새로운 OnClickListener() { 나는이 두 줄을 모두 주석으로 처리하면 작동합니다. 내 문제는 문맥과 관련이 있다고 생각합니다. 사용자 지정 대화 상자를 만들 때와 연관 짓고 있지만, t는경고 대화 상자에서 사용자 지정 대화 상자 실행 - 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를 (...)를 작동

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

희망

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

사용 NullPointerException이 원인이되는 널 (null)을 반환. 이 대화 상자를 호출하는 위치에서 더 많은 코드를 게시 할 수 있습니까? – kosa

+0

redeemAlertDialog에서 couponDialog를 호출합니다. redeemAlertDialog.setPositiveButton의 끝에 couponDialog.show()를 볼 수 있습니다. – Roardog

답변

3

대신 :이

+0

Chris에게 감사하지만 작동하지 않았습니다. 두려워요. "창을 추가 할 수 없습니다 - 토큰 null은 응용 프로그램이 아닙니다." – Roardog

+0

couponDialog = new Dialog (mContext); mContext가 어디에 있고 무엇이 당신에게 도움이되는지는이 또는 기본 활동을 사용해야합니다 – Chris

+0

해결했습니다. 어떤 이유로 getApplicationContext()는 새로운 Dialog()가 사용할 수있는 것을 반환하지 않았습니다. 내 onCreate() 메서드에서 mContext = getApplicationContext()를 변경했습니다. ~ mContext = this; 그리고 지금 그것이 작동합니다. – Roardog

관련 문제