java.lang.RuntimeException:Can';t在未调用Looper.prepare()的线程内创建处理程序
当单击按钮时,我使用简单线程来执行httpGet到服务器,但在执行之后我得到了这个消息java.lang.RuntimeException:Can';t在未调用Looper.prepare()的线程内创建处理程序,java,android,multithreading,handler,looper,Java,Android,Multithreading,Handler,Looper,当单击按钮时,我使用简单线程来执行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 o
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元素执行操作的问题相关
我认为,通过声明//调用耗时的方法,您遗漏了一些代码。这个耗时的方法在常规线程上运行,这意味着它无法与UI元素交互
如果您发布更多代码(以及指定错误发生的行),我们可能会提供更多关于如何解决该问题的信息。我们可以了解一些上下文吗?您在这里使用的是什么框架<代码>活套
不是众所周知的事情。请在代码中指出导致错误的行