2017-05-25 56 views
-2

也許有人可以告訴我爲什麼下面的代碼工作在Api上面25,但在Api 19哪一個現在我測試我有null之後,我嘗試獲取經度和緯度? 我讀過官方docs但但我用(目標阿比25)每一種方法是跟蹤API 19LoactionManager Api19 NullPointerException

Location loc = gpStracker.getlocation(); 
     latitude = loc.getLatitude(); 
     longitude = loc.getLongitude(); 

和gpsTracker類兼容:

public class GPStracker implements LocationListener { 

    Context context; 

    public GPStracker(Context c) { 
     context = c; 
    } 

    public Location getlocation() { 
     if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      Toast.makeText(context, "Uprawnienia nie przyznane", Toast.LENGTH_SHORT).show(); 
     } 

     LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); 
     boolean isGPSenabled; 
     isGPSenabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER); 

     if (isGPSenabled) { 
      lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, this); 
      Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
      return location; 
     } else { 
      Toast.makeText(context, "Proszę włączyć GPS", Toast.LENGTH_LONG).show(); 
     } 
     return null; 
    } 

    @Override 
    public void onLocationChanged(Location location) { 

     //Toast.makeText(context, String.valueOf(dis), Toast.LENGTH_SHORT).show(); 
    } 

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

    } 

    @Override 
    public void onProviderEnabled(String s) { 

    } 

    @Override 
    public void onProviderDisabled(String s) { 

    } 
} 
+0

應該由谷歌 –

回答

-1

使用的位置經理讓谷歌播放服務可通過您的應用程序build.gradle文件獲得。

dependencies { 
    compile 'com.google.android.gms:play-services:9.2.0' 
    compile 'com.google.android.gms:play-services-location:9.2.0' 
    } 

還要在清單中指定以下必需的權限。

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


public class LocationActivity extends Activity implements 
     LocationListener, 
     GoogleApiClient.ConnectionCallbacks, 
     GoogleApiClient.OnConnectionFailedListener { 

    private static final String TAG = "LocationActivity"; 
    private static final long INTERVAL = 1000 * 10; 
    private static final long FASTEST_INTERVAL = 1000 * 5; 
    Button btnFusedLocation; 
    TextView tvLocation; 
    LocationRequest mLocationRequest; 
    GoogleApiClient mGoogleApiClient; 
    Location mCurrentLocation; 
    String mLastUpdateTime; 

    protected void createLocationRequest() { 
     mLocationRequest = new LocationRequest(); 
     mLocationRequest.setInterval(INTERVAL); 
     mLocationRequest.setFastestInterval(FASTEST_INTERVAL); 
     mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Log.d(TAG, "onCreate ..............................."); 
     //show error dialog if GoolglePlayServices not available 
     if (!isGooglePlayServicesAvailable()) { 
      finish(); 
     } 
     createLocationRequest(); 
     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .addApi(LocationServices.API) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .build(); 

     setContentView(R.layout.activity_main); 
     tvLocation = (TextView) findViewById(R.id.tvLocation); 

     btnFusedLocation = (Button) findViewById(R.id.btnShowLocation); 
     btnFusedLocation.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View arg0) { 
       updateUI(); 
      } 
     }); 

    } 

    @Override 
    public void onStart() { 
     super.onStart(); 
     Log.d(TAG, "onStart fired .............."); 
     mGoogleApiClient.connect(); 
    } 

    @Override 
    public void onStop() { 
     super.onStop(); 
     Log.d(TAG, "onStop fired .............."); 
     mGoogleApiClient.disconnect(); 
     Log.d(TAG, "isConnected ...............: " + mGoogleApiClient.isConnected()); 
    } 

    private boolean isGooglePlayServicesAvailable() { 
     int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); 
     if (ConnectionResult.SUCCESS == status) { 
      return true; 
     } else { 
      GooglePlayServicesUtil.getErrorDialog(status, this, 0).show(); 
      return false; 
     } 
    } 

    @Override 
    public void onConnected(Bundle bundle) { 
     Log.d(TAG, "onConnected - isConnected ...............: " + mGoogleApiClient.isConnected()); 
     startLocationUpdates(); 
    } 

    protected void startLocationUpdates() { 
     PendingResult<Status> pendingResult = LocationServices.FusedLocationApi.requestLocationUpdates(
       mGoogleApiClient, mLocationRequest, this); 
     Log.d(TAG, "Location update started ..............: "); 
    } 

    @Override 
    public void onConnectionSuspended(int i) { 

    } 

    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 
     Log.d(TAG, "Connection failed: " + connectionResult.toString()); 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
     Log.d(TAG, "Firing onLocationChanged.............................................."); 
     mCurrentLocation = location; 
     mLastUpdateTime = DateFormat.getTimeInstance().format(new Date()); 
     updateUI(); 
    } 

    private void updateUI() { 
     Log.d(TAG, "UI update initiated ............."); 
     if (null != mCurrentLocation) { 
      String lat = String.valueOf(mCurrentLocation.getLatitude()); 
      String lng = String.valueOf(mCurrentLocation.getLongitude()); 
      tvLocation.setText("At Time: " + mLastUpdateTime + "\n" + 
        "Latitude: " + lat + "\n" + 
        "Longitude: " + lng + "\n" + 
        "Accuracy: " + mCurrentLocation.getAccuracy() + "\n" + 
        "Provider: " + mCurrentLocation.getProvider()); 
     } else { 
      Log.d(TAG, "location is null ..............."); 
     } 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     stopLocationUpdates(); 
    } 

    protected void stopLocationUpdates() { 
     LocationServices.FusedLocationApi.removeLocationUpdates(
       mGoogleApiClient, this); 
     Log.d(TAG, "Location update stopped ......................."); 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 
     if (mGoogleApiClient.isConnected()) { 
      startLocationUpdates(); 
      Log.d(TAG, "Location update resumed ....................."); 
     } 
    } 
} 


<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:id="@+id/linearLayout1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="40dip" 
     android:orientation="horizontal" > 

     <TextView 
      android:id="@+id/TextView01" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="10dip" 
      android:layout_marginRight="5dip" 
      android:text="Latitude: " 
      android:textSize="20dip" > 
     </TextView> 

     <TextView 
      android:id="@+id/TextView02" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="unknown" 
      android:textSize="20dip" > 
     </TextView> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/linearLayout2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:id="@+id/TextView03" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="10dip" 
      android:layout_marginRight="5dip" 
      android:text="Longitute: " 
      android:textSize="20dip" > 
     </TextView> 

     <TextView 
      android:id="@+id/TextView04" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="unknown" 
      android:textSize="20dip" > 
     </TextView> 
    </LinearLayout> 

</LinearLayout> 
+0

使用熔融位置API https://meta.stackexchange.com/問題/ 8231 /是 - 答案 - 即,剛剛包含鏈接別處 - 真的好 - 答案/ 8259#8259 – 2Dee

0

功能getLastKnownLocation可能返回null,即使你叫requestLocationUpdates。此外,由於在getLocation功能,你可以返回null還有,我建議你添加一個檢查如下:

Location loc = gpStracker.getlocation(); 
if (loc != null) { 
    latitude = loc.getLatitude(); 
    longitude = loc.getLongitude(); 
}