0

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?

2
  • See the documentation for NetworkOnMainThreadException Commented Nov 3, 2012 at 18:33
  • 1
    Please show the relevant parts of your code. Just linking to a code repository in not enough... Commented Nov 3, 2012 at 18:34

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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 HandlerThreadand manage communication between that and the UI thread yourself (with Handler). Don't forget the UI is not thread-safe.

6 Comments

I changed my Update.java with this: pastebin.com/LUQp64us But i got a strange error: "Skipped xx frames! the application may be doing too much work on its main thread" like if the app generated a lot of load for the cpu...
Try putting connect() into the AsyncTask's class instead of your main one and see if that helps.
the errors are gone but the applications opens a blank layout instead of updating the textview as it is supposed to do...
The android UI is not thread-safe, so you can only update your TextView from the main thread. You should use onPostExecute() in the AsyncTask to update it, since that runs on the UI thread. It is passed whatever is returned from doInBackground().
Ok thanks for your help... Now i completely rewrote the Update class but I'm still getting an exception when i launch goToUpdate void... MainActivity.java: pastebin.com/9HYH5hfi Update.java: pastebin.com/ukzX2sYb XMLParser.java: pastebin.com/2MuNmfp6 Exception: pastebin.com/fcNj0i1q
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.