2011-06-02 46 views
16

我得到這個錯誤「內螺紋無法創建處理程序尚未調用Looper.prepare()」不能內螺紋已不叫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); 
} 
+0

發佈startPayment()方法的代碼,請。 – 2011-06-02 10:57:15

回答

28

你應該知道,當你試圖修改你的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(); 
+0

仍然出現這個錯誤 – Sergio 2011-06-02 11:08:34

+0

你可以把你的'startPayment()'代碼# – Houcine 2011-06-02 11:09:39

+0

@Sergio:看我的編輯 – Houcine 2011-06-02 13:13:23

3

我假設你創建startPayment處理程序()方法。你不能那樣做,因爲處理程序只能在第一個UI線程上創建。只需在您的活動課程中創建它。

3

代替new Thread()線,嘗試給

this.runOnUiThread(new Runnable() { 
1

你不能在線程可以更改任何UI使用runOnUIThreadAsyncTask關於更詳細的這click here

1

我發現,大多數線程處理可以通過AsyncTasks被替換:

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

我將應用程序上下文傳遞給異步,以便它可以從中創建對話框。

這樣做的好處是您確切知道在您的UI中運行哪些方法,哪些方法位於單獨的後臺線程中。你的主UI線程不會被延遲,分離成小的異步任務是相當不錯的。

該代碼假設您的startPayment()方法對UI沒有任何作用,如果它確實如此,則將其移入AsyncTask的onPostExecute,以便在UI線程中完成。

+0

我得到這個錯誤 「的新StartPaymentAsyncTask(本).execute((無效[])NULL)}」 錯誤 - 構造PaymentActivity.StartPaymentAsyncTask(新View.OnClickListener(){})是未定義 – Sergio 2011-06-02 11:26:35

+0

是, 'this'應該是'YourActivity.this'。我更新了代碼(我認爲它應該是PaymentActivity) – 2011-06-02 11:44:20

+0

斷點onClick方法,以及異步任務的構造函數以查看它們是否正在工作(或添加日誌記錄)。您是否爲進度對話框添加了所有設置?我已經在上面添加了示例代碼。 – 2011-06-02 12:10:52

1

嘗試

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

           } 
               }, time_interval}); 
相關問題