- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
嗨,我的应用程序中有 AsyncTask,但我无法更改其 setMessage
例如:-
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();
}
现在我想要更改 setmessage,我尝试将其添加到 doinbackground
protected Boolean doInBackground(final String... args) {
dothis1();
this.dialog.setMessage("one done...");
dothis2();
this.dialog.setMessage("two done...");
但这使应用程序强制关闭,并且不要将其评价为低,因为我尽了最大努力并搜索了论坛,但能够解决此问题,因此在这个不错的社区请求帮助:)
有人可以帮忙吗? :)
错误
05-13 23:36:34.899: E/AndroidRuntime(2454): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
最佳答案
ohmm,您不应该从后台线程更新 UI。要更新 UI,您可以通过两种方式进行:
1) 使用publishProgress (Progress...values)
和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]);
}
}
2) 使用onPostExecute(....)
:
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);
}
}
关于java - 一段时间后AsyncTask setMessage android java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30221847/
我是开发 Android 应用程序的初学者。我不知道为什么我的 alertDialog 框始终显示 null 值。该代码用于通过检查我的数据库上的数据来登录。将不胜感激任何帮助。 我已将 MySql
dialogBuilder.setMessage 什么也没显示!就好像它正在显示一个 null 字符串。但是,newUserPassword 会通过用户通过 EditText newPasswor
这是一个奇怪的案例,最近才引起我的注意。我有一个 Activity 在创建时显示 AlertDialog 以警告用户要完成他们需要重新启动的过程。在我的设备 (Galaxy Nexus) 上,它显示得
我正在尝试将一些文本设置到 DialogFragment 中,并且该文本包含一些新行。 这些我都试过了: setMessage("这是A行\n这是b行"); setMessage("这是A行\\\n这
我有这个代码 public void serviceUnavailable() { runOnUiThread(new Runnable() { @Override
我希望有人能帮我弄清楚如何仅使用一个循环遍历字符串数组或任何其他方式的虚拟计时器来更改进度对话框中的设置消息对话框。例如,在加载时它可以说正在加载... -> 建筑... -> 渲染... -> 等等
嗨,我的应用程序中有 AsyncTask,但我无法更改其 setMessage 例如:- private class ProgressTask1 extends AsyncTask { pri
AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(getString(R.string
我在 onCreateDialog() 方法中创建 ProgressDialog 时遇到问题。代码如下: Dialog dialog; switch(id){ case CONNECTING:
我正在创建一个对话框,但我不知道如何编辑消息。 Dialog d2 = new Dialog(this); d2.setTitle("title ok"); d2.setMessage("error
下面是我的代码,我试图通过 onProgressUpdate 方法在 Asyntask 中显示我的进度,但它没有显示在警报对话框中。仅显示初始消息。 class DownloadFileFromUR
我需要在警报对话框中显示多行文本。如果我使用多个 setMessage() 方法,则只显示最后一个 setMessage,如下所示。 final AlertDialog alertDialog = n
我正在尝试为 AlertDialog.Builder 的消息部分设置字体。以下代码仅适用于标题和按钮。我不知道为什么消息字体不一样? Java 代码: AlertDialog.Builder adb
如何通过 setMessage 使用类型安全的 url? 我要改变 ... setMessage [shamlet|Warning! See Help.|] ... 包含链接的消息。 据我目前收
为什么我在这个句子中会出现编译错误? this.message.setMessage(R.string.game_over); 错误信息: The method setMessage(String)
使用 AlertDialog 时如何将部分文本设置为粗体的setMessage() ?添加 和 给我的String不工作。 最佳答案 您也需要使用 Html.fromHtml()。例如: AlertD
在alertController中,如果输入字段为空,我想调用prompt.setMessage()。但这在ionic4中不是有效的函数 代码 const prompt= await this.al
我正在为 Honeycomb 重写一个现有的应用程序,但我遇到了一个问题。在现有的应用程序中,我们创建了一个具有默认标题和消息值的 AlertDialog,然后在需要时替换它们。为了替换它们,我们使用
我无法理解为什么 Java 不允许在异常类型(或其父类(super class) Throwable)创建后更改异常消息。它允许使用 setStackTrace 更改 stackTrace 而不是消息
我有一个 asynktask,它显示一个 progressDialog 并在 doInBackground 方法中更新它的值。方法代码: @Override protected void onPreE
我是一名优秀的程序员,十分优秀!