android-求助:Progressbar error

问题描述

求助:Progressbar error

我要在获取数据时显示进度条

代码:

public void progressBar() {
        progress = ProgressDialog.show(this, "dialog title",
                  "dialog message", true);

                new Thread(new Runnable() {
                  public void run()
                  {
                    // do the thing that takes a long time
                      displayView();
                    runOnUiThread(new Runnable() {
                      public void run()
                      {
                        progress.dismiss();
                      }
                    });
                  }
                }).start();
    }

然后是displayView();方法:

public void displayView() {

        listAdapter = new ArrayAdapter<String>(this, R.layout.listview_format,
                R.id.item_list,handler.getRetrieveData());
        listView = (ListView)findViewById(R.id.listView2);
        listView.setAdapter(listAdapter);

        listView.setOnItemClickListener( new OnItemClickListener() {
            public void onItemClick(AdapterView<?> arg0, View v, int position ,
                    long id) {

                intent = new Intent(TechiesActivity.this,ItemInfoActivity.class);
                startActivity(intent);
            }
        });

    }

报出的错误logcat:

02-25 14:39:49.980: E/AndroidRuntime(1654): FATAL EXCEPTION: Thread-10
02-25 14:39:49.980: E/AndroidRuntime(1654): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
02-25 14:39:49.980: E/AndroidRuntime(1654):     at android.view.ViewRoot.checkThread(ViewRoot.java:2802)
02-25 14:39:49.980: E/AndroidRuntime(1654):     at android.view.ViewRoot.invalidateChild(ViewRoot.java:607)
02-25 14:39:49.980: E/AndroidRuntime(1654):     at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:633)
02-25 14:39:49.980: E/AndroidRuntime(1654):     at android.view.ViewGroup.invalidateChild(ViewGroup.java:2505)
02-25 14:39:49.980: E/AndroidRuntime(1654):     at android.view.View.invalidate(View.java:5139)
02-25 14:39:49.980: E/AndroidRuntime(1654):     at android.widget.AbsListView.resetList(AbsListView.java:1011)
02-25 14:39:49.980: E/AndroidRuntime(1654):     at android.widget.ListView.resetList(ListView.java:493)
02-25 14:39:49.980: E/AndroidRuntime(1654):     at android.widget.ListView.setAdapter(ListView.java:422)
02-25 14:39:49.980: E/AndroidRuntime(1654):     at com.shop.browser.TechiesActivity.displayView(TechiesActivity.java:111)
02-25 14:39:49.980: E/AndroidRuntime(1654):     at com.shop.browser.TechiesActivity$1.run(TechiesActivity.java:75)
02-25 14:39:49.980: E/AndroidRuntime(1654):     at java.lang.Thread.run(Thread.java:1096)
02-25 14:39:51.629: E/WindowManager(1654): Activity com.shop.browser.TechiesActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44f30660 that was originally added here
02-25 14:39:51.629: E/WindowManager(1654): android.view.WindowLeaked: Activity com.shop.browser.TechiesActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44f30660 that was originally added here
02-25 14:39:51.629: E/WindowManager(1654):  at android.view.ViewRoot.<init>(ViewRoot.java:247)
02-25 14:39:51.629: E/WindowManager(1654):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
02-25 14:39:51.629: E/WindowManager(1654):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
02-25 14:39:51.629: E/WindowManager(1654):  at android.view.Window$LocalWindowManager.addView(Window.java:424)
02-25 14:39:51.629: E/WindowManager(1654):  at android.app.Dialog.show(Dialog.java:241)
02-25 14:39:51.629: E/WindowManager(1654):  at android.app.ProgressDialog.show(ProgressDialog.java:107)
02-25 14:39:51.629: E/WindowManager(1654):  at android.app.ProgressDialog.show(ProgressDialog.java:90)
02-25 14:39:51.629: E/WindowManager(1654):  at com.shop.browser.TechiesActivity.progressBar(TechiesActivity.java:68)
02-25 14:39:51.629: E/WindowManager(1654):  at com.shop.browser.TechiesActivity.onClick(TechiesActivity.java:198)
02-25 14:39:51.629: E/WindowManager(1654):  at java.lang.reflect.Method.invokeNative(Native Method)
02-25 14:39:51.629: E/WindowManager(1654):  at java.lang.reflect.Method.invoke(Method.java:521)
02-25 14:39:51.629: E/WindowManager(1654):  at android.view.View$1.onClick(View.java:2067)
02-25 14:39:51.629: E/WindowManager(1654):  at android.view.View.performClick(View.java:2408)
02-25 14:39:51.629: E/WindowManager(1654):  at android.view.View$PerformClick.run(View.java:8816)
02-25 14:39:51.629: E/WindowManager(1654):  at android.os.Handler.handleCallback(Handler.java:587)
02-25 14:39:51.629: E/WindowManager(1654):  at android.os.Handler.dispatchMessage(Handler.java:92)
02-25 14:39:51.629: E/WindowManager(1654):  at android.os.Looper.loop(Looper.java:123)
02-25 14:39:51.629: E/WindowManager(1654):  at android.app.ActivityThread.main(ActivityThread.java:4627)
02-25 14:39:51.629: E/WindowManager(1654):  at java.lang.reflect.Method.invokeNative(Native Method)
02-25 14:39:51.629: E/WindowManager(1654):  at java.lang.reflect.Method.invoke(Method.java:521)
02-25 14:39:51.629: E/WindowManager(1654):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-25 14:39:51.629: E/WindowManager(1654):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-25 14:39:51.629: E/WindowManager(1654):  at dalvik.system.NativeStart.main(Native Method)

