gpt4 book ai didi

java - NullPointerException 进入 onPostExecute 方法

转载 作者:行者123 更新时间:2023-12-02 03:38:42 28 4
gpt4 key购买 nike

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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com