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.