重构后获取空对象引用

时间:2016-01-19 14:56:29

标签: java android google-api-client locationlistener

编辑:我现在已经初步化了BreakDownOnMaps对象

我收到以下错误:

E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.model.Marker com.google.android.gms.maps.GoogleMap.addMarker(com.google.android.gms.maps.model.MarkerOptions)' on a null object reference
E/AndroidRuntime:     at com.example.yomac_000.rsrpechhulp.BreakDownOnMaps.handleNewLocation(BreakDownOnMaps.java:57)
E/AndroidRuntime:     at utils.MyLocationListener.onConnected(MyLocationListener.java:52)

这是因为这行代码:

gMap.addMarker(options);

这段代码:

breakDownOnMaps.handleNewLocation(location);

下面你可以看到我的代码:

MyLocationListener.java:

public class MyLocationListener implements
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,
        LocationListener {
    private final Context context;
    private GoogleMap mMap;
    private BreakDownOnMaps breakDownOnMaps = new BreakDownOnMaps();
    protected GoogleApiClient mGoogleApiClient;
    private LocationRequest mLocationRequest =  new LocationRequest();

    public MyLocationListener(Context context) {
        this.context = context;
        buildApi();
    }

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

    @Override
    public void onConnected(Bundle bundle) {
        System.out.println("onConnected");
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        }
        Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        if (location == null) {
            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, (com.google.android.gms.location.LocationListener) this);
        }
        else {
            breakDownOnMaps.handleNewLocation(location);
        };
    }

    private void buildApi() {
        mGoogleApiClient = new GoogleApiClient.Builder(context)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }

    public void connect() {
        mGoogleApiClient.connect();
    }
}

这是BreakDownOnMaps.java的代码:

public class BreakDownOnMaps extends FragmentActivity implements
        OnMapReadyCallback {
    double currentLatitude;
    double currentLongitude;
    LatLng latLng;
    GoogleMap gMap;
    private MyLocationListener myLocationListener;

    @TargetApi(Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_break_down_on_maps);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
        myLocationListener = new MyLocationListener(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        myLocationListener.connect();
    }

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

    public void handleNewLocation(Location loc) {
        currentLatitude = loc.getLatitude();
        currentLongitude = loc.getLongitude();
        latLng = new LatLng(currentLatitude, currentLongitude);
        System.out.println("handleNewLocation ");
        MarkerOptions options = new MarkerOptions()
                .position(latLng)
                .title("I am here!");
        gMap.addMarker(options);
        gMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        System.out.println("currentLatitude : " + currentLatitude);
        System.out.println("currentLongitude : " + currentLongitude);
        latLng = new LatLng(currentLatitude, currentLongitude);
        setgMap(googleMap);
        if(currentLatitude != 0 || currentLongitude != 0) {
            MarkerOptions options = new MarkerOptions()
                    .position(latLng)
                    .title("I am here!");
            googleMap.addMarker(options);
            googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
        }
    }

    public void setgMap(GoogleMap gMap) {
        this.gMap = gMap;
    }
}

在重构之前,我已将所有代码放在BreakDownOnMaps.java中。一切都运行良好。因此,如果您想通过以下链接查看它之前的样子:

链接:http://pastebin.com/p6c3UeSC

我还听说重构的艺术不是添加代码,而只是改变代码的方式。所以我真的不知道自己做错了什么。

0 个答案:

没有答案