当我试图展示我的位置时,为什么我会崩溃?

时间:2017-02-08 11:57:15

标签: android google-maps google-api-client

我的应用在使用这些行后崩溃了:

LatLng curPos = new LatLng(myLocation.getLatitude(), myLocation.getLongitude());

这是我得到的例外:

`E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.NullPointerException
       at ks.pinder.LocationActivity.onMapReady(LocationActivity.java:139)
       at com.google.android.gms.maps.SupportMapFragment$zza$1.zza(Unknown Source)
       at com.google.android.gms.maps.internal.zzt$zza.onTransact(Unknown Source)
       at android.os.Binder.transact(Binder.java:326)
       at aai.a(:com.google.android.gms.DynamiteModulesB:82)
       at maps.ad.t$5.run(Unknown Source)
       at android.os.Handler.handleCallback(Handler.java:615)
       at android.os.Handler.dispatchMessage(Handler.java:92)
       at android.os.Looper.loop(Looper.java:137)
       at android.app.ActivityThread.main(ActivityThread.java:4898)
       at java.lang.reflect.Method.invokeNative(Native Method)
       at java.lang.reflect.Method.invoke(Method.java:511)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
       at dalvik.system.NativeStart.main(Native Method)

我是初学Android程序员,我不知道这意味着什么。 我该怎么办才能解决问题并提出我的位置?

以下是所有代码:

package ks.pinder;

import android.content.pm.PackageManager;
import android.location.Location;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;


public class LocationActivity extends AppCompatActivity implements
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,
        LocationListener,

        OnMapReadyCallback
{
    private GoogleApiClient mGoogleApiClient;
    private Location myLocation;
    private LocationRequest mLocationRequest;
    private int REQUEST_LOCATION = 1;

    private static final String TAG = "debug";
    private GoogleMap mMap;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.i(TAG, "onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
      //  buildGoogleApiClient();
    }
    private void buildGoogleApiClient() {
        if (mGoogleApiClient == null) {
            Log.i(TAG, "onCreate - mGoogleApiClient == null");
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .addApi(LocationServices.API)
                    .build();
        }
    }

    @Override
    public void onConnected(Bundle bundle) {
        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            Log.i(TAG, "onConnected - there is no permissions");
            ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_LOCATION);
        }
        else {
            Log.i(TAG, "onConnected - there is all permissions");
            createLocationRequest();
            myLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
            Log.i(TAG, String.valueOf(myLocation.getLatitude()));
            Log.i(TAG, String.valueOf(myLocation == null));
            // startLocationUpdates(); //this method doesn't work
        }
    }
    protected void createLocationRequest() {
        Log.i(TAG, "createLocationRequest");
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(1000);
        mLocationRequest.setFastestInterval(1000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {

    }

    @Override
    protected void onStart() {
        Log.i(TAG, "onStart");
        super.onStart();
        if (mGoogleApiClient != null) {
            mGoogleApiClient.connect();
            Log.i(TAG, String.valueOf(mGoogleApiClient.isConnected()));
        }
    }

    @Override
    protected void onPause() {
        Log.i(TAG, "onPause");
        super.onPause();
        stopLocationUpdates();
    }
    protected void stopLocationUpdates() {
        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
    }

    @Override
    protected void onStop() {
        Log.i(TAG,"onStop");
        super.onStop();
        if (mGoogleApiClient != null){
            mGoogleApiClient.disconnect();
        }
    }

    @Override
    public void onLocationChanged(Location location) {
        myLocation = location;
    }




    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        Log.i(TAG, "onMapReady");
        // Add a marker in Sydney, Australia, and move the camera.

     //  Log.i(TAG,String.valueOf(myLocation.getLatitude()));
        LatLng curPos = new LatLng(myLocation.getLatitude(),myLocation.getLongitude());
        mMap.addMarker(new MarkerOptions().position(curPos).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(curPos));
    }
}

1 个答案:

答案 0 :(得分:0)

像这样更新您的代码

@Override
public void onLocationChanged(Location location) {
    myLocation = location;
    String lat= ""+mylocation.getLatitude();
    String longs= ""+mylocation.getLongitude();

      //  Do your remaining stuffs here
}