ProgressBar setVisibility不起作用

时间:2012-06-20 13:22:29

标签: android progress-bar

我有一个asynchtask用于某些数据库操作

在我开始任务之前,我调用此函数,以显示prograssBar ProgressBar位于RelativLayout中,因为网上有人说在progressBar上调用setVisibility();在某些追逐中不起作用(对我来说也不行)

 public static void showProgressBar()
{
    Log.e("TEST","Try to show ProgressBar, Visibiliy: " + prog_bar.getVisibility());
    if(prog_bar != null)
    {
        prog_bar.setVisibility(RelativeLayout.VISIBLE);
        Log.e("TEST","Success");
    }
    else
    {
        Log.e("TEST","prog_bar != null");
    }
    Log.e("TEST","Visibiliy: " + prog_bar.getVisibility());
}
在PostExecute上

我调用函数:

 public static void hideProgressBar()
{
    Log.e("TEST","Try to hide ProgressBar, Visibiliy: " + prog_bar.getVisibility());
    if(prog_bar != null)
    {
        prog_bar.setVisibility(RelativeLayout.INVISIBLE);
        Log.e("TEST","Success");
    }
    else
    {
        Log.e("TEST","prog_bar != null");
    }
    Log.e("TEST","Visibiliy: " + prog_bar.getVisibility());
}

奇怪的是这些方法被称为正确,日志也是正确的,没有出现错误,但没有显示progressBar。

Android 2.2, Windows 7的, 在模拟器和GalaxyTab上测试

最好的问候schwandi

编辑:

将我的方法更改为:

  public static void hideProgressBar()
{

    if(prog_bar != null)
    {
        Log.e("TEST","Try to hide ProgressBar, Visibiliy: " + prog_bar.getVisibility());
        prog_bar.setVisibility(View.GONE);
        Log.e("TEST","Success");
    }
    else
    {
        Log.e("TEST","prog_bar != null");
    }
    Log.e("TEST","Visibiliy: " + prog_bar.getVisibility());
}

public static void showProgressBar()
{

    if(prog_bar != null)
    {
        Log.e("TEST","Try to show ProgressBar, Visibiliy: " + prog_bar.getVisibility());
        prog_bar.setVisibility(View.VISIBLE);
        Log.e("TEST","Success");
    }
    else
    {
        Log.e("TEST","prog_bar != null");
    }
    Log.e("TEST","Visibiliy: " + prog_bar.getVisibility());
}

仍然不起作用。

我还改变了prog_bar是ProgressBar本身。

  prog_bar = (ProgressBar) findViewById(R.id.progressBar1);

1 个答案:

答案 0 :(得分:0)

尝试将启动asyncTask的整个代码块放入这样的工作线程中(如果不相关,请忽略onClick()):

public void onClick(View v) {
    new Thread(new Runnable() {
        public void run() {

            // your entire asyncTask initiation code block here

        }
    }).start();
}

Here is how it solved my own problem&这里是the docs