gpt4 book ai didi

android - ProgressBar setVisibility 不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 11:19:25 24 4
gpt4 key购买 nike

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

在我开始任务之前,我调用了这个函数,以显示一个 prograssBarProgressBar 在 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());
}

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

安卓 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);

最佳答案

尝试将启动 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 .

关于android - ProgressBar setVisibility 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11120575/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com