Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
156 views
in Technique[技术] by (71.8m points)

java - Show spinning wheel dialog while loading data on Android

I want to show a spinning wheel dialog while my app loads some data:

Spinning wheel dialog

The spinning wheel dialog should show on a button click. I’m using the code below but it does now show the spinning wheel. What could be the problem?

public void CheckAccount(String username, String password) {
    try {

        final ProgressDialog progDailog = ProgressDialog.show(this,
            "Progress_bar or give anything you want",
            "Give message like ....please wait....", true);
        new Thread() {
            public void run() {
                try {
                    // sleep the thread, whatever time you want. 
                    sleep(1000);
                } catch (Exception e) {
                }
                progDailog.dismiss();
            }
        }.start();

        //getting data code here
        //getting data code here
        //getting data code here
        //getting data code here
        //getting data code here

    } catch (Exception e) {
        Log.e(LOG_TAG, e.getMessage());
        PopIt("CheckAccountError", e.getMessage(), "Denied");
    }
}
question from:https://stackoverflow.com/questions/6159702/show-spinning-wheel-dialog-while-loading-data-on-android

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

So, seeing that this answer is getting more and more upvotes, I have decided that I should take it upon myself to improve the quality of the answer. As I look at the original, I see no real 'answer' to the question of "what is the problem in my code?". I am leaving the original answer intact below to preserve the linking, which is what I am assuming has caused the answers popularity.

Updated Answer

It is likely that you are violating the 'single thread model'. The Android UI Toolkit is not threadsafe, and should only be manipulated from the 'main' UI thread. Android has a handful of helpful methods you can use to ensure that your UI manipulations are done on the UI thread. Most of the details of these method calls can be found in the Android Painless Threading blog post (linked in the word 'one' below, and here for quick reference).

Looking at your code, the specific violation I see is that a ProgressDialog is being created in what is likely the UI thread, which is then dismissed in the newly created background thread.

You would be better off encapsulating your background method in a Runnable and using View#post(Runnable) to do the work in the background.

Remember, there are many ways to do background, so take a look at what is available, and use what feels right for your situation.

Original Answer

Take a look at one of the many tutorials of how to do asynchronous work in Android.

Also, here are some other StackOverflow questions that are similar

Progress dialog problem in Android

Progress Dialog on open activity

Progress Dialog while starting new activity

Android: Progress Dialog spinner not spinning

Updating progress dialog

android: showing a progress dialog


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...