更新的用户界面在一个线程通过处理程序的主要活动(安卓) [英] Update UI in the main activity through handler in a thread (Android)
问题描述
我试图在一个类多个连接和更新多个进度在主屏幕上。
I try to make several connection in a class and update the multiple progressbar in the main screen.
但我得到了以下错误尝试使用线程的android: code: 05-06 13:13:11.092:错误/的ConnectionManager(22854):ERROR:内螺纹已经不叫尺蠖prepare无法创建处理程序()
But I've got the following error trying to use thread in android : Code: 05-06 13:13:11.092: ERROR/ConnectionManager(22854): ERROR:Can't create handler inside thread that has not called Looper.prepare()
下面是我的code的主要活动的一小部分。
Here is a small part of my code in the main Activity
public class Act_Main extends ListActivity
{
private ConnectionManager cm;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Set up the window layout
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);
}
public void startConnection()
{
//open DB connection
db = new DBAdapter(getApplicationContext());
db.open();
cm = new ConnectionManager(handler, db);
showDialog(DIALOG_PROGRESS_LOGIN);
}
@Override
public void onStart()
{
super.onStart();
startConnection();
}
protected Dialog onCreateDialog(int id)
{
switch (id)
{
case DIALOG_PROGRESS_LOGIN:
progressDialog = new ProgressDialog(Act_Main.this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("Connecting.\nPlease wait...");
progressThreadLogin = new ProgressThreadLogin();
progressThreadLogin.start();
return progressDialog;
case DIALOG_PROGRESS_NETWORK:
[b]progressDialog = new ProgressDialog(Act_Main.this);[/b]
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("Loading entire network.\nPlease wait...");
progressThreadNetwork = new ProgressThreadNetwork();
progressThreadNetwork.start();
return progressDialog;
default:
return null;
}
}
// Define the Handler that receives messages from the thread and update the progress
final Handler handler = new Handler()
{
public void handleMessage(Message msg)
{
int total = msg.getData().getInt("total");
int step = msg.getData().getInt("step");
Log.d(TAG, "handleMessage:PROCESSBAR:"+total);
progressDialog.setProgress(total);
if (total >= 100)
{
switch (step)
{
case UPDATE_NETWORK:
dismissDialog(DIALOG_PROGRESS_LOGIN);
showDialog(DIALOG_PROGRESS_NETWORK);
cm.getNetwork();
break;
....
default:
break;
}
}
}
};
private class ProgressThreadLogin extends Thread
{
ProgressThreadLogin() { }
public void run() { cm.login(); }
}
private class ProgressThreadNetwork extends Thread
{
ProgressThreadNetwork() { }
public void run() { cm.getNetwork(); }
}
}
和我的ConnectionManager类:
And my connectionManager class:
public class ConnectionManager
{
public ConnectionManager(Handler handler, DBAdapter db)
{
this.handler = handler;
this.db = db;
}
public void updateProgressBar(int step, int value)
{
if (value == 0)
total = total+1;
else
total = value ;
Message msg = handler.obtainMessage();
Bundle b = new Bundle();
b.putInt("total", total);
b.putInt("step", step);
msg.setData(b);
handler.handleMessage(msg);
}
public void login()
{
//DO MY LOGIN TASK
updateProgressBar(Act_Main.UPDATE_NETWORK, 100);
}
}
崩溃的错误发生的第一道防线的情况下DIALOG_PROGRESS_NETWORK:。我的第一个进度条是隐藏的,但不显示第二个。
The crash errors occurs on the first line of "case DIALOG_PROGRESS_NETWORK:". My first progressbar is hidden but the second one is not displayed.
我想我已经使用了线程和处理程序做了财产以后错了,但我不'知道为什么。
I think I've done somthing wrong using the threads and handlers but I dont' know why.
我是第一次使用handler.sendMessage到位handler.handleMessage的,但是当我在我的ConnectionManager有几个任务,进度条进行了更新,仅在所有任务的结束。
I was first using handler.sendMessage in place of handler.handleMessage but when I had several task in my connectionManager, the progressbar was updated only at the end of all tasks.
感谢您在您的帮助
推荐答案
请处理程序处理程序
非终局的,里面的的onCreate初始化它()
。
这篇关于更新的用户界面在一个线程通过处理程序的主要活动(安卓)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!