在线程 2017 中显示进度对话框
Show progress dialog inside thread 2017
我读了一些关于这个主题的帖子,我试过这样:
@Override
public void onSuccess(String s) {
Log.d("TestFFmpeg", "SUCCESS with output : " + s);
progressDialog = new ProgressDialog(getApplicationContext());
progressDialog.setTitle("Server request");
progressDialog.setCancelable(false);
progressDialog.setMessage("Waiting response from server...");
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
progressDialog.show();
}
});
final Response response = httpClient.newCall(request).execute();
if (!response.isSuccessful()) {
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
progressDialog.dismiss();
}
});
}
我提到我在 class 中使用了相同的进度对话框,但我拒绝了它。
如果我 运行 这样的应用程序,我会收到此错误:
android.view.WindowManager$BadTokenException: Unable to add window --
token null is not for an application
我应该改变什么?
Dialog
需要 Activity 上下文。
将 ProgressDialog
初始化更改为
progressDialog = new ProgressDialog(MainActivity.this);
我读了一些关于这个主题的帖子,我试过这样:
@Override
public void onSuccess(String s) {
Log.d("TestFFmpeg", "SUCCESS with output : " + s);
progressDialog = new ProgressDialog(getApplicationContext());
progressDialog.setTitle("Server request");
progressDialog.setCancelable(false);
progressDialog.setMessage("Waiting response from server...");
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
progressDialog.show();
}
});
final Response response = httpClient.newCall(request).execute();
if (!response.isSuccessful()) {
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
progressDialog.dismiss();
}
});
}
我提到我在 class 中使用了相同的进度对话框,但我拒绝了它。
如果我 运行 这样的应用程序,我会收到此错误:
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
我应该改变什么?
Dialog
需要 Activity 上下文。
将 ProgressDialog
初始化更改为
progressDialog = new ProgressDialog(MainActivity.this);