gpt4 book ai didi

java - 无法在未调用 Looper.prepare() 的线程内创建处理程序

转载 作者:IT老高 更新时间:2023-10-28 23:19:10 27 4
gpt4 key购买 nike

我收到此错误“无法在未调用 Looper.prepare() 的线程内创建处理程序”

你能告诉我如何解决它吗?

public class PaymentActivity extends BaseActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.payment);

final Button buttonBank = (Button) findViewById(R.id.buttonBank);

buttonBank.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
progressDialog = ProgressDialog.show(PaymentActivity.this, "",
"Redirecting to payment gateway...", true, true);

new Thread() {
public void run() {
try {
startPayment("Bank");
} catch (Exception e) {
alertDialog.setMessage(e.getMessage());
handler.sendEmptyMessage(1);
progressDialog.cancel();
}
}
}.start();
}

});

开始付款方式:

    private void startPayment(String id) {
Bundle b = getIntent().getExtras();
final Sail sail = b.getParcelable(Constant.SAIL);

final Intent bankIntent = new Intent(this, BankActivity.class);

try {
Reservation reservation = RestService.createReservation(
sail.getId(),
getSharedPreferences(Constant.PREF_NAME_CONTACT, 0));
bankIntent.putExtra(Constant.RESERVATION, reservation);

// <workingWithDB> Storing Reservation info in Database
DBAdapter db = new DBAdapter(this);
db.open();
@SuppressWarnings("unused")
long rowid;
rowid = db.insertRow(sail.getId(), sail.getFrom(),
sail.getTo(), sail.getShip(), sail.getDateFrom().getTime(),
sail.getPrice().toString(), reservation.getId().floatValue());
db.close();
// </workingWithDB>

String html = PaymentService.getRedirectHTML(id, reservation);

bankIntent.putExtra(Constant.BANK, html);
} catch (Exception e) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
AlertDialog alertDialog = builder.create();
alertDialog.setMessage(e.getMessage());
alertDialog.show();
}

startActivity(bankIntent);
}

最佳答案

您应该知道,当您尝试修改您的 UI 时,唯一线程可以做到这一点是 UiThread

所以如果你想在另一个线程中修改你的UI,尝试使用方法:Activity.runOnUiThread(new Runnable);

你的代码应该是这样的:

 new Thread() {
public void run() {
YourActivity.this.runOnUiThread(new Runnable(){

@Override
public void run(){
try {
startPayment("Bank");//Edit,integrate this on the runOnUiThread
} catch (Exception e) {
alertDialog.setMessage(e.getMessage());
handler.sendEmptyMessage(1);
progressDialog.cancel();
}
});
}
}
}.start();

关于java - 无法在未调用 Looper.prepare() 的线程内创建处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6213538/

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