0

我通过 xml 解析向我的应用程序添加了版本检查。

但是当我尝试执行它时,它会在我粘贴在那里的异常中运行:

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

您可以使用 GitHub 获取 Eclipse 项目: https ://github.com/LookedPath/lookedpath_android_applications/tree/FirstApp/PrimaLezione

我应该怎么办?

4

2 回答 2

3

最好的方法是使用 AsyncTask。

http://developer.android.com/reference/android/os/AsyncTask.html

异步任务允许您在完成下载时与 UI 交互,线程(或可运行)不允许您与 UI 交互。

如果您需要在 UI 中进行更改,您需要创建一个处理程序并在该处理程序中运行更改,但 AsyncTask 会为您实现它。

于 2012-11-04T18:11:27.913 回答
0

你得到一个NetworkOnMainThreadException. 这意味着您正尝试在主 (UI) 线程中以某种方式访问​​网络(例如下载图像)。这是为了更好地响应 API 11 中的用户界面而禁用的。您需要为网络使用不同的线程,最简单的实现方法是使用AsyncTask. 如果您需要保持连接一段时间(例如实时通信),您还可以创建一个新类来扩展ThreadHandlerThread管理该类与您自己的 UI 线程之间的通信(使用Handler)。不要忘记 UI 不是线程安全的。

于 2012-11-03T18:46:05.737 回答