2015-11-23 126 views
0

我試圖顯示一條警告消息。 但是當我點擊特定的按鈕來顯示警告消息時,應用程序不幸停止。如何在Android應用程序中顯示警報消息

我警報消息代碼是在這裏...

search.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 

      Cursor res = myDB.getAllData(); 
      if (res.getCount() == 0) { 
       showMessage("Error", "Nothing found"); 
       return; 
      } 

      StringBuffer buffer = new StringBuffer(); 
      while (res.moveToNext()) { 
       buffer.append("EMAIL: " + res.getString(0) + "\n"); 
       buffer.append("full_name : " + res.getString(1) + "\n"); 
       buffer.append("district : " + res.getString(2) + "\n"); 
       buffer.append("phone_num : " + res.getString(3) + "\n"); 

      } 
      showMessage("Data", buffer.toString()); 
     } 



    }); 
} 

public void showMessage(String title, String message) 
{ 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setCancelable(true); 
    builder.setTitle(title); 
    builder.setMessage(message); 
    builder.show(); 

} 
+1

檢查您是否打開數據庫連接(即檢查myDB是否爲空並且遊標返回非空)。 –

+0

它更好地發送您的logcat了 –

回答

0

代替

AlertDialog.Builder builder = new AlertDialog.Builder(this); 

使用本

AlertDialog.Builder builder = new AlertDialog.Builder(YourActivity.this); // for activity 

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); // for fragment 
1

試試這個代碼:

AlertDialog.Builder builder1 = new AlertDialog.Builder(context); 
      builder1.setMessage("Write your message here."); 
      builder1.setCancelable(true); 
      builder1.setPositiveButton("Yes", 
        new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        dialog.cancel(); 
       } 
      }); 
      builder1.setNegativeButton("No", 
        new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        dialog.cancel(); 
       } 
      }); 

      AlertDialog alert11 = builder1.create(); 
      alert11.show(); 
0
public static void showAlert(final Activity activity, String title, 
      String message, final boolean finish) { 
     new AlertDialog.Builder(activity).setTitle(title).setMessage(message) 
       .setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         // TODO Auto-generated method stub 
         dialog.dismiss(); 
         if (finish) { 
          activity.finish(); 
         } 
        } 
       }).show(); 
    } 
0

我測試過showMessage方法。它的工作沒有問題。我想你必須檢查你的數據庫連接,cursorbuffer。但是,此代碼用於顯示警報消息:

/** 
* @param context 
* @param title 
* @param message 
* 
*   Caution:Error--> android.view.WindowManager$BadTokenException: 
*   Unable to add window -- token null is not for an application 
* 
*   --> Instead of getApplicationContext(), just use 
*   ActivityName.this 
*/ 
public static AlertDialog showDialog(final Context context, String title, 
     String message) { 
    AlertDialog.Builder builder = new AlertDialog.Builder(context); 
    builder.setCancelable(true); 
    builder.setMessage(message); 
    builder.setTitle(title); 
    builder.setPositiveButton(R.string.ok, 
      new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
       } 
      }); 
    return builder.show(); 
} 
相關問題