由网友(遇你)分享简介:我想表现出纺进步,而我装载一些数据,但它没有显示在加载数据后,才能?下面是我如何试图做到这一点:公共无效的onCreate(包savedInstanceState){super.onCreate(savedInstanceState);的setContentView(R.layout.screen1);//显示微调在...
我想表现出纺进步,而我装载一些数据,但它没有显示在加载数据后,才能?
下面是我如何试图做到这一点:
公共无效的onCreate(包savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.screen1);
//显示微调在数据被加载
ProgressDialog对话框= ProgressDialog.show(这一点,,请耐心等待...,真正的);
加载preferences();
LoadData();
//删除微调一旦所有的数据已被加载
dialog.dismiss();
}
我是什么做错了吗?
更新code:
公共无效的onCreate(包savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.screen1);
//显示微调在数据被加载
最后ProgressDialog对话框= ProgressDialog.show(这一点,,请耐心等待...,真正的);
//定义和运行后台线程
螺纹backgroundThread =新主题(新的Runnable()
{
公共无效的run()
{
//继续确保该业务
//是线程安全的!
活套prepare()。 //我必须包括这prevent强制关闭错误
加载preferences();
LoadData();
runOnUiThread(新的Runnable()
{
公共无效的run()
{
dialog.dismiss();
}
});
}
});
backgroundThread.start();
}
这现在加载应用程序,并给出了微调。但我LoadData()函数试图更新UI时崩溃的:
12-27 21:58:12.575:ERROR / AndroidRuntime(357):未捕获的处理程序:螺纹螺纹-8出口由于未捕获的异常
12-27 21:58:12.575:ERROR / AndroidRuntime(357):android.view.ViewRoot $ CalledFromWrongThreadException:只有创建视图层次可以触摸其观点原来的线程。
12-27 21:58:12.575:ERROR / AndroidRuntime(357):在android.view.ViewRoot.checkThread(ViewRoot.java:2683)
12-27 21:58:12.575:ERROR / AndroidRuntime(357):在android.view.ViewRoot.requestLayout(ViewRoot.java:557)
12-27 21:58:12.575:ERROR / AndroidRuntime(357):在android.view.View.requestLayout(View.java:7918)
12-27 21:58:12.575:ERROR / AndroidRuntime(357):在android.view.View.requestLayout(View.java:7918)
12-27 21:58:12.575:ERROR / AndroidRuntime(357):在android.view.View.requestLayout(View.java:7918)
12-27 21:58:12.575:ERROR / AndroidRuntime(357):在android.view.View.requestLayout(View.java:7918)
12-27 21:58:12.575:ERROR / AndroidRuntime(357):在android.view.View.requestLayout(View.java:7918)
12-27 21:58:12.575:ERROR / AndroidRuntime(357):在android.view.View.requestLayout(View.java:7918)
12-27 21:58:12.575:ERROR / AndroidRuntime(357):在android.view.View.requestLayout(View.java:7918)
12-27 21:58:12.575:ERROR / AndroidRuntime(357):在android.widget.TableLayout.requestLayout(TableLayout.java:223)
12-27 21:58:12.575:ERROR / AndroidRuntime(357):在android.view.View.requestLayout(View.java:7918)
12-27 21:58:12.575:ERROR / AndroidRuntime(357):在android.widget.ScrollView.requestLayout(ScrollView.java:1103)
12-27 21:58:12.575:ERROR / AndroidRuntime(357):在android.view.View.requestLayout(View.java:7918)
12-27 21:58:12.575:ERROR / AndroidRuntime(357):在android.view.View.requestLayout(View.java:7918)
12-27 21:58:12.575:ERROR / AndroidRuntime(357):在android.view.View.requestLayout(View.java:7918)
12-27 21:58:12.575:ERROR / AndroidRuntime(357):在android.widget.TextView.checkForRelayout(TextView.java:5373)
12-27 21:58:12.575:ERROR / AndroidRuntime(357):在android.widget.TextView.setText(TextView.java:2684)
12-27 21:58:12.575:ERROR / AndroidRuntime(357):在android.widget.TextView.setText(TextView.java:2552)
12-27 21:58:12.575:ERROR / AndroidRuntime(357):在android.widget.TextView.setText(TextView.java:2527)
12-27 21:58:12.575:ERROR / AndroidRuntime(357):在com.bebetech.helloWorld.helloWorld.UpdateGUI(helloWorld.java:346)
12-27 21:58:12.575:ERROR / AndroidRuntime(357):在com.bebetech.helloWorld.helloWorld.LoadData(helloWorld.java:191)
12-27 21:58:12.575:ERROR / AndroidRuntime(357):在com.bebetech.helloWorld.helloWorld $ 1.运行(helloWorld.java:106)
12-27 21:58:12.575:ERROR / AndroidRuntime(357):在java.lang.Thread.run(Thread.java:1096)
解决方案
是米奇小麦的是绝对正确的。
下面有什么,你必须编辑:
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
//显示微调在数据被加载
最后ProgressDialog对话框= ProgressDialog.show(这一点,,请耐心等待...,真正的);
线程线程=新主题(新的Runnable(){
公共无效的run(){
加载preferences();
LoadData();
runOnUiThread(新的Runnable(){
@覆盖
公共无效的run(){
如果(dialog.isShowing())
dialog.dismiss();
}
});
}
});
thread.start();
}
I am trying to show a spinning progress while I load some data, but it isn't showing until after the data is loaded?
Here is how I am trying to do this:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.screen1);
//Show spinner while data is being loaded
ProgressDialog dialog = ProgressDialog.show(this, "", "Loading. Please wait...", true);
LoadPreferences();
LoadData();
//Remove the spinner once all the data has been loaded
dialog.dismiss();
}
What am I doing wrong?
Updated code:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.screen1);
//Show spinner while data is being loaded
final ProgressDialog dialog = ProgressDialog.show(this, "", "Loading. Please wait...", true);
// define and run background thread
Thread backgroundThread = new Thread(new Runnable()
{
public void run()
{
// keep sure that this operations
// are thread-safe!
Looper.prepare(); //I had to include this to prevent force close error
LoadPreferences();
LoadData();
runOnUiThread(new Runnable()
{
public void run()
{
dialog.dismiss();
}
});
}
});
backgroundThread.start();
}
This now loads the app and shows the spinner. But my LoadData() function is crashing it when trying to update the UI:
12-27 21:58:12.575: ERROR/AndroidRuntime(357): Uncaught handler: thread Thread-8 exiting due to uncaught exception
12-27 21:58:12.575: ERROR/AndroidRuntime(357): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
12-27 21:58:12.575: ERROR/AndroidRuntime(357): at android.view.ViewRoot.checkThread(ViewRoot.java:2683)
12-27 21:58:12.575: ERROR/AndroidRuntime(357): at android.view.ViewRoot.requestLayout(ViewRoot.java:557)
12-27 21:58:12.575: ERROR/AndroidRuntime(357): at android.view.View.requestLayout(View.java:7918)
12-27 21:58:12.575: ERROR/AndroidRuntime(357): at android.view.View.requestLayout(View.java:7918)
12-27 21:58:12.575: ERROR/AndroidRuntime(357): at android.view.View.requestLayout(View.java:7918)
12-27 21:58:12.575: ERROR/AndroidRuntime(357): at android.view.View.requestLayout(View.java:7918)
12-27 21:58:12.575: ERROR/AndroidRuntime(357): at android.view.View.requestLayout(View.java:7918)
12-27 21:58:12.575: ERROR/AndroidRuntime(357): at android.view.View.requestLayout(View.java:7918)
12-27 21:58:12.575: ERROR/AndroidRuntime(357): at android.view.View.requestLayout(View.java:7918)
12-27 21:58:12.575: ERROR/AndroidRuntime(357): at android.widget.TableLayout.requestLayout(TableLayout.java:223)
12-27 21:58:12.575: ERROR/AndroidRuntime(357): at android.view.View.requestLayout(View.java:7918)
12-27 21:58:12.575: ERROR/AndroidRuntime(357): at android.widget.ScrollView.requestLayout(ScrollView.java:1103)
12-27 21:58:12.575: ERROR/AndroidRuntime(357): at android.view.View.requestLayout(View.java:7918)
12-27 21:58:12.575: ERROR/AndroidRuntime(357): at android.view.View.requestLayout(View.java:7918)
12-27 21:58:12.575: ERROR/AndroidRuntime(357): at android.view.View.requestLayout(View.java:7918)
12-27 21:58:12.575: ERROR/AndroidRuntime(357): at android.widget.TextView.checkForRelayout(TextView.java:5373)
12-27 21:58:12.575: ERROR/AndroidRuntime(357): at android.widget.TextView.setText(TextView.java:2684)
12-27 21:58:12.575: ERROR/AndroidRuntime(357): at android.widget.TextView.setText(TextView.java:2552)
12-27 21:58:12.575: ERROR/AndroidRuntime(357): at android.widget.TextView.setText(TextView.java:2527)
12-27 21:58:12.575: ERROR/AndroidRuntime(357): at com.bebetech.helloWorld.helloWorld.UpdateGUI(helloWorld.java:346)
12-27 21:58:12.575: ERROR/AndroidRuntime(357): at com.bebetech.helloWorld.helloWorld.LoadData(helloWorld.java:191)
12-27 21:58:12.575: ERROR/AndroidRuntime(357): at com.bebetech.helloWorld.helloWorld.$1.run(helloWorld.java:106)
12-27 21:58:12.575: ERROR/AndroidRuntime(357): at java.lang.Thread.run(Thread.java:1096)
解决方案
Yes, Mitch Wheat is absolutely correct.
Heres what you have to edit:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Show spinner while data is being loaded
final ProgressDialog dialog = ProgressDialog.show(this, "", "Loading. Please wait...", true);
Thread thread=new Thread(new Runnable(){
public void run(){
LoadPreferences();
LoadData();
runOnUiThread(new Runnable(){
@Override
public void run() {
if(dialog.isShowing())
dialog.dismiss();
}
});
}
});
thread.start();
}
相关推荐
最新文章