无法在未调用Looper.prepare()的线程内创建处理程序-setListAdapter [英] Can't create handler inside thread that has not called Looper.prepare() - setListAdapter
问题描述
我正在尝试从线程内部调用android中的setListAdapter来显示列表.我知道我无法从UI线程外访问Android UI,所以我使用的是runOnUiThread,但仍然有一个例外:无法在未调用Looper.prepare()的线程内创建处理程序.谁能告诉我原因以及如何解决?谢谢!
I'm trying to call setListAdapter in android from inside a thread to show a list. I know that I can't acces Android UI from outside a UI thread, so I'm using runOnUiThread, but I still get the exception: Can't create handler inside thread that has not called Looper.prepare(). Can anyone tell me why and how can I fix it? Thanks!
这是我的代码:
public void checkLoginInformation(View view)
{ client_th = new Thread( new Runnable()
{
public void run()
{
try
{
cs = new Socket("192.168.0.101",2014);
final DataOutputStream os = new DataOutputStream(cs.getOutputStream());
final DataInputStream is = new DataInputStream(cs.getInputStream());
String msg = is.readUTF();
if(msg.equals("logat"))
{
final ShowList l = new ShowList();
runOnUiThread(new Runnable() {
@Override
public void run()
{
l.show();
}
});
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
client_th.start();
}
ShowList类:
The ShowList class:
public class ShowList extends ListActivity
{
private final String [] online_drivers = {"one","two","three"};
private final Context context = this;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.online_window);
}
public void show()
{
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
online_drivers));
}
}
还有我得到的例外:
03-12 08:22:03.096 1895-1914/com.example.irinab.licenta2 W/System.err﹕ java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
03-12 08:22:03.096 1895-1914/com.example.irinab.licenta2 W/System.err﹕ at android.os.Handler.<init>(Handler.java:200)
03-12 08:22:03.096 1895-1914/com.example.irinab.licenta2 W/System.err﹕ at android.os.Handler.<init>(Handler.java:114)
03-12 08:22:03.096 1895-1914/com.example.irinab.licenta2 W/System.err﹕ at android.app.Activity.<init>(Activity.java:765)
03-12 08:22:03.096 1895-1914/com.example.irinab.licenta2 W/System.err﹕ at android.app.ListActivity.<init>(ListActivity.java:175)
03-12 08:22:03.096 1895-1914/com.example.irinab.licenta2 W/System.err﹕ at com.example.irinab.licenta2.ShowList.<init>(ShowList.java:14)
03-12 08:22:03.096 1895-1914/com.example.irinab.licenta2 W/System.err﹕ at com.example.irinab.licenta2.MainActivity$1.run(MainActivity.java:120)
03-12 08:22:03.096 1895-1914/com.example.irinab.licenta2 W/System.err﹕ at java.lang.Thread.run(Thread.java:841)
推荐答案
此final ShowList l = new ShowList();
并不是您应该启动新活动的方式.
要开始您的ShowList
活动,请执行以下操作:
This final ShowList l = new ShowList();
is not how you're supposed to start a new Activity.
For starting your ShowList
Activity, do this :
Intent startShowListIntent = new Intent(MainActivity.this, ShowList.class);
startActivity(startShowListIntent);
在上面的代码段中,我假设您当前活动的名称为MainActivity
.
In the above snippet, I'm assuming the name of your current activity to be MainActivity
.
这篇关于无法在未调用Looper.prepare()的线程内创建处理程序-setListAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!