启动第二个意图&从主活动中查看并使用线程显示 ProgressDialog

发布于 2024-12-20 21:11:01 字数 1788 浏览 5 评论 0原文

我在显示 ProgressDialog 时遇到一些问题。我有一种从网站上抓取信息的方法,我想向用户显示某种“正在加载”窗口,而不是应用程序在工作时看起来挂起一两秒钟。

当我不实现 ProgressDialog & 时,一切正常。线程,但是当我尝试实现线程来完成繁重的工作时,“关于我”视图窗口是空的。

我有一个带有注册 OnClickListener 的 TextView 的 MainActivity。

单击 TextView 会执行以下操作:

    startActivity(new Intent(getBaseContext(), AboutMe.class));

这是 AboutMe.class 活动的大部分内容:

public class AboutMe extends Activity {
private ProgressDialog aboutMeProgressDialog;
private String htmlAboutMe = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
    getAboutMe(); // Get information from Internet

    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFeatureDrawableResource(Window.FEATURE_NO_TITLE, android.R.drawable.ic_dialog_alert);
    setContentView(R.layout.abutme);

    TextView tvAbout = (TextView) findViewById(R.id.aboutMe);
    tvAbout.setText(Html.fromHtml(htmlAboutMe));

}

private void getAboutMe() {
    try {
        aboutMeProgressDialog = ProgressDialog.show(AboutMe.this, "", "Loading");
        new Thread() {
            @Override
            public void run() {
                try {
                                /** Code to scape webpage **/
                }
                catch (Exception exp) {
                    exp.printStackTrace();
                }
                handler.sendEmptyMessage(0);
            }
        }.start();
    }
    catch (Exception ex) {
        ex.printStackTrace();
    }
}

private final Handler handler = new Handler() {
    @Override
    public void handleMessage(final Message msg) {
        aboutMeProgressDialog.dismiss();
    }
};   

我显然错过了一些琐碎的事情,但我已经尝试 Google 搜索我能想到的所有内容,但仍然找不到与 ProgressDialog 一起为我工作。

I'm having some problems displaying a ProgressDialog. I have a method that scrapes information from a website, and I want to show the user some kind of "Loading" window instead of the app just looking like it is hanging for a second or two when it is working.

Everything works fine when I don't implement a ProgressDialog & Thread, but as soon as I try to implement a Thread to do the heavy lifting, the AboutMe View window is empty.

I have a MainActivity with a TextView that registers a OnClickListener.

A Click on the TextView does a:

    startActivity(new Intent(getBaseContext(), AboutMe.class));

This is most of the AboutMe.class Activity:

public class AboutMe extends Activity {
private ProgressDialog aboutMeProgressDialog;
private String htmlAboutMe = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
    getAboutMe(); // Get information from Internet

    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFeatureDrawableResource(Window.FEATURE_NO_TITLE, android.R.drawable.ic_dialog_alert);
    setContentView(R.layout.abutme);

    TextView tvAbout = (TextView) findViewById(R.id.aboutMe);
    tvAbout.setText(Html.fromHtml(htmlAboutMe));

}

private void getAboutMe() {
    try {
        aboutMeProgressDialog = ProgressDialog.show(AboutMe.this, "", "Loading");
        new Thread() {
            @Override
            public void run() {
                try {
                                /** Code to scape webpage **/
                }
                catch (Exception exp) {
                    exp.printStackTrace();
                }
                handler.sendEmptyMessage(0);
            }
        }.start();
    }
    catch (Exception ex) {
        ex.printStackTrace();
    }
}

private final Handler handler = new Handler() {
    @Override
    public void handleMessage(final Message msg) {
        aboutMeProgressDialog.dismiss();
    }
};   

I'm clearly missing out on something trivial, but I've tried to Google just about everything I can think of, but still can't get a Thread together with ProgressDialog to work for me.

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

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

发布评论

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

评论(2

陌上青苔 2024-12-27 21:11:01

请使用 run on ui thread 方法
而不是 handler.sendEmptyMessage(0);使用此代码并删除处理消息

               runOnUiThread(new Runnable() {
                @Override
                public void run() {
                     aboutMeProgressDialog.dismiss();
                }
            });

伙计让我知道这是否成功,大多数时候它都有效

please use run on ui thread method
instead of handler.sendEmptyMessage(0); use this code and remove handle message

               runOnUiThread(new Runnable() {
                @Override
                public void run() {
                     aboutMeProgressDialog.dismiss();
                }
            });

dude let me know if this was successful,it works most of times

街角卖回忆 2024-12-27 21:11:01

请在调用 super.onCreate(savedInstanceState) 后调用 getAboutMe() 方法;

please call getAboutMe() method after calling super.onCreate(savedInstanceState);

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