简体   繁体   English

无法获得旋转进度条工作

[英]Can't get spinning Progress Bar to work

No matter what I try I can't get my progress bar to work. 无论我尝试什么,我都无法让我的进度条工作。 Here is the code: 这是代码:

public void setDayView(final int _day, final int _month, final int _year) {
    ProgressBar progressBar = (ProgressBar)findViewById(R.id.progressBar);
    progressBar.setVisibility(ProgressBar.VISIBLE);
    comm = new Thread() {

        public void run() {
            final postRequest req = new postRequest(username, password);
            doc = (Document) req.GetDayEvents(_day, _month, _year);
        }
    };
    comm.start();
    while (comm.isAlive()) { // waiting for network to finish

    }
    progressBar.setVisibility(ProgressBar.GONE);
    NodeList eventsTitles = doc.getElementsByTagName("Title");
    NodeList eventsClasses = doc.getElementsByTagName("Class");
    NodeList eventsTypes = doc.getElementsByTagName("Type");
    NodeList eventsComments = doc.getElementsByTagName("Comment");
    NodeList eventsAmounts = doc.getElementsByTagName("Amount");
    NodeList eventsHashes = doc.getElementsByTagName("Hash");
    NodeList refCurrency = doc.getElementsByTagName("RefCurrency");
    NodeList dateStamp = doc.getElementsByTagName("DateStamp");

    final int nrOfEvents = eventsTitles.getLength();
    LinearLayout dayEventsArea = (LinearLayout) findViewById(R.id.DayEventsArea);

    // set SelectedDayLabel
    TextView selDayLabel = (TextView) findViewById(R.id.selectedDayLabel);
    selDayLabel.setText(dateStamp.item(0).getTextContent());
    //

    // set day totals labels
    TextView dayDebit = (TextView) findViewById(R.id.dayDebitLabel);
    TextView dayOutgo = (TextView) findViewById(R.id.dayOutgoLabel);
    TextView dayIncome = (TextView) findViewById(R.id.dayIncomeLabel);
    dayDebit.setText("Debit: "
            + doc.getElementsByTagName("DayDebit").item(0).getTextContent()
            + " " + refCurrency.item(0).getTextContent());
    dayOutgo.setText("Outgo: "
            + doc.getElementsByTagName("DayOutgo").item(0).getTextContent()
            + " " + refCurrency.item(0).getTextContent());
    dayIncome.setText("Income: "
            + doc.getElementsByTagName("DayIncome").item(0)
                    .getTextContent() + " "
            + refCurrency.item(0).getTextContent());
    //
    dayEventsArea.removeAllViews();

    // clearing all local caches
    _eventsTitlesLocalCopy.clear();
    _eventsClassesLocalCopy.clear();
    _eventsAmountsLocalCopy.clear();
    _eventsCommentsLocalCopy.clear();
    _eventsTypesLocalCopy.clear();
    _eventsHashesLocalCopy.clear();
    //
    for (int i = 0; i < eventsTitles.getLength(); i++) {
        // creating a local cache of the day events, as they can be edited
        // and sent back
        _eventsTitlesLocalCopy.add(eventsTitles.item(i).getTextContent());
        _eventsClassesLocalCopy.add(eventsClasses.item(i).getTextContent());
        _eventsAmountsLocalCopy.add(eventsAmounts.item(i).getTextContent());
        _eventsCommentsLocalCopy.add(eventsComments.item(i)
                .getTextContent());
        _eventsTypesLocalCopy.add(eventsTypes.item(i).getTextContent());
        _eventsHashesLocalCopy.add(eventsHashes.item(i).getTextContent());
        //

and code from layout.xml 和layout.xml中的代码

<ProgressBar
    android:id="@+id/progressBar"
    style="?android:attr/progressBarStyleLarge"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:indeterminate="true"
    android:layout_gravity="center" />

If I turn off code line progressBar.setVisibility(ProgressBar.GONE); 如果我关闭代码行progressBar.setVisibility(ProgressBar.GONE); then my progress bar is shown all the time, so it actually works. 然后我的进度条一直显示,所以它确实有效。

I would suggest using an AsyncTask for this instead of your Thread-implementation (click on the link for a clear example in the documentation). 我建议使用AsyncTask代替你的Thread实现(点击链接以获得文档中的明确示例)。 You can make the progress bar visible when initiating and hide it again in the onPostExecute method. 您可以在启动时使进度条可见,并在onPostExecute方法中再次隐藏它。 I expect some problem with your while-loop, this would be much cleaner. 我希望你的while循环有一些问题,这会更清晰。

Nevermind, I was able to resolve this by running while loop on a separate thread. 没关系,我能够通过在单独的线程上运行while循环来解决这个问题。 In the code above it runs on UI thread so all the user interface just hangs up while the loop is executed. 在上面的代码中,它在UI线程上运行,因此在执行循环时,所有用户界面都会挂起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android进度栏持续旋转 - Android progress bar keeps spinning Firebase 身份验证 - 在控制台中创建用户但进度条不会停止旋转并且任务未完成? - Firebase authentication - users being created in console but progress bar won't stop spinning and the task is not completed? 我如何在异步任务类中使用循环旋转进度条 - How i can use circular spinning progress bar in my async task class 停止进度条旋转并分散显示输入的结果? - Stop the progress bar from spinning and diisplaying the results entered? 单击按钮时不确定模式的进度条不起作用 - Progress bar with indeterminate mode on a button click doesn't work 如何使进度条与服务器代码同步工作? - How can I make the progress bar work in synchronization with the server code? 带有进度栏的通知无法扩展(牛轧糖+) - Notification with Progress Bar can't be expanded (Nougat+) 为什么我不能在另一个类中设置进度条的值? - Why can't I set the value of a progress bar in another class? 单击按钮后无法显示进度栏 - Can't make progress bar visible after button click 无法创建maven archetype项目,进度条卡在33% - can not create a maven archetype project, the progress bar get stuck in 33%
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM