Alertdialog已保护Access

时间:2014-08-29 01:30:45

标签: android

我正在尝试Android开发,并获取设备的位置。

下面列出了我的GPSTracker类,并引发了错误。

import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.util.Log;

/**
 * Created by informationservices on 29/08/14.
 */
public class GPSTracker extends Service implements LocationListener {


    private final Context mContext;

    //flag for GPS status
    boolean isGPSEnabled = false;
    //flag for network status
    boolean isNetworkEnabled = false;

    boolean canGetLocation = false;

    Location location;
    double latitude; //latitude variable
    double longitude; //longitude variable

    //The minimum distance to change updates in meters

    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 1;
    private static final long MIN_TIME_BW_UPDATES = 1000;

    //declare location manager
    protected LocationManager locationManager;


    public GPSTracker(Context context){
        this.mContext = context;
        getLocation();
    }

    //function to get latitude
    public double getLatitude(){
        if (location != null){
            latitude = location.getLatitude();

        }
        //must have a return (as its a function)
        return latitude;
    }

    public double  getLongitude(){
        if (location !=null){
            longitude = location.getLongitude();
        }

        return longitude;
    }





    public Location getLocation() {
        try {
            locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);

            //getting GPS Status
            isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

            //getting Network Status
            isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            if (!isGPSEnabled && !isNetworkEnabled) {
                //No Network provider is enabled
            }else{
                this.canGetLocation=true;
                //first get location from Network Provider
                if(isNetworkEnabled){
                    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("Network", "Network");
                    if (locationManager !=null){
                        location=locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if(location !=null){
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();

                        }
                    }
                }

            }
        }
        catch (Exception e){
            e.printStackTrace();

        }
        return location;
    }

    //check if this is the best network provider
    public boolean canGetLocation(){
        return this.canGetLocation;
    }

    //show GPs settings in alert box
    public void showSettingsAlert(){
        AlertDialog.Builder alertDialog = new AlertDialog(mContext);//AlertDialog(android.content.Context) has protected access in 'android.app.AlertDialog'

        //set alert title
        alertDialog.setTitle("GPS is Settings");

        //set Dialog message
        alertDialog.setMessage("GPS is not Enabled. Do you want to go to settings menu?");

        alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int which){
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                mContext.startActivities(new Intent[]{intent});

            }
        });

        alertDialog.setNegativeButton("Cancel",new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int which){
                dialog.cancel();
            }
        });

        //show alert
        alertDialog.show();
    }


    @Override
    public void onLocationChanged(Location location) {

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

这是我第一次尝试编写android。我一直在谷歌上搜索,但不知道为什么会抛出这个错误。

有人可以解释这个错误的含义,以及如何解决它。

彻底解释,并且也欢迎链接到任何相关的Google文档。

2 个答案:

答案 0 :(得分:21)

错误意味着无法访问AlertDialog构造函数(公共)。它被声明为protected,因此在使用AlertDialogs时,程序员必须使用构建器模式。

要显示AlertDialog,您可以使用AlertDialog.Builder设置所有内容,然后调用show()来构建并显示AlertDialog。

// Use the AlertDialog.Builder to configure the AlertDialog.
AlertDialog.Builder alertDialogBuilder =
        new AlertDialog.Builder(this)
                .setTitle("GPS is Settings")
                .setMessage("GPS is not Enabled. Do you want to go to settings menu?")
                .setPositiveButton("Settings", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                        mContext.startActivities(new Intent[]{intent});
                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });

// Show the AlertDialog.
AlertDialog alertDialog = alertDialogBuilder.show();

答案 1 :(得分:1)

我反驳了这个问题,因为我刚才要了解这个问题 在AlertDialog.BuilderAlertDialog之间,所以当我写

AlertDialog dialog=new AlertDialog();

所以它给了我和你一样的问题 但既然你写了

AlertDialog.Builder alertDialog = new AlertDialog(mContext);

                                  //.Builder was missing
AlertDialog.Builder = new AlertDialog.Builder(mContext)

如果您想使用AlertDialog,那么就这样做

AlertDialog = new AlertDialog.Builder(mContext)

这应该有用。