java - NullPointerException 进入 onPostExecute 方法

标签 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。 我想将一些路径字符串放入我的电影信息适配器中。 当我调试时,它会停止在其他一些库而不是我编写的代码。

最佳答案

您的 ArrayAdapter movieInfos 未实例化。所以当它跑到线上时

movieInfos.clear();

NullPointerException 将会出现。

关于java - NullPointerException 进入 onPostExecute 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37128243/

相关文章:

java - 使用recyclerview的 "onChildRemoved'方法出错

java - Play Framework 2.1 中的简单搜索?

android - 使用电话号码和密码的 Firebase Auth

java - MultiAutoCompleteTextView 标记定义

java - 如何停止android应用程序中的所有工作线程

java - 使用多个自定义表格模型避免重复代码

java - 这是什么“拉动刷新” View ?

java - 带有 JPA 的 GWT - 没有持久性提供者

java - 创建一个页面,在主要 Activity 开始之前询问用户详细信息

android - 添加原始资源文件夹时,'"aapt.exe'“以非零退出值完成”崩溃