getLastKnownLocation()在没有选项卡

时间:2016-10-13 09:15:45

标签: android

在我的应用中,我想获取纬度,经度和当前位置名称,并在数据库中传递此值。该应用程序在Tab中运行完美。但是在其他设备的情况下getLastKnownLocation()返回null。出于这个原因,我面临nullpointerexception

private Location getLastKnownLocation() {
    locationmanager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
    List<String> providers = locationmanager.getProviders(true);
    Location bestLocation = null;
    for (String provider : providers) {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return null;
        }
        Location l = locationmanager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
        if (l == null) {
            continue;
        }
        if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {
            // Found best last known location: %s", l);
            bestLocation = l;
        }
    }
    return bestLocation;
}

请帮我解决这个问题。我知道以前问过这个问题。但我尝试了大部分。我仍然在同一个地方面对nullpointerexception(在获取lat long值时)。

  

10-13 15:54:12.825 16095-16095 / com.example.ivdisplays.vehereapp E / AndroidRuntime:FATAL EXCEPTION:main                                                                                     处理:com.example.ivdisplays.vehereapp,PID:16095                                                                                     显示java.lang.NullPointerException                                                                                         在com.example.ivdisplays.vehereapp.MainActivity $ 1.onClick(MainActivity.java:117)                                                                                         在android.view.View.performClick(View.java:4463)                                                                                         在android.view.View $ PerformClick.run(View.java:18792)                                                                                         在android.os.Handler.handleCallback(Handler.java:808)                                                                                         在android.os.Handler.dispatchMessage(Handler.java:103)                                                                                         在android.os.Looper.loop(Looper.java:193)                                                                                         在android.app.ActivityThread.main(ActivityThread.java:5344)                                                                                         at java.lang.reflect.Method.invokeNative(Native Method)                                                                                         在java.lang.reflect.Method.invoke(Method.java:515)                                                                                         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:860)                                                                                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:676)                                                                                         在dalvik.system.NativeStart.main(本地方法)

public class MainActivity extends AppCompatActivity implements NetworkCallback, 
LocationListener {

private EditText empIdTxt;
private EditText passwordTxt;
private TextView tv_lat;
private TextView tv_long;
private TextView tv_place;
private Button loginBtn;
private Button logoutBtn;
LocationManager locationmanager;
GPSTracker gps;
String cityName = "";
Context context;
static String loginDate;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    empIdTxt = (EditText) findViewById(R.id.et_loginId);
    passwordTxt = (EditText) findViewById(R.id.et_loginPass);
    loginBtn = (Button) findViewById(R.id.bt_login);
    logoutBtn= (Button) findViewById(R.id.bt_logout);

    empIdTxt.addTextChangedListener(new MyTextWatcher(empIdTxt));
    passwordTxt.addTextChangedListener(new MyTextWatcher(passwordTxt));

    empIdTxt.setText("vinay");
    passwordTxt.setText("qwerty");

    context=MainActivity.this;
    final Location location = getLastKnownLocation();
    if (location != null) {
        onLocationChanged(location);
    } else {
        Toast.makeText(getApplicationContext(), "location not found", Toast.LENGTH_LONG).show();
        System.out.println("Output of Location is:" + getLastKnownLocation());
    }

    loginDate = AlertDialogManager.todayDate();

    loginBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!validateEmpID()) {
                return;
            }
            if (!validatePassword()) {
                return;
            }
            LoginApi api = new LoginApi(context, (NetworkCallback) context);
            System.out.println("Output of LoginAPI:" +"username" +empIdTxt.getText().toString()
                    +"password"+passwordTxt.getText().toString()+"Lat"+location.getLatitude()+"Long"+location.getLongitude()
                    +"Date"+loginDate+"Locality"+new GPSTracker(MainActivity.this).getLocality(location));
            api.processLogin(empIdTxt.getText().toString(), passwordTxt.getText().toString(),
                    location.getLatitude()+"",location.getLongitude()+"",loginDate,
                    new GPSTracker(MainActivity.this).getLocality(location),"123456");
            loginBtn.setVisibility(View.GONE);
            logoutBtn.setVisibility(View.VISIBLE);
        }
    });

    logoutBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            LogoutApi api = new LogoutApi(context, (NetworkCallback) context);
            System.out.println("Output of LogoutAPI:" +"userid " +AppDelegate.getInstance().uid
                    +"Lat"+location.getLatitude()+"Long"+location.getLongitude()
                    +"Date"+AlertDialogManager.todayDate()+"Locality"+new GPSTracker(MainActivity.this).getLocality(location));
            api.processLogout(AppDelegate.getInstance().uid,
                    location.getLatitude()+"",location.getLongitude()+"",AlertDialogManager.todayDate(),
                    new GPSTracker(MainActivity.this).getLocality(location),"123456","");
            logoutBtn.setVisibility(View.GONE);
            loginBtn.setVisibility(View.VISIBLE);
        }
    });
}

