我正在获取一些JSON数据并将其解析为我的对象。我想用它来获取我需要的所有字段。但是我在我的arraylist上得到一个null,我不知道为什么例如。
private List<MovieDetail> mMovieDetails;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
setupActionBar();
Intent intent = getIntent();
MovieDetail movieDetail = (MovieDetail) intent.getSerializableExtra(POSTER_TRANSFER);
Glide.with(this).load(movieDetail.getPoster()).into((ImageView) findViewById(R.id.main_backdrop));
ProcessMovieSearchImdbIDString processMovieSearchImdbIDString = new ProcessMovieSearchImdbIDString(movieDetail.getImdbID());
processMovieSearchImdbIDString.execute();
CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.main_collapsing);
collapsingToolbarLayout.setTitle(movieDetail.getTitle());
//this returns null which it shouldn't
if(mMovieDetails !=null){
for (MovieDetail detail: mMovieDetails) {
Log.d("Details", "movie actors: " + detail.getActors());
}
}
((TextView) findViewById(R.id.grid_title)).setText(movieDetail.getTitle());
((TextView) findViewById(R.id.grid_writers)).setText(movieDetail.getWriter());
((TextView) findViewById(R.id.grid_actors)).setText(movieDetail.getActors());
((TextView) findViewById(R.id.grid_director)).setText(movieDetail.getDirector());
((TextView) findViewById(R.id.grid_genre)).setText(movieDetail.getGenre());
((TextView) findViewById(R.id.grid_released)).setText(movieDetail.getReleased());
((TextView) findViewById(R.id.grid_plot)).setText(movieDetail.getPlot());
((TextView) findViewById(R.id.grid_runtime)).setText(movieDetail.getRuntime());
}
private void setupActionBar() {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
// Show the Up button in the action bar.
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
startActivity(new Intent(this, MainActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
public class ProcessMovieSearchImdbIDString extends JsonParse {
public ProcessMovieSearchImdbIDString(String id) {
super(id);
}
public void execute() {
//super.execute();
ProcessData processData = new ProcessData();
processData.execute();
}
public class ProcessData extends DownloadSearchMovieImdbidJsonData {
protected void onPostExecute(String webData) {
super.onPostExecute(webData);
//this doesn't return null
mMovieDetails=getMovies();
for (MovieDetail detail : mMovieDetails) {
Log.d("Details", "movie actors a: " + detail.getActors());
}
}
}
}
我不确定为什么在onCreate方法中使用数组时返回null。如果有人可以提供帮助,那将非常感激。
答案 0 :(得分:0)
解决问题的方法是将列表移动到@ M.WaqasPervez提到的onPostExecute
这是代码修复
public class ProcessMovieSearchImdbIDString extends JsonParse {
public ProcessMovieSearchImdbIDString(String id) {
super(id);
}
public void execute() {
//super.execute();
ProcessData processData = new ProcessData();
processData.execute();
}
public class ProcessData extends DownloadSearchMovieImdbidJsonData {
protected void onPostExecute(String webData) {
super.onPostExecute(webData);
mMovieDetails=getMovies();
for (MovieDetail detail : mMovieDetails) {
((TextView) findViewById(R.id.grid_title)).setText(detail.getTitle());
((TextView) findViewById(R.id.grid_writers)).setText(detail.getWriter());
((TextView) findViewById(R.id.grid_actors)).setText(detail.getActors());
((TextView) findViewById(R.id.grid_director)).setText(detail.getDirector());
((TextView) findViewById(R.id.grid_genre)).setText(detail.getGenre());
((TextView) findViewById(R.id.grid_released)).setText(detail.getReleased());
((TextView) findViewById(R.id.grid_plot)).setText(detail.getPlot());
((TextView) findViewById(R.id.grid_runtime)).setText(detail.getRuntime());
}
}
}
}
全部谢谢