当应用程序处于后台时,异步任务会崩溃

时间:2014-08-27 22:44:01

标签: android

我正在调用Asynctask从Json服务获取响应并在post执行中设置列表。它工作正常,但当我打开任何其他应用程序,然后再次打开我的应用程序。我在后执行中获得NPE。不知道为什么会发生这种情况。任何帮助将不胜感激。

    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)

1 个答案:

答案 0 :(得分:2)

您无法更改非活动Activity的观看次数。您需要检查Activity是否仍然在onPostExecute或更好地运行,只需在onPause或onStop方法中完全取消ASyncTask

public void onPause() {
    super.onPause();

    if (mAsyncTaskParks != null){
        mAsyncTaskParks.cancel();
     }
}

第二种方法的好处是它会调用ASyncTask's onCancelled方法,这将允许您取消网络呼叫,如果这是您想要做的。