NullPointerException出现在onPostExecute方法中

时间:2016-05-10 02:17:07

标签: java android

public class MainFragment extends Fragment {

//private poster_adapter movieInfos;
private ArrayAdapter<MovieInfo> movieInfos;

private String LOG_TAG = MainFragment.class.getSimpleName();

public MainFragment() {

}

@Override
public void onStart() {
    super.onStart();
    FetchMovieInfo update = new FetchMovieInfo();
    update.execute();
    Log.d(LOG_TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    Log.d(LOG_TAG, Thread.currentThread().getStackTrace()[2].getMethodName());

    /*movieInfos = new ArrayAdapter<String>(
            getActivity(),
            R.layout.poster_item_layout,
            R.id.poster_item,
            new ArrayList<String>()
    );*/

    View rootView = inflater.inflate(R.layout.activity_main_fragment, container, false);

    //  Log.d(LOG_TAG,movieInfos[]);

    //infoAdapter = new poster_adapter(getActivity(), Arrays.asList(movieInfos));
    GridView gridView = (GridView) rootView.findViewById(R.id.gridview);
    gridView.setAdapter(movieInfos);
    return rootView;
}

public class FetchMovieInfo extends AsyncTask<Void, Void, String[]> {

    String LOG_TAG = MainActivity.class.getSimpleName();

    private String[] getMovieInfoFromJSON(String moviesInfoJSONStr) throws JSONException {
        Log.d(LOG_TAG, Thread.currentThread().getStackTrace()[2].getMethodName().toString());
        JSONObject moviesInfoJSON = new JSONObject(moviesInfoJSONStr);
        JSONArray movieInfoJSON = moviesInfoJSON.getJSONArray("results");
        String[] poster_urls = new String[movieInfoJSON.length()];

        for (int i = 0; i < movieInfoJSON.length(); i++) {
            JSONObject movieInfo = movieInfoJSON.getJSONObject(i);
            poster_urls[i] = movieInfo.getString("poster_path");
        }

        return poster_urls;
    }

    @Override
    protected String[] doInBackground(Void... params) {
        Log.d(LOG_TAG, Thread.currentThread().getStackTrace()[2].getMethodName().toString());

        HttpURLConnection httpURLConnection = null;
        BufferedReader reader = null;

        String movieInfoStr = null;

        try {
            final String BASE_URL = "http://api.themoviedb.org/3/movie/popular?";
            final String API_KEY_PARAM = "api_key";

            Uri builtUri = Uri.parse(BASE_URL).buildUpon()
                    .appendQueryParameter(API_KEY_PARAM, BuildConfig.OPEN_MOVIE_INFO_API_KEY)
                    .build();
            URL url = new URL(builtUri.toString());

            httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("GET");
            httpURLConnection.connect();

            InputStream inputStream = httpURLConnection.getInputStream();
            StringBuffer buffer = new StringBuffer();
            if (inputStream == null) {
                return null;
            }
            reader = new BufferedReader(new InputStreamReader(inputStream));

            String line;
            while ((line = reader.readLine()) != null) {
                buffer.append(line + "\n");
            }
            if (buffer.length() == 0) {
                return null;
            }

            movieInfoStr = buffer.toString();

        } catch (IOException e) {
            Log.e(LOG_TAG, "Error" + e);
            return null;
        } finally {
            if (httpURLConnection != null)
                httpURLConnection.disconnect();
            if (reader != null) {
                try {
                    reader.close();
                } catch (final IOException e) {
                    Log.e(LOG_TAG, "Error" + e);
                }
            }
        }

        try {
            return getMovieInfoFromJSON(movieInfoStr);
        } catch (JSONException e) {
            Log.e(LOG_TAG, e.getMessage(), e);
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(String[] results) {
        Log.d(LOG_TAG, Thread.currentThread().getStackTrace()[2].getMethodName().toString());
        if (results != null) {
            movieInfos.clear();
            for (String movieInfo : results) {
                movieInfos.add(new MovieInfo(movieInfo));
            }
        }
    }
}

}

这是代码。 当我调试时,只要我进入onPostExecute方法,NullPointerException就会出现。 我想把一些路径字符串放入我的电影信息适配器。 当我调试时,它将停在其他一些库而不是我写的代码。

2 个答案:

答案 0 :(得分:1)

您的ArrayAdapter movieInfos未实例化。所以当它跑到行

movieInfos.clear();

NullPointerException wolud出来了。

答案 1 :(得分:1)

在onCreateView方法中,您可以注释掉这些代码:

/*movieInfos = new ArrayAdapter<String>(
        getActivity(),
        R.layout.poster_item_layout,
        R.id.poster_item,
        new ArrayList<String>()
);*/

所以movieInfos是一个空对象,从不实例化。请取消对这些代码的注释,你应该在使用它之前实例movieInfos。