Reputation: 148
I am Calling Asynctask to get response from the Json Service and setting the list in the post execute. Its working fine but when I open any other applications and then again open my app. I am getting NPE in the post execute. Any idea why its happening. Any Help Would be greatly appreciated.
public class AsyncTaskParks extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
utility.showProgress(getActivity(), "Please Wait", "Loading..");
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
String response = mServiceHandler
.getSoapResponseByMethod("GetSiteList");
Log.d("Response: ", "> " + response);
if (response != null) {
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray rows = jsonObject.getJSONArray("Rows");
for (int i = 0; i < rows.length(); i++) {
JSONObject c = rows.getJSONObject(i);
infoParks = new InfobeanParks();
String siteId = c.getString("SiteID");
infoParks.setSiteId(siteId);
String siteType = c.getString("SiteType");
infoParks.setSiteType(siteType);
String siteName = c.getString("SiteName");
infoParks.setSiteName(siteName);
String siteShortName = c.getString("SiteShortName");
infoParks.setSiteShortName(siteShortName);
String siteImage = c.getString("SiteImage");
infoParks.setSiteImage(siteImage);
if (siteType.equalsIgnoreCase("P")) {
lstPark.add(infoParks);
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return response;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
utility.dismissProgress();
gridviewParks.setAdapter(new GridviewParksAdapter(getActivity(),
lstPark));
}
}
08-28 00:58:15.911: E/AndroidRuntime(3720): FATAL EXCEPTION: main
08-28 00:58:15.911: E/AndroidRuntime(3720): Process: com.oceancity.ocparks, PID: 3720
08-28 00:58:15.911: E/AndroidRuntime(3720): java.lang.NullPointerException
08-28 00:58:15.911: E/AndroidRuntime(3720): at com.ocparks.adapter.GridviewParksAdapter.<init>(GridviewParksAdapter.java:47)
08-28 00:58:15.911: E/AndroidRuntime(3720): at com.ocparks.FragmentParks$AsyncTaskParks.onPostExecute(FragmentParks.java:201)
08-28 00:58:15.911: E/AndroidRuntime(3720): at com.ocparks.FragmentParks$AsyncTaskParks.onPostExecute(FragmentParks.java:1)
08-28 00:58:15.911: E/AndroidRuntime(3720): at android.os.AsyncTask.finish(AsyncTask.java:632)
08-28 00:58:15.911: E/AndroidRuntime(3720): at android.os.AsyncTask.access$600(AsyncTask.java:177)
08-28 00:58:15.911: E/AndroidRuntime(3720): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
08-28 00:58:15.911: E/AndroidRuntime(3720): at android.os.Handler.dispatchMessage(Handler.java:102)
08-28 00:58:15.911: E/AndroidRuntime(3720): at android.os.Looper.loop(Looper.java:136)
08-28 00:58:15.911: E/AndroidRuntime(3720): at android.app.ActivityThread.main(ActivityThread.java:5086)
08-28 00:58:15.911: E/AndroidRuntime(3720): at java.lang.reflect.Method.invokeNative(Native Method)
08-28 00:58:15.911: E/AndroidRuntime(3720): at java.lang.reflect.Method.invoke(Method.java:515)
08-28 00:58:15.911: E/AndroidRuntime(3720): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
08-28 00:58:15.911: E/AndroidRuntime(3720): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
08-28 00:58:15.911: E/AndroidRuntime(3720): at dalvik.system.NativeStart.main(Native Method)
Upvotes: 0
Views: 759
Reputation: 1566
You can't change the views of an inactive Activity
. You need to check that the Activity
is still running in the onPostExecute
or better yet, just cancel the ASyncTask
completely in the onPause or onStop methods.
public void onPause() {
super.onPause();
if (mAsyncTaskParks != null){
mAsyncTaskParks.cancel();
}
}
The benefit of this second method is that it will call ASyncTask's
onCancelled
method which will allow you to cancel the network call, if that's what you want to do.
Upvotes: 2