我有一个简单的函数,显示在获取数据时加载微调器(通常需要 7 秒左右)
private void load_data(View v)
{
task_complete = false;
progressBar = new ProgressDialog(v.getContext());
progressBar.setCancelable(false);
progressBar.setMessage("Fetching data ...");
progressBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressBar.show();
try
{
Log.v("thread", "Starting thread");
new Thread(new Runnable()
{
public void run()
{
while(!task_complete)
{
fetch_data();
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
progressBar.dismiss();
}
}).start();
Log.v("thread", "Thread finished successfully");
}
catch(Exception e)
{
Log.v("thread", "fail "+e.toString());
}
}
我遇到的问题是 onClick 通过调用该函数
load.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
load_data(v);
show_data(v); // shows the data on a spinner
}
});
当我第二次按下加载按钮时,我得到一个错误。我的日志确实显示
Starting thread
Thread finished successfully
但不知何故,该应用程序因错误而死
03-27 02:48:39.282: E/AndroidRuntime(956): Uncaught handler: thread Thread-9 exiting due to uncaught exception
03-27 02:48:39.293: E/AndroidRuntime(956): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
03-27 02:48:39.293: E/AndroidRuntime(956): at android.view.ViewRoot.checkThread(ViewRoot.java:2683)
如果有人可以向我展示或向我解释导致错误的原因,我将不胜感激