gpt4 book ai didi

java.lang.RuntimeException : Can't create handler inside thread that has not called Looper. 准备()

转载 作者:行者123 更新时间:2023-12-01 07:39:35 25 4
gpt4 key购买 nike

当单击按钮时,我使用简单的线程执行 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 元素执行操作的问题相关。

我想通过声明//调用耗时的方法你已经遗漏了一些代码。事实上,这个耗时的方法在常规 Thread 上运行,这意味着它无法与 UI 元素交互。

如果您发布更多代码(以及指定发生错误的行),我们可能可以提供有关如何解决该问题的更多信息。

关于java.lang.RuntimeException : Can't create handler inside thread that has not called Looper. 准备(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6880662/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com