0

这是我的 AsyncTask 类

private class UpdatingNews extends AsyncTask<Void, Void, String> {

    @Override
    protected String doInBackground(Void... params) {
        webservice.UpdateAllNews();
        webservice.updateallCatNews();
        return null;
    }

    @Override
    protected void onPostExecute(String result) {

    }
}

我叫它

UpdatingNewsupdate = new UpdatingNews();
        update.execute();

它确实执行。然后我想在单击按钮时检查此任务的状态是否完成,所以我像这样检查。

if (update.getStatus().toString()
                            .equals(AsyncTask.Status.FINISHED.toString())) {
                        toast = Toast.makeText(Main_FormNewUser.this,
                                "Thanks for fill in our survey form!",
                                Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER_VERTICAL
                                | Gravity.CENTER_HORIZONTAL, 0, 0);
                        toast.show();
                        name = nametext.getText().toString();
                        email = emailtext.getText().toString();
                        webservice.InsertSurvey(name, email, ages, genders,
                                jobs, salaries, interesting);
                        webservice.insertFormSubmit(1);

                        startActivity(new Intent(Main_FormNewUser.this,
                                Main_AllLatestNews.class));
                        finish();
                    }

单击后,它将启动 Main_AllLatestNews.class 但出现 NullPointerException。

我尝试不使用 AsyncTask 并且没有返回错误,我不知道为什么我调用 AsyncTask 它会给我错误?

4

2 回答 2

0

像这样使用 onPostExecute 怎么样

添加布尔值以确定 AsyncTask 是否结束

@Override
protected void onPostExecute(String result) {
    check = true;
}


//if (update.getStatus().toString().equals(AsyncTask.Status.FINISHED.toString())) {
if(check){
                    toast = Toast.makeText(Main_FormNewUser.this,
                            "Thanks for fill in our survey form!",
                            Toast.LENGTH_SHORT);
                    toast.setGravity(Gravity.CENTER_VERTICAL
                            | Gravity.CENTER_HORIZONTAL, 0, 0);
                    toast.show();
                    name = nametext.getText().toString();
                    email = emailtext.getText().toString();
                    webservice.InsertSurvey(name, email, ages, genders,
                            jobs, salaries, interesting);
                    webservice.insertFormSubmit(1);

                    startActivity(new Intent(Main_FormNewUser.this,
                            Main_AllLatestNews.class));
                    finish();
                }
于 2012-05-01T06:31:23.743 回答
0

了解 AsyncTask 的基础知识查看此博客。无痛线程AsyncTask

您尚未接受 onPostExecute(String result) 中 doInBackground() 的返回

doInBackground() 将结果返回给 onPostExecute。因此,您必须仅在 onPostExecute 中更新您想要的值或 UI。

于 2012-05-01T06:31:30.023 回答