android中使用线程的进度相关问题!

发布于 2024-08-22 23:18:46 字数 519 浏览 5 评论 0原文

朋友们,

当我调用 Web 服务方法来 getposts 它显示进度时,我使用以下代码来显示 andorid 活动的进度。但是当服务调用完成时,我的应用程序崩溃了。

请指导我犯了什么错误或有其他替代方法来实现此目标吗?

mProgressStatus = 0;
  Thread th=new Thread(new Runnable() {
   public void run() {
    if (mProgressStatus < 100) {
     myProgressBar.setVisibility(View.VISIBLE);
    } else {
     myProgressBar.setVisibility(View.GONE);
    }
   }

  });


    th.start();

  results = p.GetPosts(p, PageSize, adap.getCount());

  mProgressStatus=100;

th.stop();

friends,

i am using following code to display progress on andorid activity when i call web service method to getposts it show progress. but when call of serivce is complete my application gets crashed.

please guide what mistake am i doing or any other alternative way to achieve this goal?

mProgressStatus = 0;
  Thread th=new Thread(new Runnable() {
   public void run() {
    if (mProgressStatus < 100) {
     myProgressBar.setVisibility(View.VISIBLE);
    } else {
     myProgressBar.setVisibility(View.GONE);
    }
   }

  });


    th.start();

  results = p.GetPosts(p, PageSize, adap.getCount());

  mProgressStatus=100;

th.stop();

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

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

发布评论

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

评论(2

山川志 2024-08-29 23:18:46

因此,如果您的应用程序崩溃,也许您应该首先查看错误消息?或者至少为我们提供它。

(提示:我今天读了您的三篇文章,您总是在粘贴代码的第一行/最后一行时遇到问题,请在提交问题之前检查您的代码粘贴...)

So, if your application crashes, maybe you should look at the error message first? Or at least provide it for us.

(A tip: I read three posts of you today and you always have trouble with the first/last lines of your code pasting, please check you code pasting before you submit a question...)

倒数 2024-08-29 23:18:46

你可以尝试使用runOnUiThread,如果你在一个活动类中

mProgressStatus = 0;
    MyActivity.this.runOnUiThread(new Runnable() {});
        public void run() {
            if (mProgressStatus < 100) {
                myProgressBar.setVisibility(View.VISIBLE);
            } else {
                myProgressBar.setVisibility(View.GONE);
            }
        }
   });

关于UI的所有事情都必须在UI线程中执行

You can try using runOnUiThread, if you are in an activity class

mProgressStatus = 0;
    MyActivity.this.runOnUiThread(new Runnable() {});
        public void run() {
            if (mProgressStatus < 100) {
                myProgressBar.setVisibility(View.VISIBLE);
            } else {
                myProgressBar.setVisibility(View.GONE);
            }
        }
   });

Everything about the UI have to be executed in the UI Thread

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