o19830015 2013-02-25 08:00 采纳率: 12.5%
浏览 2925
已采纳

求助: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)
  • 写回答

2条回答

  • abcdeFGh_xyz 2013-02-25 08:54
    关注

    只有调用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();
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog