Android:Android在Android模拟器中请求位置更新后停止工作

时间:2012-12-27 13:55:25

标签: android location android-4.0-ice-cream-sandwich android-3.0-honeycomb

我的应用在Android模拟器中请求位置更新后停止工作。这仅适用于3.0以上的Android版本。 任何代码如下

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

该应用停止工作。这是在这些版本的模拟器中的常规行为还是我做错了什么?

更多信息:如果我为Android 2.3或更低版本启动模拟器,则会正常捕获异常。这种情况在其他版本中没有发生。

 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)

1 个答案:

答案 0 :(得分:0)

这是预期的。在更新的版本中,Android处理UI线程的工作要严格得多。

解决方案是在位置更新可用时(在onLocationChanged()方法中)使用handler更改UI线程上的UI。