【问题标题】:Android: ProgressBar in ProgressDialog not AnimatingAndroid:ProgressDialog 中的 ProgressBar 没有动画
【发布时间】:2012-07-24 09:17:55
【问题描述】:

我创建了一个 AsyncTask 类用于从后台获取数据(访问数据库)。

onPreExecute()方法中,我创建了一个进度对话框

        try {
            progressDialog = ProgressDialog.show(context, "",
                    "Please wait...", true);
            progressDialog.setIndeterminate(true);

        } catch (final Throwable th) {
            // TODO
        }

在 onpostExecute() 中

        protected void onPostExecute(Boolean result) {

            pinPoint();
            progressDialog.dismiss();
        }

我更新的课程,

@Override
        protected Boolean doInBackground(Void... params) {
            clust = getPinPointClusters();
            runOnUiThread(new Runnable() {
                public void run() {
                    plotClusters(clust);
                }
            });

            return true;
        }

        @Override
        protected void onPostExecute(Boolean result) {

            progressDialog.dismiss();
        }

这里的 pinpoint() 方法将从服务器获取一些数据并将点固定在地图中。 但是 ProgressDialog 中的 progressBar 没有动画...... 请给我最好的方法...

谢谢

【问题讨论】:

  • doInBackGround()中我没有使用pinPoint(),因为这个方法会影响UI
  • getPinPointClusters() 方法是否尝试更新 UI??
  • @Haresh Chaudhary,不,它只会返回 GeoCluster 对象
  • @Haresh Chaudhary,但是 plotCluster() 会更新 UI
  • 试试这个方法..改变这个 runOnUiThread(new Runnable() {...to activityname.runOnUiThread(new Runnable()...

标签: android android-asynctask


【解决方案1】:

我建议您将 pinPoint() 方法 放在 doInBackground method 中,方法 pinPoint() 的行是 影响UI的可以保持在这个方法下::

activityname.runOnUiThread(new Runnable() {
    @Override
        public void run() 
         {
        // Put here the line which is changing the UI.          
     }
});  

示例:当任何一行尝试更改 doInBackground 中的 UI 时,请执行以下操作:

 protected Boolean doInBackground(final String... args) 
   {
     .
     .
     .
     .     
    activityname.runOnUiThread(new Runnable() {
            @Override
                public void run() 
                 {
                   eg. linearLayout.addView(ChildView);         
             }
        });
     .
     .
     . 
   }

现在,您只需解除 onPostExecute() 方法中的ProgressDialog

【讨论】:

  • @Haresh Chaudhary,我遇到了一些运行时异常,例如,无法在未调用 looper.prepare 的线程内创建处理程序
  • @Paresh Mayani,我遇到了一些运行时异常,例如,无法在未调用 looper.prepare 的线程内创建处理程序
  • @Sridhar 因为您在执行线程时尝试更新 UI。这是不可能的,你可以实现runOnUiThread()或者使用AsyncTask的实现onProgressUpdate()
  • 你在用什么代码..请你把它贴在问题部分
  • 我已经在我的回答中提到了关于如何更新 UI 的示例。在我的示例中,您必须假设您正在尝试从 doInBackground 添​​加子视图
【解决方案2】:

“动画”发生在主 UI 线程上。 onPostExecute 也在 UI 线程上运行,因此当您调用服务器时,您实际上是在阻止它。将 pinPoint 移动到 doInBackground 以便它在后台线程上执行,从而使进度对话框保持动画。

在更高版本的 android(Honeycomb [Android 3.0] 及更高版本)上运行代码时,在严格模式下也会抛出 NetworkOnMainThreadException

如果pinPoint 需要更改 UI,您需要将网络调用分离到 doInBackground(+ 数据操作),并且只有 UI 更改为 onPostExecute。您可以在任务中保存对数据的引用或将其作为结果传递。

您还可以使用publishProgress 方法,在doInBackground 期间发布多个 UI 更改。

【讨论】:

    【解决方案3】:

    您已经创建了不同的函数来获取数据和更新 UI 部分

        private class FetchRSSFeeds extends AsyncTask<String, Void, Boolean> {
    
        private ProgressDialog dialog = new ProgressDialog(HomeActivity.this);
    
        protected void onPreExecute() {
            this.dialog.setMessage("Please wait");
            this.dialog.show();
        }
    
        protected Boolean doInBackground(final String... args) {
            try {
                Utilities.arrayRSS = objRSSFeed.FetchRSSFeeds(Constants.Feed_URL);
                return true;
            } catch (Exception e) {
                Log.e("tag", "error", e);
                return false;
            }
        }
    
        @Override
        protected void onPostExecute(final Boolean success) {
    
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
    
            if (success) {
                // Setting data to list adaptar
                setListData();
                txtTitle.setText(Utilities.RSSTitle);
            }
        }
    }
    

    【讨论】:

    • 谢谢@Nirali,但我遇到了一些运行时异常
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多