Json元素在AsyncTask中返回null



当我在按钮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");
    }
}

相关内容

  • 没有找到相关文章

最新更新