一段时间后的AsyncTask setMessage android java

一段时间后的AsyncTask setMessage android java,java,android,android-asynctask,Java,Android,Android Asynctask,嗨,我的应用程序中有AsyncTask,但我无法更改其设置消息 例如:- private class ProgressTask1 extends AsyncTask<String, Void, Boolean> { private ProgressDialog dialog; public ProgressTask1(MainActivity mainActivity) { context = mainActivity; di

嗨,我的应用程序中有AsyncTask,但我无法更改其设置消息

例如:-

private class ProgressTask1 extends AsyncTask<String, Void, Boolean> {
    private ProgressDialog dialog;

        public ProgressTask1(MainActivity mainActivity) {
        context = mainActivity;
        dialog = new ProgressDialog(context);
    }

    private Context context;

    protected void onPreExecute() {

        this.dialog.setMessage("Checking system...");
        this.dialog.setCancelable(false);
        this.dialog.setTitle("Please Wait...");
        this.dialog.setIcon(R.drawable.ic_launcher);
        this.dialog.show();
    }
但这让应用程序强制关闭,不要给它太低的评价,因为我尽了最大努力搜索了论坛,但能够解决这个问题,所以在这个不错的社区请求帮助:)

有人能帮忙吗?:)

错误

05-13 23:36:34.899:E/AndroidRuntime(2454):由以下原因引起: android.view.ViewRootImpl$CalledFromErrorThreadException:只有 创建视图层次结构的原始线程可以接触其视图


您无法从后台更新UI。


请参阅以根据后台进度更新UI:D

嗯,您不应该从后台线程更新UI。要更新UI,您可以通过两种方式进行:

1) 使用
publishProgress(进度…值)
onProgressUpdate(…)
。为此,必须更改
AsynTask
类:

 private class ProgressTask1 extends AsyncTask<String, String, Boolean> {

    //.......... //your init
    @Override
    protected Boolean doInBackground(String... params) {
        //your background handle

        //publish to change UI
        String toShow = "your_string_here";
        publishProgress(toShow);
        //return ...; //your return value
    }

    @Override
    protected void onProgressUpdate(String... values) {
        super.onProgressUpdate(values);
        //detect message and show it.
        //this.dialog.setMessage(values[0]);
    }
}

如果你解决了问题,请发布你的解决方案!不存在问题我不知道真正的问题是什么请参见@alenz316答案,您必须使用异步中的其他方法来完成此操作。就我个人而言,我喜欢在所有工作完成后在
onPostExecute
中做最后一次更新。是的,这很酷,但不知何故,它很好地让用户知道现在的进展情况:)是的,或者使用
onPostExecute
:)我不知何故没有得到你能帮我一个忙写一点代码吗?我真的没有时间为你写。但是在
doInBackground()
中使用
publishProgress(Progress…values)
,它将在UI线程上调用
onProgressUpdate()
。这是我尝试过的,但对我不起作用。我确定我做错了:(
 private class ProgressTask1 extends AsyncTask<String, String, Boolean> {

    //.......... //your init
    @Override
    protected Boolean doInBackground(String... params) {
        //your background handle

        //publish to change UI
        String toShow = "your_string_here";
        publishProgress(toShow);
        //return ...; //your return value
    }

    @Override
    protected void onProgressUpdate(String... values) {
        super.onProgressUpdate(values);
        //detect message and show it.
        //this.dialog.setMessage(values[0]);
    }
}
private class ProgressTask1 extends AsyncTask<String, Void, Boolean> {
    //.......... //your init

    @Override
    protected Boolean doInBackground(String... params) {
        //your background handle
        //return ...;//your return value
    }


    @Override
    protected void onPostExecute(Boolean aBoolean) {
        super.onPostExecute(aBoolean);
        String toShow = "your_string_here";
        //this.dialog.setMessage(toShow);
    }
}