2016-03-04 93 views
0

這就是我試圖在android應用程序中獲取位置的方式。但每次我開始我的應用程序它給我NullPointerException,雖然我檢查,如果位置爲null。這裏發生了什麼?請幫幫我。在位置上獲取NullPointerException Android

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; 
       if (isNetworkEnabled) { 
        //updates will be send according to these arguments 
        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(); 
         } 
        } 
       } 
       // if GPS Enabled get lat/long using GPS Services 
       if (isGPSEnabled) { 
        if (location == null) { 
         locationManager.requestLocationUpdates(
           LocationManager.GPS_PROVIDER, 
           MIN_TIME_BW_UPDATES, 
           MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
         Log.d("GPS Enabled", "GPS Enabled"); 
         if (locationManager != null) { 
          location = locationManager 
            .getLastKnownLocation(LocationManager.GPS_PROVIDER); 
          if (location != null) { 
           latitude = location.getLatitude(); 
           longitude = location.getLongitude(); 


          } 
         } 
        } 
       } 
      } 

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

而且這種方法在我由intent.I調用一個服務運行要打發我的位置給服務器使用的AsyncTask像這樣:

@Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show(); 
     Log.i(LOG, "Service started"); 
     Log.i("asd", "This is sparta"); 



     new SendToServer().execute(Double.toString(getLocation().getLongitude()),Double.toString(getLocation().getLatitude()); 
return START_STICKY; 
    } 

這裏是我的logcat:

3-04 12:55:03.748 3235-3235/sourcemate.com.locationsender E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: sourcemate.com.locationsender, PID: 3235 
    java.lang.RuntimeException: Unable to start service [email protected] with Intent { cmp=sourcemate.com.locationsender/.LocationService }: java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLongitude()' on a null object reference 
      at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2914) 
      at android.app.ActivityThread.access$2100(ActivityThread.java:151) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1401) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:135) 
      at android.app.ActivityThread.main(ActivityThread.java:5257) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:372) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLongitude()' on a null object reference 
      at sourcemate.com.locationsender.LocationService.onStartCommand(LocationService.java:75) 
      at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2897) 
            at android.app.ActivityThread.access$2100(ActivityThread.java:151) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1401) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:135) 
            at android.app.ActivityThread.main(ActivityThread.java:5257) 
+2

哪裏是你的logcat? –

+0

你的目標sdk是什麼? –

+0

你想得到Lat和Long的用戶嗎? –

回答

0

請將此課程用於位置。

import android.Manifest; 
import android.app.AlertDialog; 
import android.app.Service; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.content.pm.PackageManager; 
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; 


public class GPSTracker extends Service implements LocationListener { 

    private final Context mContext; 

    // flag for GPS status 
    public boolean isGPSEnabled = false; 

    // flag for network status 
    boolean isNetworkEnabled = false; 

    // flag for GPS status 
    boolean canGetLocation = false; 

    //flag for WIFi 
    boolean isWifiEnabled = false; 


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

    // The minimum distance to change Updates in meters 
    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters 

    // The minimum time between updates in milliseconds 
    private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute 
// private static final long MIN_TIME_BW_UPDATES = 1000 ; // 1 sec 

