【问题标题】:Android: Progressbar stops on async taskAndroid:进度条在异步任务上停止
【发布时间】:2014-12-17 06:46:29
【问题描述】:

我正在开发一个应用程序。我的布局如下:

代码如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg" >

<ImageView
    android:id="@+id/imageview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_margin="20dp"
    android:contentDescription="@string/app_name"
    android:src="@drawable/logo" />

<ImageView
    android:id="@+id/imageview_1"
    android:layout_width="120sp"
    android:layout_height="120sp"
    android:layout_below="@+id/imageview"
    android:layout_centerHorizontal="true"
    android:layout_margin="20dp"
    android:background="@drawable/appicon"
    android:contentDescription="@string/app_name" />

<ProgressBar
    android:id="@+id/progressBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/imageview_1"
    android:layout_centerHorizontal="true" />

<TextView
    android:id="@+id/textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/progressBar"
    android:layout_centerHorizontal="true"
    android:textColor="@android:color/white"
    android:layout_margin="20sp" />

</RelativeLayout>

现在我的 java 代码流程为:

onCreate => Async task 1
doInBackground of Async task 1 => function 1 => from function 1 to function 2 => from function 2 to function 3 = > from function 3 return.

from onPost of Async task 1 => call Async task 2
doInBackground of Async task 2 => function a => from function a to function b => from function b to return.

from onPost of Async task 2 => call Async task 3
doInBackground of Async task 3 => function x => from function x to function y => from function y to return.

这是我的应用程序的固定流程。

问题是:当我的活动运行时,进度条运行良好,但当异步任务执行开始时,进度条停止移动。

我尝试了链接中的解决方案:

Link

但是输出是一样的。

我还想在异步 1 上更改“运行时更改文本视图”的文本,然后在异步 2 和异步 3 上再次更改。

请建议我如何处理您的宝贵建议。如果需要,请回复以进一步澄清问题。

【问题讨论】:

  • 显示进度条代码
  • @manojFedge 只是一个问题,为什么不使用 1 个 asyncTask 执行所有工作(调用所有函数)?
  • 您在 cat 日志中看到任何错误?如果您点击了该链接,那么您的代码流中的某些对象可能会被锁定。
  • 我没有收到任何错误。
  • @Yazan :因为我在函数中执行的任务是可重用的。所以我不能异步编写整个代码。这就是app的需要。

标签: android android-layout android-asynctask android-ui android-progressbar


【解决方案1】:

在你的xml中,添加

android:indeterminate="true"

为您的 ProgressBar。这将启用无限动画。

我希望这会有所帮助。

【讨论】:

  • 感谢您的回复。我的问题由您的回答部分解决。但是现在到完成活动之前,进度条会停止一段时间。请让我知道如何从 async 2 更改文本。
  • 要更改文本,您可以使用 AsyncTask 内部使用的 onProgressUpdate 方法。检查developer.android.com/reference/android/os/AsyncTask.html。查看 onProgressUpdate 方法以从您的 AsyncTask 更新 UI 线程
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-16
  • 1970-01-01
  • 1970-01-01
  • 2020-04-30
  • 2012-08-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多