onlocationchanged nullpointerexception但location不为null

时间:2014-02-18 19:55:54

标签: android nullpointerexception gps location

在myFragment类中调用onLocationChanged时,我得到nullpointerexception,它扩展了Fragment并实现了LocationListener

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?我该如何避免这种错误?

1 个答案:

答案 0 :(得分: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获取当前的纬度,经度。