    // Declaring a Location Manager 
    protected LocationManager locationManager; 

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

    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); 

      isWifiEnabled = locationManager.isProviderEnabled(LocationManager.PASSIVE_PROVIDER); 


      if (!isGPSEnabled && !isNetworkEnabled && !isWifiEnabled) { 
       // no network provider is enabled 
      } else { 

       this.canGetLocation = true; 
       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(); 

         } 
        } 
       } 
       // if GPS Enabled get lat/long using GPS Services 
       if (isGPSEnabled) { 
        if (location == null) { 


         locationManager.requestLocationUpdates(
           LocationManager.GPS_PROVIDER, 
           MIN_TIME_BW_UPDATES, 
           MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
         Log.d("GPS Enabled", "GPS Enabled"); 
         if (locationManager != null) { 
          location = locationManager 
            .getLastKnownLocation(LocationManager.GPS_PROVIDER); 
          if (location != null) { 
           latitude = location.getLatitude(); 
           longitude = location.getLongitude(); 

          } 
         } 
        } 
       } 


       if (isWifiEnabled) { 

        if (location == null) { 
         locationManager.requestLocationUpdates(
           LocationManager.PASSIVE_PROVIDER, 
           MIN_TIME_BW_UPDATES, 
           MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
         Log.d("GPS Enabled", "GPS Enabled"); 
         if (locationManager != null) { 
          location = locationManager 
            .getLastKnownLocation(LocationManager.PASSIVE_PROVIDER); 
          if (location != null) { 
           latitude = location.getLatitude(); 
           longitude = location.getLongitude(); 

          } 
         } 
        } 

       } 
       if (String.valueOf(latitude).equalsIgnoreCase("0.0") || String.valueOf(longitude).equalsIgnoreCase("0.0")) { 
//     showSettingsAlert(); 
        canGetLocation = false; 

       } 


      } 

     } catch (Exception e) { 
     } 
     return location; 
    } 

    /** 
    * Stop using GPS listener 
    * Calling this function will stop using GPS in your app 
    * */ 
    public void stopUsingGPS() { 
     if (locationManager != null) { 
      if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
       // TODO: Consider calling 
       // public void requestPermissions(@NonNull String[] permissions, int requestCode) 
       // 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 Activity#requestPermissions for more details. 
       return; 
      } 
      locationManager.removeUpdates(GPSTracker.this); 
     } 
    } 

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

     // return latitude 
     return latitude; 
    } 

    /** 
    * Function to get longitude 
    * */ 
    public double getLongitude(){ 
     if(location != null){ 
      longitude = location.getLongitude(); 
     } 

     // return longitude 
     return longitude; 
    } 

    /** 
    * Function to check GPS/wifi enabled 
    * @return boolean 
    * */ 
    public boolean canGetLocation() { 
     return this.canGetLocation; 
    } 

    /** 
    * Function to show settings alert dialog 
    * On pressing Settings button will lauch Settings Options 
    * */ 
    public void showSettingsAlert(){ 
     AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext,AlertDialog.THEME_HOLO_LIGHT); 

     // Setting Dialog Title 
     alertDialog.setTitle("GPS settings"); 

     // Setting Dialog Message 
     alertDialog.setMessage("GPS is not enabled. Please turn ON GPS."); 

     // On pressing Settings button 
     alertDialog.setNeutralButton("Settings", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
       mContext.startActivity(intent); 
      } 
     }); 

     // Showing Alert Message 
     alertDialog.show(); 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
    } 

    @Override 
    public void onProviderDisabled(String provider) { 
    } 

    @Override 
    public void onProviderEnabled(String provider) { 
    } 

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

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

} 

,並使用這個類作爲 -

GPSTracker gps = new GPSTracker(CLASSNAME.this); 
double latitude = gps.getLatitude(); 
double longitude = gps.getLongitude(); 

編輯

請在您的清單文件中添加這一點。

<!-- Required to show current location --> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
+0

您提供的示例來自Androidhive,我試過了。同樣的結果。 –

+0

請參閱我的編輯。 –

