Я пытаюсь показать процесс вращения во время загрузки некоторых данных, но он не отображается до тех пор, пока данные не загружены?
Вот как я пытаюсь это сделать:
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();
}
Что я делаю не так?
Обновленный код:
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();
}
Теперь загружается приложение и отображается счетчик.Но моя функция LoadData () вызывает сбой при попытке обновить интерфейс:
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)