private Location getLastKnownLocation() {
    locationmanager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
    List<String> providers = locationmanager.getProviders(true);
    Location bestLocation = null;
    for (String provider : providers) {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return null;
        }
        Location l = locationmanager.getLastKnownLocation(provider);
        if (l == null) {
            continue;
        }
        if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {
            // Found best last known location: %s", l);
            bestLocation = l;
        }
    }
    return bestLocation;
}

private void requestFocus(View view) {
    if (view.requestFocus()) {
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    }
}
private boolean validateEmpID() {
    if (empIdTxt.getText().toString().trim().isEmpty()) {
        empIdTxt.setError(ErrorUtil.USERNAME_ERROR);
        requestFocus(empIdTxt);
        return false;
    }
    return true;
}

private boolean validatePassword() {
    if (passwordTxt.getText().toString().trim().isEmpty()) {
        passwordTxt.setError(ErrorUtil.PASSWORD_ERROR);
        requestFocus(passwordTxt);
        return false;
    }
    return true;
}

private class MyTextWatcher implements TextWatcher {

    private View view;

    private MyTextWatcher(View view) {
        this.view = view;
    }

    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    }

    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    }

    public void afterTextChanged(Editable editable) {
        switch (view.getId()) {
            case R.id.et_loginId:
                validateEmpID();
                break;
            case R.id.et_loginPass:
                validatePassword();
                break;
        }
    }
}

