不能在没有调用Looper.prepare()的线程中创build处理程序

我得到这个错误“不能创build处理程序线程内没有调用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(); } }); 

StartPayment方法:

  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); } 

Solutions Collecting From Web of "不能在没有调用Looper.prepare()的线程中创build处理程序"

你应该知道,当你尝试修改你的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(); 

我假设你在startPayment()方法中创build一个Handler。 你不能这样做,因为只能在UI线程上创build处理程序。 只要在你的活动课上创build它。

而不是new Thread()线,尝试给

 this.runOnUiThread(new Runnable() { 

你不能改变线程中的任何用户界面,你可以使用runOnUIThreadAsyncTask获取更多关于此点击的细节

我发现大多数线程处理可以被AsyncTasksreplace,如下所示:

 public class TestStuff extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button buttonBank = (Button) findViewById(R.id.button); buttonBank.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { new StartPaymentAsyncTask(TestStuff.this).execute((Void []) null); } }); } private class StartPaymentAsyncTask extends AsyncTask<Void, Void, String> { private ProgressDialog dialog; private final Context context; public StartPaymentAsyncTask(Context context) { this.context = context; } @Override protected void onPreExecute() { dialog = new ProgressDialog(context); // setup your dialog here dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); dialog.setMessage(context.getString(R.string.doing_db_work)); dialog.setCancelable(false); dialog.show(); } @Override protected String doInBackground(Void... ignored) { String returnMessage = null; try { startPayment("Bank"); } catch (Exception e) { returnMessage = e.getMessage(); } return returnMessage; } @Override protected void onPostExecute(String message) { dialog.dismiss(); if (message != null) { // process the error (show alert etc) Log.e("StartPaymentAsyncTask", String.format("I received an error: %s", message)); } else { Log.i("StartPaymentAsyncTask", "No problems"); } } } public void startPayment(String string) throws Exception { SystemClock.sleep(2000); // pause for 2 seconds for dialog Log.i("PaymentStuff", "I am pretending to do some work"); throw new Exception("Oh dear, database error"); } } 

我将应用程序上下文传递给asynchronous,以便它可以创build对话框。

这样做的好处是你确切地知道哪些方法在你的UI中运行,哪些在独立的后台线程中。 你的主UI线程不会被延迟,而分离成小的asynchronous任务是相当不错的。

该代码假设你的startPayment()方法对UI没有任何作用,如果是的话,将它移动到AsyncTask的onPostExecute中,这样它就在UI线程中完成了。

尝试

 final Handler handlerTimer = new Handler(Looper.getMainLooper()); handlerTimer.postDelayed(new Runnable() { public void run() { ...... } }, time_interval});