初始化后LocationManager为null

时间:2014-02-19 11:52:00

标签: android nullpointerexception android-activity locationmanager android-location

我尝试初始化LocationManager并使用Button来获取屏幕上获得的Toast位置,但我发现LocationManager为空:

/ MyLocationTest:无法初始化LocationManager

java.lang.NullPointerException
        at com.example.mylocationtest.MainActivity$1.onClick(MainActivity.java:46)
        at android.view.View.performClick(View.java:4240)
        at android.view.View$PerformClick.run(View.java:17721)
        at android.os.Handler.handleCallback(Handler.java:730)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5103)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)

第一行是我打印到Logcat的邮件,因为LocationManager为空,即使我在onCreate中将其初始化为:

  locMan=(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
  //set a click listener for the button:
  btn_loc.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v)
    {
       boolean isGPSReady,isNetworkReady;
       if(locMan==null)
       {
          Log.d("MyLocationTest", "Failed to initialize LocationManager");
       }
       isGPSReady=locMan.isProviderEnabled(LocationManager.GPS_PROVIDER);
       isNetworkReady=locMan.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
       if(!isNetworkReady && !isGPSReady)
       {
         Toast.makeText(getBaseContext(),"Sorry,location could not be enabled",Toast.LENGTH_SHORT).show();
       }
       else if(isNetworkReady)
       {
        //the third parameter is a looper that controls where the callback happens from...
              locMan.requestSingleUpdate(LocationManager.NETWORK_PROVIDER,locEar_network, null);
        latitude=loc.getLatitude();
        longitude=loc.getLongitude();
        Toast.makeText(getBaseContext(),"The latitude is "+latitude+" and the longitude is "+longitude+" and the accuracy is "+loc.getAccuracy(),Toast.LENGTH_SHORT).show();
       }
       else if(isGPSReady)
       {
           locMan.requestSingleUpdate(LocationManager.GPS_PROVIDER,locEar_gps,null);
           latitude=loc.getLatitude();
           longitude=loc.getLongitude();
           Toast.makeText(getBaseContext(),"The latitude is "+latitude+" and the longitude is "+longitude+ "and the accuracy is "+loc.getAccuracy(),Toast.LENGTH_SHORT).show();
       }

     }
  });

在我的清单中,我使用以下内容确保API级别9是requestSingleUpdate的minSDKVersion,并允许应用访问精确位置:

  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mylocationtest" >m
<uses-sdk android:minSdkVersion="9"
          android:targetSdkVersion="18"/>

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

编辑:

The entire class is here as a pastebin

如何确保LocationManager永远不为空?

2 个答案:

答案 0 :(得分:1)

如果位置管理器尝试从网络提供商处获取位置,有时可能没有网络提供商提供的位置,或者在这些情况下可能需要一些时间来获取位置,您可能会获得空位。

如果gps coudnt很可能在建筑物内找到任何位置

,位置管理器也可能会为空

答案 1 :(得分:1)

如果LocationManager为空,则只设置日志。当它也为null时,你应该避免访问它。像这样修改你的代码。

   boolean isGPSReady,isNetworkReady;
   if(locMan==null)
   {
      Log.d("MyLocationTest", "Failed to initialize LocationManager");
   }
   else{
       isGPSReady=locMan.isProviderEnabled(LocationManager.GPS_PROVIDER);
       isNetworkReady=locMan.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

       if(!isNetworkReady && !isGPSReady)
       {
            Toast.makeText(getBaseContext(),"Sorry,location could not be enabled",Toast.LENGTH_SHORT).show();
       }
       else if(isNetworkReady)
       {
            //the third parameter is a looper that controls where the callback happens from...
            locMan.requestSingleUpdate(LocationManager.NETWORK_PROVIDER,locEar_network, null);
            latitude=loc.getLatitude();
            longitude=loc.getLongitude();
            Toast.makeText(getBaseContext(),"The latitude is "+latitude+" and the longitude is "+longitude+" and the accuracy is "+loc.getAccuracy(),Toast.LENGTH_SHORT).show();
       }
       else if(isGPSReady)
       {
           locMan.requestSingleUpdate(LocationManager.GPS_PROVIDER,locEar_gps,null);
           latitude=loc.getLatitude();
           longitude=loc.getLongitude();
           Toast.makeText(getBaseContext(),"The latitude is "+latitude+" and the longitude is "+longitude+ "and the accuracy is "+loc.getAccuracy(),Toast.LENGTH_SHORT).show();
       }
   }

还有一件事,您是否使用支持GPS的设备进行测试?如果您需要更多信息refer this

尝试将这两个权限也添加到AndroidManifest.xml文件中。

INTERNET

ACCESS_COARSE_LOCATION