0
**I see that you want the Current Lat and Long of the User, the above method using GPS is not efficient. 
Try this to Get the Lat and Long** 

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

    private GoogleApiClient mGoogleApiClient; 

    mGoogleApiClient = new GoogleApiClient.Builder(this) 
     .addConnectionCallbacks(this) 
     .addOnConnectionFailedListener(this) 
     .addApi(LocationServices.API) 
     .build(); 


     mLocationRequest = LocationRequest.create() 
     .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) 
     .setInterval(10 * 1000)  // 10 seconds, in milliseconds 
     .setFastestInterval(1 * 1000); // 1 second, in milliseconds 
    public class YourActivity extends AppCompact implements 
     GoogleApiClient.ConnectionCallbacks, 
     GoogleApiClient.OnConnectionFailedListener { 



    @Override 
    public void onConnected(Bundle bundle) { 
    Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
    if (location == null) { 
     LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
    } 
    else { 
     handleNewLocation(location); 
    } 
} 
    @Override 
    public void onConnectionSuspended(int i) { 
    Log.i(TAG, "Location services suspended. Please reconnect."); 
    } 

    @Override 
    protected void onResume() { 
    super.onResume(); 
    setUpMapIfNeeded(); 
    mGoogleApiClient.connect(); 
    } 

    @Override 
    protected void onPause() { 
    super.onPause(); 
    if (mGoogleApiClient.isConnected()) { 
     mGoogleApiClient.disconnect(); 
    } 
    } 

    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 
    if (connectionResult.hasResolution()) { 
     try { 
      // Start an Activity that tries to resolve the error 
      connectionResult.startResolutionForResult(this, CONNECTION_FAILURE_RESOLUTION_REQUEST); 
     } catch (IntentSender.SendIntentException e) { 
      e.printStackTrace(); 
     } 
    } else { 
     Log.i(TAG, "Location services connection failed with code " + connectionResult.getErrorCode()); 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
    handleNewLocation(location); 
    } 
0

創建一個服務。其功能將是獲得經緯度。

public class Location_Service extends Service implements 
    GoogleApiClient.ConnectionCallbacks, 
    GoogleApiClient.OnConnectionFailedListener { 


private Handler handler = new Handler(); 


private GoogleApiClient mGoogleApiClient; 

private LocationRequest mLocationRequest; 



public static final String BROADCAST_ACTION = "Hello World"; 

private static final int TWO_MINUTES = 2; 
public MyLocationListener listener; 
public Location previousBestLocation = null; 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 


    mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .addApi(LocationServices.API) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .build(); 
    intent = new Intent(BROADCAST_ACTION); 
    mGoogleApiClient.connect(); 


    return START_STICKY; 
} 


@Override 
public void onCreate() { 
    super.onCreate(); 


} 


@Override 
public IBinder onBind(Intent intent) { 


    return null; 
} 

@Override 
public void onConnected(Bundle bundle) { 

    if (mGoogleApiClient.isConnected()) { 
     mLocationRequest = LocationRequest.create(); 
     mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
     mLocationRequest.setInterval(10000); // Update location every second 
     listener = new MyLocationListener(); 
     LocationServices.FusedLocationApi.requestLocationUpdates(
       mGoogleApiClient, mLocationRequest, listener); 
    } 
} 

@Override 
public void onConnectionSuspended(int i) { 

} 

@Override 
public void onConnectionFailed(ConnectionResult connectionResult) { 

} 

public class MyLocationListener implements LocationListener { 

    public void onLocationChanged(final Location loc) { 
     // Log.i("**************************************", "Location changed"); 
     if (isBetterLocation(loc, previousBestLocation)) { 
     //  Log.e("Lat/Long : ", "" + loc.getLatitude() + "/" + loc.getLongitude()); 
      // Log.d("Provider : ", "" + loc.getProvider()); 

      Utils._LATITUDE = Double.parseDouble(String.valueOf(loc.getLatitude())); 
      Utils._LONGITUDE = Double.parseDouble(String.valueOf(loc.getLongitude())); 


     } 
    } 


} 

protected boolean isBetterLocation(Location location, 
            Location currentBestLocation) { 
    if (currentBestLocation == null) { 
     // A new location is always better than no location 
     return true; 
    } 

    // Check whether the new location fix is newer or older 
    long timeDelta = location.getTime() - currentBestLocation.getTime(); 
    boolean isSignificantlyNewer = timeDelta > TWO_MINUTES; 
    boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES; 
    boolean isNewer = timeDelta > 0; 

    // If it's been more than two minutes since the current location, use 
    // the new location 
    // because the user has likely moved 
    if (isSignificantlyNewer) { 
     return true; 
     // If the new location is more than two minutes older, it must be 
     // worse 
    } else if (isSignificantlyOlder) { 
     return false; 
    } 

    // Check whether the new location fix is more or less accurate 
    int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation 
      .getAccuracy()); 
    boolean isLessAccurate = accuracyDelta > 0; 
    boolean isMoreAccurate = accuracyDelta < 0; 
    boolean isSignificantlyLessAccurate = accuracyDelta > 200; 

    // Check if the old and new location are from the same provider 
    boolean isFromSameProvider = isSameProvider(location.getProvider(), 
      currentBestLocation.getProvider()); 

    // Determine location quality using a combination of timeliness and 
    // accuracy 
    if (isMoreAccurate) { 
     return true; 
    } else if (isNewer && !isLessAccurate) { 
     return true; 
    } else if (isNewer && !isSignificantlyLessAccurate 
      && isFromSameProvider) { 
     return true; 
    } 
    return false; 
} 

/** 
* Checks whether two providers are the same 
*/ 
private boolean isSameProvider(String provider1, String provider2) { 
    if (provider1 == null) { 
     return provider2 == null; 
    } 
    return provider1.equals(provider2); 
    } 
} 

在menifest文件中添加以下行

<!-- Required to show current location --> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />