【问题标题】:After loading an activity, it sometimes works, sometimes not, why?加载一个活动后,它有时可以工作,有时不能,为什么?
【发布时间】:2012-06-02 17:35:30
【问题描述】:

我有一个我无法解决的问题,因为它可能是一个错误或类似的东西。我想用 androidplot 做一个图表,效果很好,但当然它需要一些时间来绘制它(使用数百个数据),所以我使用进度对话框来指示加载。但是发生的事情真的很奇怪。 我可以定义活动在加载和加载时的外观。当它加载时,我在后台定义一个文本视图,将其文本设置为“加载”,如果它被加载,该文本视图包含大量数据、文本等。

onCreate
{



        Thread thread = new Thread(new Runnable() {  
               public void run() {

-------what needs to be appeared after its loaded ----
Textview -> 12,3245,456,78,789

}

----what is on the screen while the progressbar is on---
TextView -> loading..
}

但是在进度对话框消失后的大部分时间里,什么都没有发生,textview 仍然说“正在加载”,并且很少加载数据并使数据出现并更改 textview。我注意到,我想出现的数据越多,它就越少停留在加载阶段。当然,每次加载进度条出现时都会消失。 有什么建议吗?这真的很奇怪,因为我在标签布局中使用它,而其他标签从不这样做,总是让数据出现。

提前致谢!

UP:好的,它始终是第一个选项卡,无论它包含什么,所以第一个选项卡在某种程度上是错误的......

【问题讨论】:

    标签: android progress-bar loading


    【解决方案1】:
    The Andoid UI toolkit is not thread-safe. So, you must not manipulate your UI 
    from a worker thread—you must do all manipulation to your user interface from
    the UI thread. Thus, there are simply two rules to Android's single thread model:
    
    1. Do not block the UI thread
    2. Do not access the Android UI toolkit from outside the UI thread 
    

    阅读this,了解有关如何从外部访问 UI 元素的更多信息。
    编辑::

    使用AsyncTask:::

    onCreate
    {
    
         new myLoading().execute();    
    }
    
    class myLoading extends AsyncTask<Void, Void, Void>
    {
         protected Void doInBackground(Void ... ) {
          .......... do all the loading here .........
         }
         protected void onPostExecute(Void ) {
            Textview -> 12,3245,456,78,789
         }
    }
    

    【讨论】:

    【解决方案2】:

    我想出了一些解决方法。我对解决方案没有任何线索,我唯一的猜测是加载屏幕以某种方式超过了所有数据的到达,因此无法绘制图表。管他呢... 我放了一个 thread.sleep(1000) ,现在它可以工作了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-17
      • 2021-09-05
      • 1970-01-01
      • 2015-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多