-1

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)
David Kasabji
  • 1,049
  • 3
  • 15
  • 32

1 Answers1

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);
    }
}

and than

case R.id.btn_search:
             new LoadData().execute();
              break;
Ravi
  • 34,851
  • 21
  • 122
  • 183
  • Thanks @Ravi : Yes, I read about it: [link](http://stackoverflow.com/questions/9671546/asynctask-android-example)[/link], **however**, I dont know how to use it in my case. The part you copied here from my code, is inside a `switch` case statement - I dont know how to implement Async here. Any help? – David Kasabji Nov 12 '14 at 12:44
  • put data in onPostExecute() or put it in runonuithread() both are same, you can use it as per your needs. – Ravi Nov 12 '14 at 12:49
  • I get this error now: `Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.myweatherapp.myweatherapp.HandleXML.getTemperature()' on a null object reference` **LINE:** onPostExecute – David Kasabji Nov 12 '14 at 13:15
  • that is because hxml.getLocation(). – Ravi Nov 12 '14 at 13:17
  • you dint initialize hxml, check – Ravi Nov 12 '14 at 13:18
  • I figured yes, but I dont know what could be wront with m getLocation(), it's just a getter method in HandleXML class. It should return the location from the XML file – David Kasabji Nov 12 '14 at 13:18
  • but u should initialise it, like hxml=new HandleXML() – Ravi Nov 12 '14 at 13:19
  • 1
    Read you comment later. I will accept your answer, as now it works! Thank you very much Ravi! – David Kasabji Nov 12 '14 at 13:22
  • can you maybe help me out more? Some other errors have occured? Now I get `Invalid stream or encoding: java.io.IOException: closed ` in `handlingOnlineXML(HandleXML.java:56)` is there a way we can use chat for this conversation? – David Kasabji Nov 12 '14 at 13:48
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/64851/discussion-between-ravi-and-androidnfc). – Ravi Nov 13 '14 at 09:57