2014-02-18 39 views
0

我得到NullPointerException異常時onLocationChanged被稱爲延伸片段和implemets myFragment類LocationListener的onlocationchanged NullPointerException異常,但位置不是空

java.lang.NullPointerException 
at com.mypackage.MyFragment.onLocationChanged(myFragment.java:616) 
at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:237) 
at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:170) 
at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:186) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
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:1008) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775) 
at dalvik.system.NativeStart.main(Native Method) 

這是我使用的代碼:

@Override 
    public void onActivityCreated(Bundle savedInstanceState) { 

     super.onActivityCreated(savedInstanceState); 

      locationManager = (LocationManager) getSherlockActivity().getSystemService(Context.LOCATION_SERVICE); 

      Criteria criteria = new Criteria(); 

      provider = locationManager.getBestProvider(criteria, true); 

      // Getting Current Location 
     if (provider !=null) 
      { 
       location = locationManager.getLastKnownLocation(provider); 
       if(location!=null) 
       { 
       onLocationChanged(location); 
       } 

       locationManager.requestLocationUpdates(provider, 0, 0,this);  

      } 
      navigatorBtn.setOnClickListener(new OnClickListener(){ 
      @Override 
    public void onClick(View arg0) { 

       if(provider!=null) 
       { 
        showNav(locationLat,locationLong,destinationLat,destinationLong); 
       } 
       else 
       { 
        //show error msg 
       } 
      }}); 
} 


@Override 
    public void onLocationChanged(Location arg0) { 
     // TODO Auto-generated method stub 
      if (provider !=null) 
      { 
       // Getting latitude of the current location 
       locationLat= location.getLatitude(); 

       // Getting longitude of the current location 
       locationLong= location.getLongitude(); 

      }  
    } 

public void showNav(double fromLat, double fromLong, double toLat, double toLong) 
{  
    Uri uri =Uri.parse("http://maps.google.com/maps?&saddr="+fromLat+","+fromLong+"&daddr="+toLat+","+toLong); 
    Intent intent = new Intent(Intent.ACTION_VIEW, uri); 
    startActivity(intent); 

} 

該片段正常打開,工作正常,但有時它崩潰給空指針。可能是什麼問題?它可能是由於GPS?我怎樣才能避免這樣的錯誤?

回答

0

更改onLocationChanged爲:用於獲取當前的經緯度

public void onLocationChanged(Location arg0) { 
    // TODO Auto-generated method stub 
     if (provider !=null) 
     { 
      // Getting latitude of the current location 
      locationLat= arg0.getLatitude(); 

      // Getting longitude of the current location 
      locationLong= arg0.getLongitude(); 

     }  
} 

使用onLocationChanged方法參數arg0

+0

謝謝我會試試這個 – ukama

相關問題