@Override
public void updateScreen(String data, String tag) {

    if (tag.compareTo(ApiUtil.TAG_LOGIN) == 0) {
        try {
            System.out.println("Login Response"+data);

            JSONObject mainObj = new JSONObject(data);
            AppDelegate ctrl = AppDelegate.getInstance();

            if (!mainObj.isNull("user_id")) {
                ctrl.uid = mainObj.getString("user_id");
            }

            if (!mainObj.isNull("error")) {
                Toast.makeText(MainActivity.this,"Login Fails",Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(MainActivity.this,"Login Success",Toast.LENGTH_SHORT).show();
            }

        } catch (Exception e) {
            Toast.makeText(MainActivity.this,"Seems there is an issue please try again later",Toast.LENGTH_SHORT).show();

        }
    }
    if (tag.compareTo(ApiUtil.TAG_LOGOUT) == 0) {
        try {
            System.out.println("Logout Response"+data);

            JSONObject mainObj = new JSONObject(data);
            AppDelegate ctrl = AppDelegate.getInstance();

            if (!mainObj.isNull("message")) {
                Toast.makeText(MainActivity.this,"Logout Success",Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(MainActivity.this,"Logout Fails",Toast.LENGTH_SHORT).show();
            }
        } catch (Exception e) {
            Toast.makeText(MainActivity.this,"Seems there is an issue please try again later",Toast.LENGTH_SHORT).show();
        }
    }
}

@Override

public void onLocationChanged(Location location) {

    double latitude,longitude;

    tv_lat = (TextView) findViewById(R.id.tv_lat);
    tv_long = (TextView) findViewById(R.id.tv_long);
    tv_place = (TextView) findViewById(R.id.tv_place);

    latitude=location.getLatitude();
    longitude=location.getLongitude();

    tv_lat.setText("Latitude : "+latitude);
    tv_long.setText("Longitude : "+ longitude);

    // create class object
    gps = new GPSTracker(MainActivity.this);

    // check if GPS enabled
    if (gps.canGetLocation()) {

        Geocoder geocoder = new Geocoder(MainActivity.this, Locale.getDefault());
        List<Address> addresses;
        try {
            addresses = geocoder.getFromLocation(latitude,
                    longitude, 1);
            System.out.println("Output of Address" + addresses);
            cityName = addresses.get(0).getAddressLine(0);
            System.out.println("Cityname is" + cityName);
            tv_place.setText("Place : "+cityName);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } else {
        gps.showSettingsAlert();
    }
}

@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}

@Override
public void onProviderEnabled(String s) {
}

@Override
public void onProviderDisabled(String s) {
}
}

2 个答案:

答案 0 :(得分:0)

我认为问题在于检查权限和&& !=操作数。处理它更仔细。 试试这个:

private Location getLastKnownLocation() {
    locationmanager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
    boolean permissionsActivated = canAll();
    if (!permissionsActivated)
        return null;
    List<String> providers = locationmanager.getProviders(true);
    Location bestLocation = null;
    for (String provider : providers) {
        Location l = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);// this may lead to 'null' value if not permissions
        if (l == null) {
            continue;
        }
        if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {
            // Found best last known location: %s", l);
            bestLocation = l;
        }
    }
    return bestLocation;
}

private boolean canAll() {
        // TODO Auto-generated method stub
        return (canAccessLocation() && canCoarseLocation());
    }
private boolean canAccessLocation() {
        return hasPermission(android.Manifest.permission.ACCESS_FINE_LOCATION);
    }
    private boolean canCoarseLocation() {
        return hasPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION);
    }
@SuppressLint("NewApi")
    private boolean hasPermission(String perm) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            return (PackageManager.PERMISSION_GRANTED == checkSelfPermission(perm));
        } else {
            return true;
        }
    }

不要忘记:

如果应用程序未检测到任何位置,

getLastKnownLocation可能会返回空值。

答案 1 :(得分:0)

您可以使用GoogleApiClient's fused location provider获取位置信息。您需要在gradle文件中添加依赖项。即

compile 'com.google.android.gms:play-services-location:9.6.1'

在清单

中添加fine_location权限
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Dangerous Permission您需要在运行时询问this.

等权限

代码段

