当我在按钮onClick
方法中只调用wpCategories = JsonToElement.getllAllCategory();
时,它工作正常。(wpCategories
填满数据)。但是当我把它放在asynctask
, wpCategories
返回null。(doInBackground
没有被调用吗?)
这是我的按钮点击方法:
public void onImageGridClick(View view) {
new GetJsonElementTask().execute();
Intent intent = new Intent(this, CategoryGridActivity.class);
intent.putParcelableArrayListExtra(Extra.IMAGES, wpCategories);
startActivity(intent);
}
和asyncTask:
private class GetJsonElementTask extends AsyncTask<Void, Integer, String> {
@Override
protected void onPreExecute() {
progressDialog = ProgressDialog.show(HomeActivity.this, "",
"Loading...");
}
@Override
protected String doInBackground(Void... arg0) {
wpCategories = JsonToElement.getllAllCategory();
return null;
}
@Override
protected void onPostExecute(String value) {
progressDialog.dismiss();
}
@Override
protected void onProgressUpdate(Integer... progress) {
Log.v("DEBUG_LOG", "In onProgressUpdate");
}
}
在onPostExecute
内启动活动,因为这个方法在doInBackground
执行完成后执行:
@Override
protected String doInBackground(Void... arg0) {
wpCategories = JsonToElement.getllAllCategory();
return wpCategories; //<<< return value from here
}
@Override
protected void onPostExecute(String value) {
progressDialog.dismiss();
Intent intent = new Intent(this, CategoryGridActivity.class);
intent.putParcelableArrayListExtra(Extra.IMAGES, value);
startActivity(intent);
}
尝试以下
public void onImageGridClick(View view) {
new GetJsonElementTask().execute();
}
public void startCategoryGridActivity(){
Intent intent = new Intent(this, CategoryGridActivity.class);
intent.putParcelableArrayListExtra(Extra.IMAGES, wpCategories);
startActivity(intent);
}
和asyncTask:
private class GetJsonElementTask extends AsyncTask<Void, Integer, String> {
@Override
protected void onPreExecute() {
progressDialog = ProgressDialog.show(HomeActivity.this, "",
"Loading...");
}
@Override
protected String doInBackground(Void... arg0) {
wpCategories = JsonToElement.getllAllCategory();
return null;
}
@Override
protected void onPostExecute(String value) {
progressDialog.dismiss();
startCategoryGridActivity();
}
@Override
protected void onProgressUpdate(Integer... progress) {
Log.v("DEBUG_LOG", "In onProgressUpdate");
}
}