【问题标题】:Android GooglePlay LocationClient is null, although initialized in onCreate()Android GooglePlay LocationClient 为空,尽管在 onCreate() 中初始化
【发布时间】:2013-08-19 05:14:16
【问题描述】:

我正在使用 Google Play LocationClient。我已经按照文档中的说明在 onCreate() 中对其进行了初始化:

mLocationClient = new LocationClient(this, this, this);

我正在 onStart() 中进行连接

mLocationClient.connect();

它在我的 Android 手机上运行良好,但在开发者控制台中,我看到 connect()line 中发生了 NullPointerException。 怎么会这样?

【问题讨论】:

  • 你能发布更多代码吗?
  • onCreate() 中发生了很多事情。初始化 TextViews、Variables、ActionBar、Fragments、... 不知道这会有什么帮助。它适用于我的设备和另一个设备
  • 发布您看到的异常的 LogCat 输出

标签: android android-maps-v2 google-play-services android-location


【解决方案1】:

这可能是因为导致 NPE 的设备没有 Google Play 服务或不是最新的。在 onCreate 你可以检查

int errorCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (errorCode != ConnectionResult.SUCCESS)
{
        if (DEBUG) {Log.d(TAG, "errorCode = " + errorCode);}
        Dialog errorDialog = GooglePlayServicesUtil.getErrorDialog(errorCode, this, 
                                1, new DialogCancelListener());
        errorDialog.show();
}

这将显示一个对话框,要求用户前往 Google 商店进行更新或安装。
在 onStart 中你需要检查 null

if (mLocationClient != null)
{
    mLocationClient.connect();
}

【讨论】:

  • 谢谢。那是真的。当 GooglePlayServices 不可用时调用 onStart(),因此发生 NullPointer
猜你喜欢
  • 2021-04-13
  • 1970-01-01
  • 1970-01-01
  • 2021-08-25
  • 1970-01-01
  • 1970-01-01
  • 2019-07-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多