4

我有一些数据库操作的异步任务

在我开始任务之前,我调用了这个函数,以显示一个 prograssBar ProgressBar 在 RelativLayout 中,因为网上有人说调用setVisibility();progressBar 在某些追逐中不起作用(而且它也不适合我)

 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());
}

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

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

最好的问候施万迪

编辑:

将我的方法更改为:

  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);
4

1 回答 1

0

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

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

            // your entire asyncTask initiation code block here

        }
    }).start();
}

这是它如何解决我自己的问题&这里是文档

于 2016-11-23T14:44:31.447 回答