2016-01-18 40 views
-1

由於某種原因,每隔一段時間我編譯我的應用程序,應用程序崩潰,我得到以下錯誤。Android應用程序崩潰每隔一個編譯/啓動

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.clear()' on a null object reference 
     at com.mycompany.talk.Fragments.MapFragment$1.onLocationChanged(MapFragment.java:68) 

我不知道如何解決這個錯誤,也沒有什麼導致它。它在一些編譯和啓動時工作正常,但在其他應用程序中崩潰。該錯誤發生在我的「MapFragment」類中。

onLocationChanged(Location)方法。

onLocationChanged()

 @Override 
     public void onLocationChanged(Location location) { 
      if (location != null) { 

       /* 
       Updates to our map may need to be taken place here. Need to listen to other devices in the area. 
       */ 

       Log.e("Latitude: ", "" + location.getLatitude()); 
       Log.e("Longitude: ", "" + location.getLongitude()); 
       maps.clear(); //Clear the map of any existing markers 
       mLatLng = new LatLng(location.getLatitude(),location.getLongitude()); 
       MarkerOptions mp1 = new MarkerOptions();//Instantiate a new "MarkerOptions" where we will be able to define a... 
                 //...marker 

       mp1.position(mLatLng); 
       mp1.title("You"); 
       maps.addMarker(mp1);//Finally add the marker to the map 
       maps.moveCamera(CameraUpdateFactory.newLatLng(mLatLng));//Move camera to markers location using our "latLng" variable 
       maps.animateCamera(CameraUpdateFactory.zoomTo(20));// Zoom, (between 2.0 - 21.0) the higher, the more zoomed in 

      } 
     } 

MapFragment.java

import android.content.Context; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

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; 
import com.mycompany.talk.R; 


public class MapFragment extends Fragment implements OnMapReadyCallback{ 

SupportMapFragment mSupportMapFragment; 
int radius = 20; 
private GoogleMap maps; 
LatLng mLatLng; 
LocationManager mLocationManager; 
LocationListener mLocationListener; 

public LatLng getLatLng(){ 
    return mLatLng; 
} 

@Override 
public void onStart(){ 
    super.onStart(); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    mSupportMapFragment = SupportMapFragment.newInstance(); 
    android.support.v4.app.FragmentManager sfm = getFragmentManager(); 
    mSupportMapFragment.getMapAsync(this); 
    if(!mSupportMapFragment.isAdded()) 
    sfm.beginTransaction().add(R.id.map_frag,mSupportMapFragment).commit(); 

    else if(mSupportMapFragment.isAdded()) 
     sfm.beginTransaction().hide(mSupportMapFragment).commit(); 
    else 
     sfm.beginTransaction().show(mSupportMapFragment).commit(); 

    //LocationManager mLocationManager; 
    // LocationListener mLocationListener; 
    mLocationListener = new LocationListener() { 
     @Override 
     public void onLocationChanged(Location location) { 
      if (location != null) { 

       /* 
       Updates to our map may need to be taken place here. Need to listen to other devices in the area. 
       */ 

       Log.e("Latitude: ", "" + location.getLatitude()); 
       Log.e("Longitude: ", "" + location.getLongitude()); 

       if(maps != null) { 
        maps.clear(); //Clear the map of any existing markers 
       } 
       mLatLng = new LatLng(location.getLatitude(),location.getLongitude());//This is an Update!!!!!!!!! Might not work!!!!! 
       MarkerOptions mp1 = new MarkerOptions();//Instantiate a new "MarkerOptions" where we will be able to define a... 
                 //...marker 

       mp1.position(mLatLng);//This is an Update!!!!!!!!! Might not work!!!!! 
       mp1.title("You"); 
       maps.addMarker(mp1);//Finally add the marker to the map 
       maps.moveCamera(CameraUpdateFactory.newLatLng(mLatLng));//Move camera to markers location using our "latLng" variable 
       maps.animateCamera(CameraUpdateFactory.zoomTo(20));// Zoom, (between 2.0 - 21.0) the higher, the more zoomed in 

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

     } 
     @Override 
     public void onProviderEnabled(String s) { 

     } 
     @Override 
     public void onProviderDisabled(String s) { 

     } 
    }; 

    mLocationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE); 
    mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,mLocationListener);//THIS IS AN UPDATE! 
    //Need more precise coordinates.............. 
    return inflater.inflate(R.layout.fragment_map, container, false); 
} 

@Override 
public void onMapReady(GoogleMap map) { 
    maps = map; 
} 

@Override 
public void onResume(){ 
    Log.e("RESUME","onResume called"); 
    super.onResume(); 
} 
@Override 
public void onStop(){ 
    mLocationManager.removeUpdates(mLocationListener); 
    Log.e("PAUSE", "Coordinates stopped"); 
    super.onStop(); 
} 

@Override 
public void onPause(){ 
    mLocationManager.removeUpdates(mLocationListener); 
    Log.e("PAUSE", "Coordinates stopped"); 
    super.onPause(); 
} 
} 
+0

其中是在onLocationChanged中定義的地圖,它應該像私人Google Maps = new Maps();或者你想使用的任何構造函數 – therealprashant

+0

也許在地圖準備就緒之前位置發生了變化。 – Titus

+0

我相信它實際上是作爲一個全局實例化的,並且在「onMapReady(GoogleMap map)」中定義了「@therealprashant – Jcan1995

回答

1

maps被初始化,並在兩個不同的回調方法onLocationChanged(...)onMapReady(...)訪問,這意味着如果該映射之前的位置的變化是就緒(onLocationChanged(...)之前調用onMapReady(...)),mapsnull。要解決這個問題,你可以在訪問之前檢查它是否爲null。例如:

@Override 
public void onLocationChanged(Location location) { 
    if (location != null && maps != null) { 
     .... 
    } 
} 
+0

謝謝。這似乎解決了問題。我在調用MapReady(..)時感到困惑。 – Jcan1995