[英]java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
[英]java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
单击按钮时,我正在使用简单线程来执行httpGet到服务器,但是执行后会得到此信息。
Button b_back = (Button) findViewById(R.id.bback);
b_back.setOnClickListener(this);
Button b_sign_up = (Button) findViewById(R.id.signup_button);
b_sign_up.setOnClickListener(this);
@Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
switch (arg0.getId())
{
case R.id.bback:
Intent i = new Intent(this, MainSwitch.class);
finish();
startActivity(i);
break;
// More buttons go here (if any) ...
case R.id.signup_button:
if(username.getText().toString().equalsIgnoreCase("") ||
password.getText().toString().equalsIgnoreCase("") ||
email.getText().toString().equalsIgnoreCase(""))
{
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setMessage("Please fill in all the gaps!");
dialog.show();
}
else
{
//****** Call method that sends the information to server.
Thread background = new Thread (new Runnable()
{
public void run()
{
// Call the time consuming method
handler.sendEmptyMessage(0);
}
});
background.start();
}
}
}
private Handler handler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
Toast.makeText(getApplicationContext(),
"Done thread",
Toast.LENGTH_SHORT).show();
}
};
您得到的错误行:
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
通常与您尝试对非UI线程上的UI元素进行处理的问题有关。
我想通过声明// Call the time consuming method
,可以省去一些代码。 这种耗时的方法在常规Thread
上运行的事实意味着它无法与UI元素进行交互。
如果您发布更多代码(并且还指定了发生错误的行),我们可能会提供有关解决方法的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.