应用程序崩溃:IllegalStateException错误

时间:2014-11-12 12:36:57

标签: java android exception android-activity illegalstateexception

我收到以下错误:

Process: com.example.myweatherapp.myweatherapp, PID: 1263
    java.lang.IllegalStateException: Could not execute method of the activity
Caused by: android.os.NetworkOnMainThreadException

尝试运行我的应用程序时。我的应用程序应该是显示来自XM L的数据。 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).

当我尝试运行App时,它会崩溃。

MainActivity类: link to fiddle MainActivity code


DownloadXML class: 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)

1 个答案:

答案 0 :(得分:1)

class LoadData extends AsyncTask<String, String, String>
{

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        dxml = new DownloadXML();
        in_loc = inloc.getText().toString();
        dxml.checkInetConnection(this,in_loc);
        return null;
    }
    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
             outloc.setText(hxml.getLocation());
             outtemp.setText(hxml.getTemperature());
        super.onPostExecute(result);
    }
}

而不是

case R.id.btn_search:
             new LoadData().execute();
              break;