简体   繁体   中英

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

My app stopped working after requesting location updates in android emulator. This happens only for android versions over 3.0. 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. 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.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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