public class DashboardActivity extends AppCompatActivity implements 
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,
        LocationListener,
        ResultCallback<LocationSettingsResult> {

    private GoogleApiClient googleApiClient;
    private GoogleApiAvailability apiAvailability;

    private static final long MIN_TIME_BW_UPDATES = 10000;
    private static final long FAST_TIME_BW_UPDATES = 3000;
    private final static int REQUEST_CHECK_SETTINGS = 100;
    private static final long EXPIRATION_DURATION = 3600000;

    protected LocationRequest mLocationRequest;
    protected LocationSettingsRequest mLocationSettingsRequest;
    private Location location;
    private LatLng latLng = new LatLng(0, 0);
    private double latitude;
    private double longitude;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dashboard);
        init();
    }

    private void init() {
        apiAvailability = GoogleApiAvailability.getInstance();
        if (googleApiAvaibility()) {
            buildGoogleApiClient();
            displayLocation();
        }
    }

    protected void createLocationRequest() {
        mLocationRequest = LocationRequest.create();
        mLocationRequest.setInterval(MIN_TIME_BW_UPDATES);
        mLocationRequest.setFastestInterval(FAST_TIME_BW_UPDATES);
        mLocationRequest.setSmallestDisplacement(10);
        mLocationRequest.setExpirationDuration(EXPIRATION_DURATION);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
        builder.addLocationRequest(mLocationRequest);
        builder.setAlwaysShow(true);
        mLocationSettingsRequest = builder.build();
    }

    protected synchronized void buildGoogleApiClient() {
        googleApiClient = new GoogleApiClient.Builder(getApplicationContext())
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }

    private boolean googleApiAvaibility() {
        int resultCode = apiAvailability.isGooglePlayServicesAvailable(this);
        switch (resultCode) {
            case ConnectionResult.SUCCESS:
                return true;
            case ConnectionResult.API_UNAVAILABLE:
                Dialog dialog = apiAvailability.getErrorDialog(this, ConnectionResult.API_UNAVAILABLE, 200,
                        new DialogInterface.OnCancelListener() {
                            @Override
                            public void onCancel(DialogInterface dialog) {
                                //finish();
                            }
                        });
                dialog.show();
                break;
            case ConnectionResult.SERVICE_MISSING:
                Dialog dialog1 = apiAvailability.getErrorDialog(this, ConnectionResult.SERVICE_MISSING, 201,
                        new DialogInterface.OnCancelListener() {
                            @Override
                            public void onCancel(DialogInterface dialog) {
                                //finish();
                            }
                        });
                dialog1.show();
                break;
        }
        return false;
    }

    @Override
    public void onLocationChanged(Location location) {
        latLng = new LatLng(location.getLatitude(), location.getLongitude());
        //here you will get new latitude and longitude if location is changed
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case REQUEST_CHECK_SETTINGS:
                switch (resultCode) {
                    case Activity.RESULT_OK:
                        displayLocation();
                        break;
                    case Activity.RESULT_CANCELED:
                        //finish();
                        Toast.makeText(getActivity(), "Please enable location. Otherwise you can not use this application", Toast.LENGTH_LONG).show();
                        break;
                }
                break;
            default:

                break;
        }
    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {
        createLocationRequest();
        checkLocationSettings();
    }

    @Override
    public void onConnectionSuspended(int i) {
        googleApiClient.connect();
    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
        googleApiClient.connect();
    }

    @Override
    public void onResult(@NonNull LocationSettingsResult result) {
        Status status = result.getStatus();
        switch (status.getStatusCode()) {
            case LocationSettingsStatusCodes.SUCCESS:
                displayLocation();
                break;
            case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                try {
                    status.startResolutionForResult(this, REQUEST_CHECK_SETTINGS);
                } catch (IntentSender.SendIntentException e) {
                    Log.e("Exception", e.toString());
                }
                break;
            case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                //showDialog(this,"You have choose never for Location!");
                break;
            default:
                break;
        }
    }

    protected void checkLocationSettings() {
        PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.
                checkLocationSettings(googleApiClient, mLocationSettingsRequest);
        result.setResultCallback(this);
    }

    private void displayLocation() {
        if (ActivityCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 250);
        } else {
            location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
            if (location != null) {
                latLng = new LatLng(location.getLatitude(), location.getLongitude());
                latitude = location.getLatitude();
                longitude = location.getLongitude();
                //print latitude longitude here
            } else {
                if (googleApiAvaibility()) {
                    buildGoogleApiClient();
                    googleApiClient.connect();
                    if (googleApiClient.isConnected()) {
                        LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, mLocationRequest, this);
                    }
                }
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            if (permissions[0].equals(Manifest.permission.ACCESS_FINE_LOCATION)) {
                if (location != null) {
                  displayLocation();
                } else {
                    //googleApiClient.connect();
                    if (googleApiAvaibility()) {
                        buildGoogleApiClient();
                        googleApiClient.connect();
                    }
                }

            }
        }
    }
}

您还可以查看此sample