android - 使用 GPS 的 Android 应用程序中的 NullPointerException

标签 android gps nullpointerexception

我有以下非常简单的类,它查找最后已知的用户坐标,然后打印其中之一。但是我将 loc 作为空变量。应用程序未将数据读入 loc 变量。

    import android.app.Activity;
    import android.content.Context;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.os.Bundle;
    import android.widget.Toast;

    public class Navigation extends Activity
    {
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState)
      {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        /* Use the LocationManager class to obtain GPS locations */
        try
        {
            LocationManager mlocManager = 
              (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            Toast.makeText(getBaseContext(), 
                  "Provider Enable Status : " + mlocManager.isProviderEnabled(                       
                   mlocManager.getProvider("gps").getName()),     
                   Toast.LENGTH_LONG).show();

            Location loc = mlocManager.getLastKnownLocation("gps");
            Toast.makeText( getApplicationContext(), "" + 
                loc.getLatitude(),  Toast.LENGTH_LONG).show();
            Toast.makeText( getApplicationContext(), "" + loc,
                Toast.LENGTH_LONG).show();
         } catch (Exception e) {
           Toast.makeText(getBaseContext(), e.getMessage() + 
             e.toString(), Toast.LENGTH_LONG).show();
         }
      }
   }

我在代码中添加了以下监听器:

        LocationListener mlocListener = new MyLocationListener();

        mlocManager.requestLocationUpdates( mlocManager.getProvider("gps").getName(), 0, 0, mlocListener);

在 oncreate 函数中。和下面的类

public class MyLocationListener implements LocationListener {
    @Override
    public void onLocationChanged(Location loc) {
        Toast.makeText( getApplicationContext(),
            "Hi, location changed. :)", Toast.LENGTH_LONG).show();
        try {
        loc.getLatitude();
        loc.getLongitude();
        String Text = "My current location is: " +
                " Latitude = " + loc.getLatitude() +
                " Longitude = " + loc.getLongitude();
        Toast.makeText(getApplicationContext(),
                Text, Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            Toast.makeText(getBaseContext(), 
                e.getMessage(), Toast.LENGTH_LONG).show();
        }
    }
}

运行窗口后,我执行以下操作: 转到 Eclipse 中的 DDMS 并设置纬度/经度,然后单击发送。

它仍然不会在函数中触发任何 toast 命令。

最佳答案

如果您阅读 manual ,它明确表示该方法返回“提供者的最后一个已知位置,或 null”。

我不是 Android 开发者,但我猜它会在 GPS 冷启动时返回 null,直到修复完成。在继续之前,您必须检查 loc 是否为 !=null。您可能会尝试等待一段时间或使用异步回调。

希望对您有所帮助。

关于android - 使用 GPS 的 Android 应用程序中的 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4164094/

相关文章:

android - GPS通知 "Location set by gps"

ios - 在 ios 设备重启后重启 GPS

java - 通过访问 JButton 数组中的对象出现 NullPointerException

java - 无法在 Java 中输出正确的散列。怎么了?

android - Necessitas QT - 在设备上安装共享 QT 库

java - 安卓Java : Turn screen Off

binary - GPS 纬度和经度(十六进制值)

android - 如何在 Kotlin 中保存延迟发帖的状态

java - 从类对象的方法接收空指针异常

java - 什么是NullPointerException,我该如何解决?