App Chrashes: IlegalStateException Error

351 views Asked by At

I receive the following error:

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

While trying to run my App. What my App should is to display data from an XML. The XML is on web, so I have to fetch it first by putting URL in there. Before that I check if phone has Internet Connection.

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).

When I try to run the App it chrashes.

MainActivity class:link to fiddle MainActivity code


DownloadXML class: link to fiddle DownloadXML code


Sorry I dont know any other source where I could easily store my code and share it here. But it is too big, to just copy it here.

Hope someone can help me out. I have browsed about it on here, and came across AsyncTask, but I dont know how to implement it in my code.

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

There are 1 answers

10
Ravi On BEST ANSWER
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);
    }
}

and than

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