onlocationchanged nullpointerexception但位置不为null

问题描述 投票:0回答:1

在扩展片段并实现LocationListener的myFragment类中调用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吗?以及如何避免这种错误?

android nullpointerexception gps location
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.