警报对话框和异步任务

发布于 2024-12-17 17:46:04 字数 1048 浏览 0 评论 0原文

我正在尝试在活动中使用警报对话框和异步任务,但收到以下错误

原因:java.lang.RuntimeException:无法在内部创建处理程序 尚未调用Looper.prepare()的线程

代码:

public class loginTask extends AsyncTask<Void, Void, Void> {
    public ProgressDialog loginDialog = new ProgressDialog(
            LoginActivity.this);

    @Override
    protected void onPreExecute() {
        loginDialog.setMessage("Please wait Logging in");
        loginDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {

        if(loginUser()) {
            loginDialog.dismiss();
            alertbox("title", "winnn", "Okay");
        } else {
            loginDialog.dismiss();
            alertbox("title", "message", "Okay");
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void unused) {
        loginDialog.dismiss();
        Intent intentHome = new Intent(LoginActivity.this,
                HomeActivity.class);
        startActivity(intentHome);
    }
}

i am trying to use Alert Dialog Box and Async Task in the activity and am getting the following error

Caused by: java.lang.RuntimeException: Can't create handler inside
thread that has not called Looper.prepare()

Code:

public class loginTask extends AsyncTask<Void, Void, Void> {
    public ProgressDialog loginDialog = new ProgressDialog(
            LoginActivity.this);

    @Override
    protected void onPreExecute() {
        loginDialog.setMessage("Please wait Logging in");
        loginDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {

        if(loginUser()) {
            loginDialog.dismiss();
            alertbox("title", "winnn", "Okay");
        } else {
            loginDialog.dismiss();
            alertbox("title", "message", "Okay");
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void unused) {
        loginDialog.dismiss();
        Intent intentHome = new Intent(LoginActivity.this,
                HomeActivity.class);
        startActivity(intentHome);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

面如桃花 2024-12-24 17:46:04

您无法直接在 doInBackground() 方法内更新 UI。 (是的,如果您仍然想执行,请在 doInBackground() 内的 runOnUiThread() 方法中编写相同的内容)

否则,请在内部执行onPostExecute() 方法。

public class loginTask extends AsyncTask<Void, Void, Void> 
{
    public ProgressDialog loginDialog = new ProgressDialog( LoginActivity.this );
    public Boolean flag;

    @Override
    protected void onPreExecute() {
        loginDialog.setMessage("Please wait Logging in");
        loginDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        if(loginUser())
          flag = true;
        else 
          flag=false;

        return null;
    }

    @Override
    protected void onPostExecute(Void unused) {
        loginDialog.dismiss();

        if(flag)
          alertbox("title", "winnn", "Okay");
        else
          alertbox("title", "message", "Okay");
    }
}

You can't update UI inside the doInBackground() method directly. (Yes if you still want to execute then write the same inside the runOnUiThread() method inside the doInBackground())

Otherwise, do it inside the onPostExecute() method.

public class loginTask extends AsyncTask<Void, Void, Void> 
{
    public ProgressDialog loginDialog = new ProgressDialog( LoginActivity.this );
    public Boolean flag;

    @Override
    protected void onPreExecute() {
        loginDialog.setMessage("Please wait Logging in");
        loginDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        if(loginUser())
          flag = true;
        else 
          flag=false;

        return null;
    }

    @Override
    protected void onPostExecute(Void unused) {
        loginDialog.dismiss();

        if(flag)
          alertbox("title", "winnn", "Okay");
        else
          alertbox("title", "message", "Okay");
    }
}
笑忘罢 2024-12-24 17:46:04

onPreexecute 和 onPostExecute 是异步任务中 UI 部分的一部分。 doInBackground 是一个单独的线程,因此 doInBackground 内完成的任何操作都需要以 ProgressUpdate 的形式处理

 private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
     protected Long doInBackground(URL... urls) {
         int count = urls.length;
         long totalSize = 0;
         for (int i = 0; i < count; i++) {
             totalSize += Downloader.downloadFile(urls[i]);
             publishProgress((int) ((i / (float) count) * 100));
         }
         return totalSize;
     }

     protected void onProgressUpdate(Integer... progress) {
         setProgressPercent(progress[0]);
     }

     protected void onPostExecute(Long result) {
         showDialog("Downloaded " + result + " bytes");
     }
 }

示例参考: 链接

反映了您需要在 doInBackground 进程之间对 UI 进行的任何更改。

the onPreexecute and onPostExecute are part of the UI parts in the Async Task.. the doInBackground is a seperate thread so any thing done inside the doInBackground needs to be handled in the form of progressUpdate

 private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
     protected Long doInBackground(URL... urls) {
         int count = urls.length;
         long totalSize = 0;
         for (int i = 0; i < count; i++) {
             totalSize += Downloader.downloadFile(urls[i]);
             publishProgress((int) ((i / (float) count) * 100));
         }
         return totalSize;
     }

     protected void onProgressUpdate(Integer... progress) {
         setProgressPercent(progress[0]);
     }

     protected void onPostExecute(Long result) {
         showDialog("Downloaded " + result + " bytes");
     }
 }

Example Reference: Link

reflects any changes you need to make to the UI inbetween the doInBackground process.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文