2014-06-17 14 views
0

我正在嘗試創建一個警告對話框,但是當我運行我的應用程序時它不會顯示。這裏是我的代碼:Android應用程序中的AlertDialog不顯示

final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); 
    builder.setMessage("Your GPS seems to be disabled, do you want to enable it?").setCancelable(false).setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
     public void onClick(final DialogInterface dialog, final int id) { 
      startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS)); 
     } 
    }).setNegativeButton("No", new DialogInterface.OnClickListener() { 
     public void onClick(final DialogInterface dialog, final int id) { 
      dialog.cancel(); 
      buildLocationMessage(); 
     } 
    }); 
    final AlertDialog alert = builder.create(); 
    alert.show(); 

回答

0

試試這個:

final AlertDialog alertDialog = new AlertDialog.Builder(this).create(); 
     alertDialog.setTitle(title); 
     alertDialog.setMessage(message); 

     alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       alertDialog.dismiss(); 
      } 
     }); 
     alertDialog.show(); 

只需添加SET按鈕爲 「否」

+0

仍然一無所獲。我也收到一個警告,表示方法setButton已被棄用。 –

0

試試這個代碼 -

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

builder.setMessage("Your GPS seems to be disabled,do you want to enable it?"); 

builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 

public void onClick(DialogInterface dialog, int whichButton) { 

//do stuff 
startActivity(
    new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS)); 
    } 
}); 

builder.setNegativeButton("No", new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int id) { 
     dialog.cancel(); 
    } 
}); 

AlertDialog alert = builder.create(); 
alert.show(); 
+0

沒有工作。這本質上是相同的代碼,只是間隔更多沒有?想知道我是否錯過了你所做的改變! –

+0

我最近做了改變。嘗試上面的最新更新。 –

+0

是的,我看到並使用了你的編輯。這真的很奇怪,這與我之前使用過的代碼基本相同,但現在在不同的地方,它不起作用 –

相關問題