Reputation: 1
I've added a check of the version to my application through xml parsing.
But when I try to execute it, it runs in the exception that I've pasted down there:
11-03 19:21:36.809: E/AndroidRuntime(16531): FATAL EXCEPTION: main
11-03 19:21:36.809: E/AndroidRuntime(16531): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.lookedpath.firstlesson/com.lookedpath.firstlesson.Update}: android.os.NetworkOnMainThreadException
11-03 19:21:36.809: E/AndroidRuntime(16531): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2099)
11-03 19:21:36.809: E/AndroidRuntime(16531): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2210)
11-03 19:21:36.809: E/AndroidRuntime(16531): at android.app.ActivityThread.access$600(ActivityThread.java:142)
11-03 19:21:36.809: E/AndroidRuntime(16531): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1208)
11-03 19:21:36.809: E/AndroidRuntime(16531): at android.os.Handler.dispatchMessage(Handler.java:99)
11-03 19:21:36.809: E/AndroidRuntime(16531): at android.os.Looper.loop(Looper.java:137)
11-03 19:21:36.809: E/AndroidRuntime(16531): at android.app.ActivityThread.main(ActivityThread.java:4931)
11-03 19:21:36.809: E/AndroidRuntime(16531): at java.lang.reflect.Method.invokeNative(Native Method)
11-03 19:21:36.809: E/AndroidRuntime(16531): at java.lang.reflect.Method.invoke(Method.java:511)
11-03 19:21:36.809: E/AndroidRuntime(16531): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
11-03 19:21:36.809: E/AndroidRuntime(16531): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
11-03 19:21:36.809: E/AndroidRuntime(16531): at dalvik.system.NativeStart.main(Native Method)
11-03 19:21:36.809: E/AndroidRuntime(16531): Caused by: android.os.NetworkOnMainThreadException
11-03 19:21:36.809: E/AndroidRuntime(16531): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
11-03 19:21:36.809: E/AndroidRuntime(16531): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
11-03 19:21:36.809: E/AndroidRuntime(16531): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
11-03 19:21:36.809: E/AndroidRuntime(16531): at java.net.InetAddress.getAllByName(InetAddress.java:214)
11-03 19:21:36.809: E/AndroidRuntime(16531): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
11-03 19:21:36.809: E/AndroidRuntime(16531): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
11-03 19:21:36.809: E/AndroidRuntime(16531): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
11-03 19:21:36.809: E/AndroidRuntime(16531): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
11-03 19:21:36.809: E/AndroidRuntime(16531): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
11-03 19:21:36.809: E/AndroidRuntime(16531): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
11-03 19:21:36.809: E/AndroidRuntime(16531): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
11-03 19:21:36.809: E/AndroidRuntime(16531): at com.lookedpath.firstlesson.XMLParser.getXmlFromUrl(XMLParser.java:35)
11-03 19:21:36.809: E/AndroidRuntime(16531): at com.lookedpath.firstlesson.Update.<init>(Update.java:24)
11-03 19:21:36.809: E/AndroidRuntime(16531): at java.lang.Class.newInstanceImpl(Native Method)
11-03 19:21:36.809: E/AndroidRuntime(16531): at java.lang.Class.newInstance(Class.java:1319)
11-03 19:21:36.809: E/AndroidRuntime(16531): at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
11-03 19:21:36.809: E/AndroidRuntime(16531): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2090)
11-03 19:21:36.809: E/AndroidRuntime(16531): ... 11 more
You can get the Eclipse project by using GitHub: https://github.com/LookedPath/lookedpath_android_applications/tree/FirstApp/PrimaLezione
What should I do?
Upvotes: 0
Views: 344
Reputation: 954
The best way is use an AsyncTask.
http://developer.android.com/reference/android/os/AsyncTask.html
Async task lets u to interact with the UI when u fiish download, thread (or runnable) don't let you to interact with the UI.
If u need to make changes in your UI, you needs to create a handler and run changes in that handler, but AsyncTask implements it for you.
Upvotes: 3
Reputation: 1228
You are getting a NetworkOnMainThreadException
. That means you're trying to access the network in some way (downloading an image, for example) in your main (UI) thread. This was disabled for better responsiveness to the user interface in API 11. You need to use a different thread for your networking, the easiest way to implement this is with an AsyncTask
. If you need to remain connected for a while (for something such as live communication), your could also create a new class which extends Thread
or HandlerThread
and manage communication between that and the UI thread yourself (with Handler
). Don't forget the UI is not thread-safe.
Upvotes: 0