Alertdialoge没有" dismiss()"也不是"取消()"财产,我应该如何强迫它消失?

时间:2014-05-20 07:19:13

标签: java android alertdialog android-alertdialog

如代码所示,switchToGPSPreferences方法会创建alertdialoge,我可以在dialogeInterface内使用dialog.dismiss()时忽略它。

但问题是,当我在onProviderEnabled()回调中时,alertDialoge没有dismiss()cancel()属性,我不知道应该如何强制它消失?

代码

private void switchToGPSPreferences() {
    // TODO Auto-generated method stub
    Log.i(CURRENT_ACTIVITY, "Inside Of switchToGPSPreferences()");
    this.alertDialogue = new AlertDialog.Builder(this);
    this.alertDialogue.setTitle("GPS Settings");
    this.alertDialogue.setCancelable(false);
    this.alertDialogue.setMessage(ALER_DIALOGUE_MSG);

    this.alertDialogue.setNeutralButton("Settings",new DialogInterface.OnClickListener() {

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

    alertDialogue.create();
    alertDialogue.show();
}// End of showDialogueBox function.
...
...
...
...
@Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), USER_ENABLED_GPS, Toast.LENGTH_LONG).show();
        Log.i(CURRENT_ACTIVITY, "GPS Enabled, onProviderEnabled()");
        if (alertDialogue != null) {
                       // here ii want to dismiss/destroy the `alertdialoge`
                     }

    }

更新

Logcat_OutPut:

05-20 11:15:05.935: E/AndroidRuntime(32533): FATAL EXCEPTION: main
05-20 11:15:05.935: E/AndroidRuntime(32533): java.lang.RuntimeException: Failure  delivering result ResultInfo{who=null, request=1, result=0, data=null} to activity  {com.example.meetingpointlocator_03/com.example.meetingpointlocator_03.MeetingPointFix}: java.lang.ClassCastException: android.app.AlertDialog$Builder cannot be cast to android.content.DialogInterface
05-20 11:15:05.935: E/AndroidRuntime(32533):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3518)
05-20 11:15:05.935: E/AndroidRuntime(32533):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3561)
05-20 11:15:05.935: E/AndroidRuntime(32533):    at android.app.ActivityThread.access$1200(ActivityThread.java:168)
05-20 11:15:05.935: E/AndroidRuntime(32533):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1377)
05-20 11:15:05.935: E/AndroidRuntime(32533):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-20 11:15:05.935: E/AndroidRuntime(32533):    at android.os.Looper.loop(Looper.java:137)
05-20 11:15:05.935: E/AndroidRuntime(32533):    at android.app.ActivityThread.main(ActivityThread.java:5493)
05-20 11:15:05.935: E/AndroidRuntime(32533):    at java.lang.reflect.Method.invokeNative(Native Method)

05-20 11:15:05.935:E / AndroidRuntime(32533):at java.lang.reflect.Method.invoke(Method.java:525) 05-20 11:15:05.935:E / AndroidRuntime(32533):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1209) 05-20 11:15:05.935:E / AndroidRuntime(32533):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025) 05-20 11:15:05.935:E / AndroidRuntime(32533):at dalvik.system.NativeStart.main(Native Method) 05-20 11:15:05.935:E / AndroidRuntime(32533):引起:java.lang.ClassCastException:android.app.AlertDialog $ Builder无法强制转换为android.content.DialogInterface 05-20 11:15:05.935:E / AndroidRuntime(32533):at com.example.meetingpointlocator_03.MeetingPointFix.showProgressDialog(MeetingPointFix.java:102) 05-20 11:15:05.935:E / AndroidRuntime(32533):at com.example.meetingpointlocator_03.MeetingPointFix.registerGPSListener(MeetingPointFix.java:225) 05-20 11:15:05.935:E / AndroidRuntime(32533):at com.example.meetingpointlocator_03.MeetingPointFix.onActivityResult(MeetingPointFix.java:181) 05-20 11:15:05.935:E / AndroidRuntime(32533):在android.app.Activity.dispatchActivityResult(Activity.java:5563) 05-20 11:15:05.935:E / AndroidRuntime(32533):在android.app.ActivityThread.deliverResults(ActivityThread.java:3514)

1 个答案:

答案 0 :(得分:1)

在代码中执行此更改。

// gloabal variable of alert dialog.  
AlertDialog alert;


private void switchToGPSPreferences() {

    Log.i(CURRENT_ACTIVITY, "Inside Of switchToGPSPreferences()");
    AlertDialog.Builder builder = new AlertDialog.Builder(this);   
    builder.setTitle("GPS Settings");
    builder.alertDialogue.setCancelable(false);
    builder.alertDialogue.setMessage(ALER_DIALOGUE_MSG);


   builder.setNeutralButton("Settings",new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {

            dialog.dismiss();
            Intent gpsSettings = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivityForResult(gpsSettings, GPS_SETTINGS_REQUEST_CODE);
        }
    });

    alert = builder.create();
    alert.show();

}// 
...
...
...
...
  @Override
      public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub  
          Toast.makeText(getApplicationContext(), USER_ENABLED_GPS, Toast.LENGTH_LONG).show();   
          Log.i(CURRENT_ACTIVITY, "GPS Enabled, onProviderEnabled()");  
                if (builder!= null) {   
                      // here ii want to dismiss/destroy the `alertdialoge`  
                       alert.dismiss();  
                    }

    }