2013-05-14 37 views
0

我在onNewIntent(Intent intent)函數中顯示警報時遇到問題。等待意圖額外檢索正確,它的值按預期記錄,但不顯示警報。無法在onNewIntent()中顯示警報

@Override 
protected void onNewIntent(Intent intent) { 
    super.onNewIntent(intent); 

    Bundle extras = getIntent().getExtras(); 
    if(extras !=null) { 
     String value = extras.getString("message"); 
     Log.v("alert", value); // <--- this line works fine. 


     AlertDialog alertDialog = new AlertDialog.Builder(this).create(); 
     alertDialog.setTitle("title"); 
     alertDialog.setMessage(value); 
     alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() { 

      public void onClick(DialogInterface dialog, int which) { 


      } 
     }); 

     alertDialog.show(); 
    } 
} 
+0

嘗試這種情況:AlertDialog alertDialog =新AlertDialog.Builder(YourActivity.this).create(); – SKK 2013-05-14 11:42:11

+0

@Santhosh - 正確!請張貼這個答案,以便我可以接受它。 – Kristopher 2013-05-14 11:45:34

+0

快樂編碼:) – SKK 2013-05-14 11:48:36

回答

0

嘗試這種情況:

AlertDialog alertDialog = new AlertDialog.Builder(YourActivity.this).create(); 
+0

我不明白爲什麼'YourActivity.this!= this'' – PhilLab 2017-08-07 18:18:00