对话框不会显示

时间:2014-04-16 17:23:28

标签: java android

我正在创建一个对话框,以便在禁用GPS服务时向用户显示。但正在发生的事情是,虽然我手动禁用了服务以强制显示对话,但应用程序启动时并没有发生任何事情。

下面的代码显示了我是如何尝试创建对话框的。如果有任何错误,请告诉我。

JavaCode:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gpstest00);

    locMgr = (LocationManager) getSystemService(LOCATION_SERVICE);
    gpsEnable = locMgr.isProviderEnabled(LocationManager.GPS_PROVIDER);
    if (!gpsEnable) {
        showGPSDialogueBox();
    } 
    locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 1, this.locationListener);
}

/*if (savedInstanceState == null) {
    getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}*/

private void showGPSDialogueBox() {
    AlertDialog.Builder alertDialogue = new AlertDialog.Builder(this);
    alertDialogue.setTitle("GPS Settings");
    alertDialogue.setMessage("GPS is deactivated. Do you want to switch " + to settings menu to activate it?");

    alertDialogue.setPositiveButton("Settings",new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(intent);
        }
    });

    alertDialogue.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            dialog.cancel();

        }
    });
}// Enf of showDialogueBox function.

2 个答案:

答案 0 :(得分:1)

您需要为显示对话框调用show function

alertDialogue.show();

答案 1 :(得分:0)

您需要从AlertDialog对象创建AlertDialog.Builder,然后使用AlertDialog方法显示show(),如下所示...

AlertDialog dialog = alertDialogue.create();
dialog.show();

更新您的showGPSDialogueBox(),如下所示......

private void showGPSDialogueBox() {
    AlertDialog.Builder alertDialogue = new AlertDialog.Builder(this);
    alertDialogue.setTitle("GPS Settings");
    alertDialogue.setMessage("GPS is deactivated. Do you want to switch " +
            "                 to settings menu to activate it?");

    alertDialogue.setPositiveButton("Settings",new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(intent);
        }
    });

    alertDialogue.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            dialog.cancel();

        }
    });

    AlertDialog dialog = alertDialogue.create();
    dialog.show();
}