简体   繁体   English

Android:在Android模拟器中请求位置更新后,应用停止运行

[英]Android: App stops working after requesting location updates in android simulator

My app stopped working after requesting location updates in android emulator. 在android模拟器中请求位置更新后,我的应用停止了工作。 This happens only for android versions over 3.0. 仅适用于3.0以上的android版本。 Any code like the following 任何类似以下的代码

lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

the app stops working. 该应用程序停止工作。 Is this a regular behavior in simulator for these versions or am I doing something wrong? 这是这些版本在模拟器中的正常行为,还是我做错了什么?

More info: If I launch the simulator for android 2.3 or less, the exceptions are being caught properly. 更多信息:如果我为Android 2.3或更低版本启动模拟器,则可以正确捕获异常。 This is not happening in the other versions. 在其他版本中不会发生这种情况。

 12-27 19:17:59.183: W/dalvikvm(640): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
 12-27 19:17:59.253: E/AndroidRuntime(640): FATAL EXCEPTION: main
 12-27 19:17:59.253: E/AndroidRuntime(640): android.os.NetworkOnMainThreadException
 12-27 19:17:59.253: E/AndroidRuntime(640):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
 12-27 19:17:59.253: E/AndroidRuntime(640):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
 12-27 19:17:59.253: E/AndroidRuntime(640):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
 12-27 19:17:59.253: E/AndroidRuntime(640):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
 12-27 19:17:59.253: E/AndroidRuntime(640):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
 12-27 19:17:59.253: E/AndroidRuntime(640):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
 12-27 19:17:59.253: E/AndroidRuntime(640):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
 12-27 19:17:59.253: E/AndroidRuntime(640):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
 12-27 19:17:59.253: E/AndroidRuntime(640):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
 12-27 19:17:59.253: E/AndroidRuntime(640):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
 12-27 19:17:59.253: E/AndroidRuntime(640):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
 12-27 19:17:59.253: E/AndroidRuntime(640):     at com.rahul.places.AddNewLocation.getUrlArray(AddNewLocation.java:147)
 12-27 19:17:59.253: E/AndroidRuntime(640):     at com.rahul.places.AddNewLocation.access$6(AddNewLocation.java:139)
 12-27 19:17:59.253: E/AndroidRuntime(640):     at com.rahul.places.AddNewLocation$2.onClick(AddNewLocation.java:69)
 12-27 19:17:59.253: E/AndroidRuntime(640):     at android.view.View.performClick(View.java:4084)
 12-27 19:17:59.253: E/AndroidRuntime(640):     at android.view.View$PerformClick.run(View.java:16966)
 12-27 19:17:59.253: E/AndroidRuntime(640):     at android.os.Handler.handleCallback(Handler.java:615)
 12-27 19:17:59.253: E/AndroidRuntime(640):     at android.os.Handler.dispatchMessage(Handler.java:92)
 12-27 19:17:59.253: E/AndroidRuntime(640):     at android.os.Looper.loop(Looper.java:137)
 12-27 19:17:59.253: E/AndroidRuntime(640):     at android.app.ActivityThread.main(ActivityThread.java:4745)
 12-27 19:17:59.253: E/AndroidRuntime(640):     at java.lang.reflect.Method.invokeNative(Native Method)
 12-27 19:17:59.253: E/AndroidRuntime(640):     at java.lang.reflect.Method.invoke(Method.java:511)
 12-27 19:17:59.253: E/AndroidRuntime(640):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
 12-27 19:17:59.253: E/AndroidRuntime(640):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
 12-27 19:17:59.253: E/AndroidRuntime(640):     at dalvik.system.NativeStart.main(Native Method)

This is expected. 这是预期的。 Android is much more strict in dealing with work on the UI thread in more recent versions. 在处理最新版本的UI线程方面,Android更加严格。

The solution is to use a handler to change the UI on the UI thread when location updates are available (that is in your onLocationChanged() method). 解决方案是在位置更新可用时(即在onLocationChanged()方法中)使用处理程序更改UI线程上的UI。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM