在Android上加载数据时显示“纺车对话框”

| 我想在我的应用加载某些数据时显示一个纺车对话框: 单击按钮后,将显示出纺车对话框。我正在使用下面的代码,但现在确实显示了转轮。可能是什么问题呢?
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\");
    }
}
    
已邀请:
        因此,看到这个答案越来越受到人们的认可,我决定自己考虑提高答案的质量。当我看原始文档时,看不到“代码中的问题是什么?”这个问题的真实答案。我将原始答案保留在下面,以保留链接,这是我假定导致答案普及的原因。 更新的答案 您可能违反了“单线程模型”。 Android UI工具包不是线程安全的,只能从\'main \'UI线程进行操作。 Android有一些有用的方法,可用于确保在UI线程上完成UI操作。这些方法调用的大多数详细信息都可以在Android Painless Threading博客文章中找到(链接在下面的单词“ one \”中,此处供快速参考)。 查看您的代码,我看到的特定冲突是在可能的UI线程中创建了一个“ 1”,然后在新创建的后台线程中将其删除。 您最好将背景方法封装在
Runnable
中,然后使用
View#post(Runnable)
在后台进行工作。 记住,有很多方法可以进行背景分析,因此请查看可用的背景,并使用适合您的情况的背景。 原始答案 看一下如何在Android中进行异步工作的众多教程之一。 另外,这是一些其他类似的StackOverflow问题 Android中的进度对话框问题 开放活动的进度对话框 开始新活动时的进度对话框 Android:进度对话框微调器不旋转 更新进度对话框 android:显示进度对话框     
        去创造。
ProgressDialog dialog = new ProgressDialog(this); // this = YourActivity
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setTitle(\"Loading\");
dialog.setMessage(\"Loading. Please wait...\");
dialog.setIndeterminate(true);
dialog.setCanceledOnTouchOutside(false);
显示。
dialog.show();
解雇。
dialog.dismiss();
    
        我遇到了在两个活动(活动A和活动B)之间进行切换的问题。活动B有长时间运行的任务,正在更新UI,因此它不是AsyncTask的候选对象。我通过在活动A上启动进度对话框并在完成第一个活动之前启动活动B来解决该问题。该代码如下所示:
            // wrap thread around original code for progress button
            final ProgressDialog ringProgressDialog = ProgressDialog.show(getActivity(), \"Working\", \"Please Wait\",
                    true, false);
            ringProgressDialog.setIndeterminate(true);
            new Thread(new Runnable() {
                public void run() {
                    try {

                        Intent startNewActivityOpen = new Intent(getActivity(), ActivityB.class);
                        startNewActivityOpen.setFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
                        startActivity(startNewActivityOpen);
                        GlobalVars.navigateFromScreen = true;

                        Thread.sleep(3000);


                        if (ringProgressDialog != null && ringProgressDialog.isShowing()) {
                            ringProgressDialog.dismiss();
                        }

                        getActivity().finish();


                    } catch (Exception e) {

                    }

                }

            }).start();
    

要回复问题请先登录注册