【问题标题】:Not connected. Call Connect or wait for onConnected() to be called未连接。调用 Connect 或等待 onConnected() 被调用
【发布时间】:2023-03-09 13:29:01
【问题描述】:

我的应用以地图为中心。我经常打电话给requestLocationUpdates()。偶尔,当它被调用时,我会得到这个exception。或者在任何其他调用这种方法的地方。我在 SOF 上看到了建议的解决方案,遗憾的是似乎没有什么对我有用。即使我打电话给mLocationClient.connect(),也不能保证它会立即连接,如果我错了,请纠正我。我该如何解决这个问题?

案例 R.id.btnContinue:

gpsLocationDailog.cancel();

        int isGooglePlayServiceAvilable = GooglePlayServicesUtil
                .isGooglePlayServicesAvailable(getApplicationContext());
        if (isGooglePlayServiceAvilable == ConnectionResult.SUCCESS) {
            /** thorws shitty expectiosn **/
            mLocationClient.connect();
            try {
                mLocationClient.requestLocationUpdates(REQUEST, this);
                mCurrentLocation = mLocationClient.getLastLocation();
                fireQueryToGetTheResponse(latitude, longitude);
                rl.setVisibility(View.VISIBLE);
                mDrawerLayout.setVisibility(View.GONE);
                mSlidingUpPanelLayout.setVisibility(View.GONE);
            } catch (IllegalStateException e) {
                mLocationClient.connect();
                Log.e(TAG, " Waiting for onConnect to be called");
            }
        } else {
            GooglePlayServicesUtil.getErrorDialog(
                    isGooglePlayServiceAvilable,
                    SqueakeeMapListViewPager.this, 0).show();
        }

        break;

这是引发的异常:

java.lang.IllegalStateException: Not connected. Call connect() and wait for onConnected() to be called.
at com.google.android.gms.internal.de.bc(Unknown Source)
at com.google.android.gms.internal.ez.a(Unknown Source)
at com.google.android.gms.internal.ez$c.bc(Unknown Source)
at com.google.android.gms.internal.ey.requestLocationUpdates(Unknown Source)
at com.google.android.gms.internal.ez.requestLocationUpdates(Unknown Source)
at com.google.android.gms.internal.ez.requestLocationUpdates(Unknown Source)
at com.google.android.gms.location.LocationClient.requestLocationUpdates(Unknown Source)
at org.application.app.squeakee.SqueakeeMapListViewPager.onClick(SqueakeeMapListViewPager.java:1936)
at android.view.View.performClick(View.java:4475)
at android.view.View$PerformClick.run(View.java:18786)
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:5493)
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:1209)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
at dalvik.system.NativeStart.main(Native Method)

【问题讨论】:

    标签: android exception-handling illegalstateexception


    【解决方案1】:

    我遇到了类似的错误,基本上,上一条评论中所说的内容将解决您的问题。 如果你确实追踪了你的错误,它会在这里报告:

    mLocationClient.requestLocationUpdates(REQUEST, this);
    

    原因是当您调用该 API 方法时客户端未连接。这就是为什么前面的答案可以正常工作的原因,因为基本上他所做的就是在回调中启动位置更新。

    当你这样做时:

    mLocationClient.connect();
    

    您的客户端不会立即连接,但代码会继续运行,当您询问 locationUpdates 时,您还没有连接(它仍在处理它,需要时间),所以它崩溃了。此外,当你执行 .connect() 时,你基本上会激活 onConnected() 回调,所以如果你在其中编写你的 locationUpdates 请求,那么你基本上会解决你的大部分错误。

    另外,你说你有很多请求,只是有时会出现这个错误。为此,您没有提供足够的代码,但我想我可以假设错误发生的原因是/当您取消请求并重新激活它们(您的方法 onStop()、onPause()、onResume() 和开始())。当你暂停你的应用程序时,你应该断开客户端,但你应该确保当你恢复它时,你再次重新连接它,所以也许只是这样做

    mLocationClient.connect();
    

    在 onResume() 内部,这可能会解决您的一些其他问题(再一次,您没有提供该代码,也许您已经正确编码,但对于其他有此问题的读者,这可能是修复它的建议)。

    回答有点晚了,但希望这对社区的其他人有所帮助。

    最好的问候。

    【讨论】:

    • 这类解释总是很受欢迎。谢谢!
    • 我在同一行代码中遇到了这个错误,即使它在 onConnected() 方法中也是如此。你怎么看?
    【解决方案2】:

    尝试在您的 Activity 上实现 GooglePlayServicesClient.ConnectionCallbacks 和 @Override onConnected() 并将所有需要连接的代码放入其中。

    这对我有用。

    【讨论】:

      猜你喜欢
      • 2014-05-26
      • 2016-05-19
      • 2014-04-13
      • 2013-12-14
      • 1970-01-01
      • 2019-07-07
      • 2013-10-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多