0

I have the async task in my TabActivity, and although I have been using it before, it all of a sudden stopped. I must have changed something. I call new CallWebServiceAd().execute(); in the constructor of the activity.

I can comment out all of the code within the async task, but appears merely calling the asynctask crashes the app.

I get:

01-26 16:27:04.196: E/AndroidRuntime(818): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tca.app/com.tca.app.AndroidTabLayoutActivity}: java.lang.NullPointerException
01-26 16:27:04.196: E/AndroidRuntime(818):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
01-26 16:27:04.196: E/AndroidRuntime(818):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
01-26 16:27:04.196: E/AndroidRuntime(818):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
01-26 16:27:04.196: E/AndroidRuntime(818):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
01-26 16:27:04.196: E/AndroidRuntime(818):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-26 16:27:04.196: E/AndroidRuntime(818):  at android.os.Looper.loop(Looper.java:137)
01-26 16:27:04.196: E/AndroidRuntime(818):  at android.app.ActivityThread.main(ActivityThread.java:4340)
01-26 16:27:04.196: E/AndroidRuntime(818):  at java.lang.reflect.Method.invokeNative(Native Method)
01-26 16:27:04.196: E/AndroidRuntime(818):  at java.lang.reflect.Method.invoke(Method.java:511)
01-26 16:27:04.196: E/AndroidRuntime(818):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-26 16:27:04.196: E/AndroidRuntime(818):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-26 16:27:04.196: E/AndroidRuntime(818):  at dalvik.system.NativeStart.main(Native Method)
01-26 16:27:04.196: E/AndroidRuntime(818): Caused by: java.lang.NullPointerException
01-26 16:27:04.196: E/AndroidRuntime(818):  at com.tca.app.AndroidTabLayoutActivity.onCreate(AndroidTabLayoutActivity.java:51)
01-26 16:27:04.196: E/AndroidRuntime(818):  at android.app.Activity.performCreate(Activity.java:4465)
01-26 16:27:04.196: E/AndroidRuntime(818):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-26 16:27:04.196: E/AndroidRuntime(818):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
01-26 16:27:04.196: E/AndroidRuntime(818):  ... 11 more

Here is the async task part:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);




        this.setTabs();
        new CallWebServiceAd().execute();

        System.out.println("Height"+tabHost.getHeight());

    }

     public String getAd(){

              RestClient client = new RestClient("http://example.com");

            try {
                client.Execute(RequestMethod.GET);
            } catch (Exception e) {
                e.printStackTrace();
            }
           String theResponse = client.getResponse();


           return theResponse;  

        }

        public void parseJSONResponse(String jsonResponse) {

            //using gson, place all the json into the SingleEvent object and then into a List
            Type listType = new TypeToken<List<AdEvent>>(){}.getType();  
            List<AdEvent> ad = new Gson().fromJson(jsonResponse, listType);

            this.displayAd(ad);



        }


        private class CallWebServiceAd extends AsyncTask<String,String,String>{

            @Override
            protected String doInBackground(String... arg0) {
                // TODO Auto-generated method stub



                return getAd();
            }


            @Override
            protected void onPostExecute(String result) {

                parseJSONResponse(result);

            }


        }

I've added the onCreate. But like I said, it is the new CallWebServiceAd().execute(); I don't know why it was working before.

Adam
  • 8,849
  • 16
  • 67
  • 131
  • 2
    post the full code including onCreate(). You have a NullPointerException somewhere in there... – LuxuryMode Jan 26 '12 at 21:36
  • added the onCreate. Taking out the new CallWebServiceAd makes it run. So it must have something to do with that. – Adam Jan 27 '12 at 01:10

1 Answers1

-1

Firstly, check, that your app is falling really in this app. Today I wasted an hour to find that the reason was really in the other activity.

Secondly, if it is in this app, put breakpoints in onCreate, onStart and onResume. If you don't have the two last, make them. So, you'll know exactly, when it is falling.

Thirdly, if you are coming into this activity from the other, put the same breakpoints in that second Activity into onStop, onDestroy and onPause.

Very probably, you'll see the solution. If not, come back, put more info in the question and let's think.

Gangnus
  • 24,044
  • 16
  • 90
  • 149