【发布时间】:2015-01-09 07:38:12
【问题描述】:
我收到以下错误:
Process: com.example.myweatherapp.myweatherapp, PID: 1263
java.lang.IllegalStateException: Could not execute method of the activity
Caused by: android.os.NetworkOnMainThreadException
在尝试运行我的应用程序时。我的应用应该是显示来自 XML 的数据。 XML 在网络上,所以我必须首先通过将 URL 放在那里来获取它。在此之前,我会检查手机是否有 Internet 连接。
If yes, then proceed with fetchXML and when done, store it also in /res/raw/dataxml .
If not, then start handlingStoredXML (I have put the file in /res/raw/dataxml).
当我尝试运行应用程序时,它会崩溃。
MainActivity 类:link to fiddle MainActivity code
下载XML类: link to fiddle DownloadXML code
抱歉,我不知道有任何其他来源可以轻松存储我的代码并在此处分享。不过太大了,这里就不复制了。
希望有人可以帮助我。我在这里浏览过它,遇到了 AsyncTask,但我不知道如何在我的代码中实现它。
LOGCAT:
11-12 13:04:09.538 1263-1263/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.myweatherapp.myweatherapp, PID: 1263
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:4007)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4002)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
at java.net.InetAddress.getAllByName(InetAddress.java:215)
at com.android.okhttp.HostResolver$1.getAllByName(HostResolver.java:29)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:232)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:124)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:272)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:373)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106)
at com.example.myweatherapp.myweatherapp.DownloadXML.fetchXML(DownloadXML.java:66)
at com.example.myweatherapp.myweatherapp.HandleXML.handlingOnlineXML(HandleXML.java:55)
at com.example.myweatherapp.myweatherapp.DownloadXML.checkInetConnection(DownloadXML.java:37)
at com.example.myweatherapp.myweatherapp.MainActivity.ButtonClick(MainActivity.java:39)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4002)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
【问题讨论】:
标签: java android exception android-activity illegalstateexception