Fragment中AsyncTask中的ProgressDialog崩溃

时间:2015-10-27 10:25:40

标签: java asynchronous android-asynctask fragment progressdialog

Fragment内有AsyncTask Activity。但是当我运行搜索时,我得到了这个例外:

Attempt to invoke virtual method 'android.content.res.Resources$Theme android.content.Context.getTheme()' on a null object reference.

它与Fragment有关,不知道Activity是否正在运行,但我不知道如何修复它。 请帮忙。 这是代码:

class Send_API extends AsyncTask<String, Void, String> {

        private ProgressDialog dialog;

        @Override
        protected void onPreExecute() {
            if (movieList != null){
                movieList.clear();
                adapter.notifyDataSetChanged();
            }

            dialog = new ProgressDialog(getActivity());
            dialog.setMessage("Searching...");
            dialog.show();
        }

        @Override
        protected String doInBackground(String... params) {
            String queryString = null;
            try {
                queryString = "" +
                        "s=" + URLEncoder.encode(txt, "utf-8");
            } catch (UnsupportedEncodingException e) {
            }
            return HttpHandler.get(params[0], queryString);
        }


        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            dialog.dismiss();
            if (result == null) {
                Toast.makeText(getActivity(), "error getting results...", Toast.LENGTH_LONG).show();

            } else {

                try {
                    JSONObject json = new JSONObject(result);

                    JSONArray searchArray = json.getJSONArray("Search");

                    for (int i = 0; i < searchArray.length(); i++) {
                        JSONObject searchObject = searchArray.getJSONObject(i);
                        String title = searchObject.getString("Title");
                        String type = searchObject.getString("Type");
                        String year = searchObject.getString("Year");
                        String imdbID = searchObject.getString("imdbID");
                        String poster_url = searchObject.getString("Poster");
                        movieList.add(new Movie(title, type, year, imdbID, poster_url));

                        Log.e(TAG, "MovieList is " + movieList);


                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(getActivity(), "error parsing results...", Toast.LENGTH_LONG).show();
                }

                adapter.notifyDataSetChanged();

            }
        }

    }

2 个答案:

答案 0 :(得分:0)

  dialog = new ProgressDialog(getActivity());
初始化getActivity()时,

Fragment将返回null。您必须等到片段生命周期中的onAttach()才能返回非空。

这就是NPE

的原因

总的来说,片段中没有非默认构造函数。仅在onCreateView()

中设置片段视图

答案 1 :(得分:0)

尝试将基本上下文传递给您的asynctask并将outer_dictionary = your_list[0] list_of_dictionaries = outer_dictionary['tr'] 替换为names = [] for dictionary in list_of_dictionaries: if "name" in dictionary: names.append(dictionary["name"])