解决方案

只有调用runOnUiThread方法中的DisplayView就可以了。

public void progressBar() {
        progress = ProgressDialog.show(this, "dialog title",
                  "dialog message", true);

                new Thread(new Runnable() {
                  public void run()
                  {
                    // do the thing that takes a long time
                      runOnUiThread(new Runnable() {
                      public void run()
                      {
                        displayView();
                        progress.dismiss();
                      }
                    });
                  }
                }).start();
    }

解决方案二:

不能从背景线程中执行UI更新,因此方法

displayView();

应该在runOnUiThread()中调用:

runOnUiThread(new Runnable() {
   public void run() {
      displayView();
      progress.dismiss();
   }
});
时间: 2024-10-26 18:07:22

android-求助:Progressbar error的相关文章

eclipse 每次打开xml文件都会一直loadding data for android 求助

问题描述 eclipse 每次打开xml文件都会一直loadding data for android 求助 eclipse 每次打开xml文件都会一直loadding data for android, 如下图 , 求助 解决方案 它要加载Android的库,不影响你使用就行 解决方案二: 去除eclipse中xml文件的 No grammar constraints (DTD or XML schema) detected for the document警告每次运行都会自动生成****ou

android-关于Android中ProgressBar的问题

问题描述 关于Android中ProgressBar的问题 ProgressBar中的ProgressDrawable和IndeterminateDrawable两种属性有什么不同呢?他们俩的应用场景是什么? 解决方案 IndeterminateDrawable是用来表示没法估计准确进度的场景,显示一个动画来等待.ProgressDrawable表示知道进度,有百分比,可以估计剩余时间. IndeterminateDrawable一般在用户请求网络数据时,一个圆形不断旋转的progressBar

Android编程ProgressBar自定义样式之动画模式实现方法_Android

本文实例讲述了Android编程ProgressBar自定义样式之动画模式实现方法.分享给大家供大家参考,具体如下: 忘记在哪里看到的那位仁兄写的,就是通过用动画效果来实现的,现在顺便也把他写出来,希望那位仁兄不要见怪. 效果: 和之前的一样,在布局文件中: <ProgressBar android:id="@+id/progressBar3" android:layout_width="wrap_content" android:layout_height=

Android Gradle Build Error:Some file crunching failed, see logs for details的快速解决方法_Android

错误日志:Error:java.lang.RuntimeException: Some file crunching failed, see logs for details Log: FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':app:mergeDebugResources'. > Error: java.lang.RuntimeException: Crunch

Android中ProgressBar用法简单实例

本文实例讲述了Android中ProgressBar用法.分享给大家供大家参考,具体如下: 在android中会经常用到ProgressBar,下面通过举例来说明如何使用ProgressBar. import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.vi

Android编程ProgressBar自定义样式之动画模式实现方法

本文实例讲述了Android编程ProgressBar自定义样式之动画模式实现方法.分享给大家供大家参考,具体如下: 忘记在哪里看到的那位仁兄写的,就是通过用动画效果来实现的,现在顺便也把他写出来,希望那位仁兄不要见怪. 效果: 和之前的一样,在布局文件中: <ProgressBar android:id="@+id/progressBar3" android:layout_width="wrap_content" android:layout_height=

Android Gradle Build Error:Some file crunching failed, see logs for details解决办法

Android Gradle Build Error:Some file crunching failed, see logs for details解决办法 错误日志:Error:java.lang.RuntimeException: Some file crunching failed, see logs for details Log: FAILURE: Build failed with an exception. * What went wrong: Execution failed

Android Gradle Build Error:Some file crunching failed, see logs for details的快速解决方法

错误日志:Error:java.lang.RuntimeException: Some file crunching failed, see logs for details Log: FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':app:mergeDebugResources'. > Error: java.lang.RuntimeException: Crunch

android自定义ProgressBar(仿淘宝)的加载效果

三种方式实现自定义圆形页面加载中效果的进度条  To get a ProgressBar in the default theme that is to be used on white/light back ground, use one of the inverse styles: <ProgressBar style="@android:style/Widget.ProgressBar.Inverse"/> <ProgressBar style="@a