从 Android 上的 ProgressDialog 中的新线程访问自定义类

发布于 2024-12-26 04:10:20 字数 3398 浏览 0 评论 0原文

我在android中创建了一个ProgressDialog,当我做一个简单的例子时它就可以工作。

例如,这有效。

public void onClick(View v) 
{
    // Perform action on click
    System.out.println("Progess Bar");
    final ProgressDialog myProgressDialog = ProgressDialog.show(AndroidTestApplicationActivity.this,
                    "Please wait...", "Getting updates...", true);

    new Thread() 
    {
            public void run() 
            {
                    try
                    {
                            // Do some Fake-Work
                            sleep(5000);
                    } 
                    catch (Exception e) 
                    { 

                    }

                    // Dismiss the Dialog
                    myProgressDialog.dismiss();
            }
    }.start();

}

但是,一旦我添加对自定义类的引用,它就会停止运行这个新线程。

    button1.setOnClickListener(new View.OnClickListener() 
{
    public void onClick(View v) 
    {
        // Perform action on click
        System.out.println("Progess Bar");
        // Display an indeterminate Progress-Dialog
        final ProgressDialog myProgressDialog = ProgressDialog.show(AndroidTestApplicationActivity.this,
                        "Please wait...", "Getting Updates...", true);

        new Thread() 
        {
                public void run() 
                {
                        try
                        {
                                HealthySubObject hsObject = new HealthySubObject();
                                // Do some more work with my hsObject - nothing happens after this point. 
                                sleep(5000);
                        } 
                        catch (Exception e) 
                        { 

                        }

                        // Dismiss the Dialog
                        myProgressDialog.dismiss();
                }
        }.start();

    }
});

发生的情况是,一旦我单击此按钮,进度对话框就会在屏幕上快速闪烁,然后消失。但如果你看我的代码,它应该等待 5 秒才会消失。我在对自定义类的引用之前和之后放置了调试语句,我可以看到之前的语句,但看不到之后的语句。有谁知道为什么会发生这种情况?只要我的类是公共的,我就应该能够从新线程调用它,对吧?

我对 Android 还很陌生,这是我第一次涉足多线程 Android 应用程序。任何帮助将不胜感激。

解决方案

感谢大家的帮助。现在正在工作。

    button1.setOnClickListener(new View.OnClickListener() 
    {
        public void onClick(View v) 
        {
            // Perform action on click
            System.out.println("Progess Bar");
            //ProgressDialog dialog = ProgressDialog.show(AndroidTestApplicationActivity.this, "", "Loading. Please wait...", true);
            // Display an indeterminate Progress-Dialog
            final ProgressDialog myProgressDialog = ProgressDialog.show(AndroidTestApplicationActivity.this,
                            "Please wait...", "Doing Extreme Calculations...", true);

            Handler handler = new Handler();
            handler.postDelayed(new Runnable() 
            {
                @Override
                public void run() 
                {
                    HealthySubObject hsObject = new HealthySubObject();
                    ArrayList<HashMap<String, String>> onlineDB = hsObject.jsonToArray(); 
                    //
                    // more stuff goes here.  
                    //
                    //
                    myProgressDialog.dismiss();
                }
            }, 1500);

        }
    });

I have created a ProgressDialog in android and it works when I do a simple example.

For example, this works.

public void onClick(View v) 
{
    // Perform action on click
    System.out.println("Progess Bar");
    final ProgressDialog myProgressDialog = ProgressDialog.show(AndroidTestApplicationActivity.this,
                    "Please wait...", "Getting updates...", true);

    new Thread() 
    {
            public void run() 
            {
                    try
                    {
                            // Do some Fake-Work
                            sleep(5000);
                    } 
                    catch (Exception e) 
                    { 

                    }

                    // Dismiss the Dialog
                    myProgressDialog.dismiss();
            }
    }.start();

}

But once I add in a reference to my custom class, it just stops running this new thread.

    button1.setOnClickListener(new View.OnClickListener() 
{
    public void onClick(View v) 
    {
        // Perform action on click
        System.out.println("Progess Bar");
        // Display an indeterminate Progress-Dialog
        final ProgressDialog myProgressDialog = ProgressDialog.show(AndroidTestApplicationActivity.this,
                        "Please wait...", "Getting Updates...", true);

        new Thread() 
        {
                public void run() 
                {
                        try
                        {
                                HealthySubObject hsObject = new HealthySubObject();
                                // Do some more work with my hsObject - nothing happens after this point. 
                                sleep(5000);
                        } 
                        catch (Exception e) 
                        { 

                        }

                        // Dismiss the Dialog
                        myProgressDialog.dismiss();
                }
        }.start();

    }
});

What happens is that as soon as I click this button, the progress dialog flashes up on the screen real quick and then disappears. But if you look at my code, it should wait 5 seconds before disappearing. I have put debug statements before and after the reference to my custom class and I can see the statements before but not the ones after. Does anyone have any idea why that is happening? As long as my class is public I should be able to call it from a new thread, right?

I am still pretty new to android and this is my first adventure into multi-threaded android apps. Any help would be much appreciated.

SOLUTION

Thanks for your help everyone. It is working now.

    button1.setOnClickListener(new View.OnClickListener() 
    {
        public void onClick(View v) 
        {
            // Perform action on click
            System.out.println("Progess Bar");
            //ProgressDialog dialog = ProgressDialog.show(AndroidTestApplicationActivity.this, "", "Loading. Please wait...", true);
            // Display an indeterminate Progress-Dialog
            final ProgressDialog myProgressDialog = ProgressDialog.show(AndroidTestApplicationActivity.this,
                            "Please wait...", "Doing Extreme Calculations...", true);

            Handler handler = new Handler();
            handler.postDelayed(new Runnable() 
            {
                @Override
                public void run() 
                {
                    HealthySubObject hsObject = new HealthySubObject();
                    ArrayList<HashMap<String, String>> onlineDB = hsObject.jsonToArray(); 
                    //
                    // more stuff goes here.  
                    //
                    //
                    myProgressDialog.dismiss();
                }
            }, 1500);

        }
    });

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

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

发布评论

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

评论(2

开始看清了 2025-01-02 04:10:20

我真的建议使用 Handler 代替线程。实际上不鼓励使用 Thread.sleep 方法。像这样的事情要好得多:

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        HealthySubObject hsObject = new HealthySubObject();
        myProgressDialog.dismiss();
    }
}, 5000);

I would really recommend to use Handler instead of Thread. Using the Thread.sleep method is actually discouraged. Something like this is much better:

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        HealthySubObject hsObject = new HealthySubObject();
        myProgressDialog.dismiss();
    }
}, 5000);
手心的温暖 2025-01-02 04:10:20

问题是您需要在 UI 线程上修改 UI,并且在线程的 run() 方法中您处于“后台”线程中。当您需要访问 UI 线程时,请尝试在线程内使用处理程序。

The problem is that you need to be on the UI thread to do modify the UI, and inside the run() method of your Thread you are in a "background" thread. Try using a handler inside your thread when you need to access the UI thread.

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