昨天解决了this problem之后,我的代码中不再有语法错误。但是,当我在手机上启动应用程序时,它立即崩溃。我不确定错误是从哪里来的,如果我发布所有内容,我认为它是太多代码,因此我将首先发布logcat,如果有人可以缩小错误范围,我将发布代码的相应部分。
06-03 19:18:34.288: D/AndroidRuntime(22511): Shutting down VM
06-03 19:18:34.288: W/dalvikvm(22511): threadid=1: thread exiting with uncaught exception (group=0x41532ba8)
06-03 19:18:34.288: E/AndroidRuntime(22511): FATAL EXCEPTION: main
06-03 19:18:34.288: E/AndroidRuntime(22511): Process: com.axelfiedler.stayup, PID: 22511
06-03 19:18:34.288: E/AndroidRuntime(22511): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.axelfiedler.stayup/com.axelfiedler.stayup.MainActivity}: java.lang.IllegalStateException: Not connected. Call connect() and wait for onConnected() to be called.
06-03 19:18:34.288: E/AndroidRuntime(22511): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-03 19:18:34.288: E/AndroidRuntime(22511): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-03 19:18:34.288: E/AndroidRuntime(22511): at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-03 19:18:34.288: E/AndroidRuntime(22511): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-03 19:18:34.288: E/AndroidRuntime(22511): at android.os.Handler.dispatchMessage(Handler.java:102)
06-03 19:18:34.288: E/AndroidRuntime(22511): at android.os.Looper.loop(Looper.java:136)
06-03 19:18:34.288: E/AndroidRuntime(22511): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-03 19:18:34.288: E/AndroidRuntime(22511): at java.lang.reflect.Method.invokeNative(Native Method)
06-03 19:18:34.288: E/AndroidRuntime(22511): at java.lang.reflect.Method.invoke(Method.java:515)
06-03 19:18:34.288: E/AndroidRuntime(22511): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-03 19:18:34.288: E/AndroidRuntime(22511): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-03 19:18:34.288: E/AndroidRuntime(22511): at dalvik.system.NativeStart.main(Native Method)
06-03 19:18:34.288: E/AndroidRuntime(22511): Caused by: java.lang.IllegalStateException: Not connected. Call connect() and wait for onConnected() to be called.
06-03 19:18:34.288: E/AndroidRuntime(22511): at com.google.android.gms.internal.ff.bT(Unknown Source)
06-03 19:18:34.288: E/AndroidRuntime(22511): at com.google.android.gms.internal.hc.a(Unknown Source)
06-03 19:18:34.288: E/AndroidRuntime(22511): at com.google.android.gms.internal.hc$c.bT(Unknown Source)
06-03 19:18:34.288: E/AndroidRuntime(22511): at com.google.android.gms.internal.hb.getLastLocation(Unknown Source)
06-03 19:18:34.288: E/AndroidRuntime(22511): at com.google.android.gms.internal.hc.getLastLocation(Unknown Source)
06-03 19:18:34.288: E/AndroidRuntime(22511): at com.google.android.gms.location.LocationClient.getLastLocation(Unknown Source)
06-03 19:18:34.288: E/AndroidRuntime(22511): at com.axelfiedler.stayup.MainActivity.onCreate(MainActivity.java:223)
06-03 19:18:34.288: E/AndroidRuntime(22511): at android.app.Activity.performCreate(Activity.java:5231)
06-03 19:18:34.288: E/AndroidRuntime(22511): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-03 19:18:34.288: E/AndroidRuntime(22511): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-03 19:18:34.288: E/AndroidRuntime(22511): ... 11 more
06-03 19:19:56.736: D/AndroidRuntime(23049): Shutting down VM
06-03 19:19:56.736: W/dalvikvm(23049): threadid=1: thread exiting with uncaught exception (group=0x41532ba8)
06-03 19:19:56.736: E/AndroidRuntime(23049): FATAL EXCEPTION: main
06-03 19:19:56.736: E/AndroidRuntime(23049): Process: com.axelfiedler.stayup, PID: 23049
06-03 19:19:56.736: E/AndroidRuntime(23049): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.axelfiedler.stayup/com.axelfiedler.stayup.MainActivity}: java.lang.IllegalStateException: Not connected. Call connect() and wait for onConnected() to be called.
06-03 19:19:56.736: E/AndroidRuntime(23049): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-03 19:19:56.736: E/AndroidRuntime(23049): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-03 19:19:56.736: E/AndroidRuntime(23049): at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-03 19:19:56.736: E/AndroidRuntime(23049): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-03 19:19:56.736: E/AndroidRuntime(23049): at android.os.Handler.dispatchMessage(Handler.java:102)
06-03 19:19:56.736: E/AndroidRuntime(23049): at android.os.Looper.loop(Looper.java:136)
06-03 19:19:56.736: E/AndroidRuntime(23049): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-03 19:19:56.736: E/AndroidRuntime(23049): at java.lang.reflect.Method.invokeNative(Native Method)
06-03 19:19:56.736: E/AndroidRuntime(23049): at java.lang.reflect.Method.invoke(Method.java:515)
06-03 19:19:56.736: E/AndroidRuntime(23049): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-03 19:19:56.736: E/AndroidRuntime(23049): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-03 19:19:56.736: E/AndroidRuntime(23049): at dalvik.system.NativeStart.main(Native Method)
06-03 19:19:56.736: E/AndroidRuntime(23049): Caused by: java.lang.IllegalStateException: Not connected. Call connect() and wait for onConnected() to be called.
06-03 19:19:56.736: E/AndroidRuntime(23049): at com.google.android.gms.internal.ff.bT(Unknown Source)
06-03 19:19:56.736: E/AndroidRuntime(23049): at com.google.android.gms.internal.hc.a(Unknown Source)
06-03 19:19:56.736: E/AndroidRuntime(23049): at com.google.android.gms.internal.hc$c.bT(Unknown Source)
06-03 19:19:56.736: E/AndroidRuntime(23049): at com.google.android.gms.internal.hb.getLastLocation(Unknown Source)
06-03 19:19:56.736: E/AndroidRuntime(23049): at com.google.android.gms.internal.hc.getLastLocation(Unknown Source)
06-03 19:19:56.736: E/AndroidRuntime(23049): at com.google.android.gms.location.LocationClient.getLastLocation(Unknown Source)
06-03 19:19:56.736: E/AndroidRuntime(23049): at com.axelfiedler.stayup.MainActivity.onCreate(MainActivity.java:223)
06-03 19:19:56.736: E/AndroidRuntime(23049): at android.app.Activity.performCreate(Activity.java:5231)
06-03 19:19:56.736: E/AndroidRuntime(23049): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-03 19:19:56.736: E/AndroidRuntime(23049): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-03 19:19:56.736: E/AndroidRuntime(23049): ... 11 more
编辑:这就是我在onCreate()内调用connect()函数的方式
mLocationClient = new LocationClient(this, this, this);
mLocationClient.connect();
mCurrentLocation = mLocationClient.getLastLocation();
最佳答案
看这行:Call connect() and wait for onConnected() to be called.
我认为您是在真正连接位置客户端之前请求更新。
呼叫locationClient.connect()
时,您必须等待onConnected()
上的答案
这是您必须重写的方法。
编辑:
我在您更新的问题中看到了问题。
你必须打电话mCurrentLocation = mLocationClient.getLastLocation();
在onConnected()
中。
在您的活动或片段中,添加implements ConnectionCallbacks
,
并覆盖方法onConnected()
和onDisconnected()
。
祝好运。