2015-06-22 42 views
-5

我表示做一個警告對話框:警報對話框,按鈕拋出異常

new AlertDialog.Builder(this) 
      .setTitle(R.string.label_searching) 
      .setMessage(R.string.label_search_noresults) 
      .setCancelable(false) 
      .setPositiveButton(DialogInterface.BUTTON_POSITIVE, null) 
      .create().show(); 

然而,這將引發異常:

致命異常:主要 android.content.res。資源$ NotFoundException:字符串資源ID#0xffffffff at android.content.res.Resources.getText(Resources.java:242) at android.content.Context.getText(Context.java:282) at android.app.AlertDialog $ Builder.setPositiveButton (AlertDialog.java:487)

當我註釋掉以下行:

.setPositiveButton(DialogInterface.BUTTON_POSITIVE, null) 

中所示的對話框,但顯然沒有顯示的按鈕。我需要在對話框中顯示一個按鈕!

我在做什麼錯?

+0

是否.setPositiveButton(「Ok」,null)'工作? –

+0

給一個字符串,然後嘗試。爲它添加一個監聽器 –

+0

http://developer.android.com/reference/android/R.string.html – Selvin

回答

1

使用它相應

AlertDialog.Builder alertbox = new AlertDialog.Builder(YourActivity.this); 

       alertbox.setTitle("Do you want To exit ?"); 
       alertbox.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface arg0, int arg1) { 
         // finish used for destroyed activity 
         exit(); 
        } 
       }); 

       alertbox.setNegativeButton("No", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface arg0, int arg1) { 
          // Nothing will be happened when clicked on no button 
          // of Dialog  
       } 
       }); 

       alertbox.show(); 
+0

樂意幫忙.. –

-1

我覺得這是有DialogInterface.BUTTON_POSITIVE 問題,你可以在here

-1

DialogInterface.BUTTON_POSITIVE找​​到的解決方案是一個常數,如果你檢查DialogInterface類:

int BUTTON_POSITIVE = -1; 

setPositiveButton方法收到無論是文本ID或chasequence爲它的論據,這是正文按鈕上顯示的文本。 Android無法找到由DialogInterface定義的-1 id的關聯字符串。

我建議你在xml文件上定義你的正面按鈕文本,就像你對標題和消息使用標籤一樣,並在第一個參數上使用它。

-1
private void createAlertDialog() { 

    AlertDialog.Builder alrtDialog = new AlertDialog.Builder(
       this); 
     alrtDialog.setMessage("your message").setCancelable(false); 
     alrtDialog.setPositiveButton("ButtonName", 
       new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       //Do somthing 
      } 
     }); 

     alrtDialog.setNeutralButton(R.string.cancel, 
       new DialogInterface.OnClickListener() { 

      public void onClick(DialogInterface dialog, int which) { 
       //Do somthing 
      } 
     }); 
     alrtDialog.create(); 
     alrtDialog.show(); 
    } 
-1

你需要得到這樣的事情串ID。

new AlertDialog.Builder(this) 
    .setTitle(getResources().getString(R.string.label_searching)) 
    .setMessage(getResources().getString(R.string.label_search_noresults)) 
    .setCancelable(false) 
    .setPositiveButton(DialogInterface.BUTTON_POSITIVE, null) 
    .create().show(); 
相關問題