2012-03-16 37 views
1

您好,我使用AsyncTask進行webservice調用。它的工作很好,當我輸入正確的用戶名和密碼,當我輸入錯誤的,那麼它是如何味精錯誤的用戶名和密碼,但在我的doInBackground區號其擲expction或應用粗魯 私有類LoginTask擴展的AsyncTask {如何在android中使用doInBackground顯示錯誤信息

private final ProgressDialog dialog = new ProgressDialog(login.this); 

    protected void onPreExecute() { 

      this.dialog.setMessage("Logging in..."); 
      this.dialog.show(); 
    } 


    protected Void doInBackground(final Void... unused) { 
     String auth=calllogin(Username,Passowrd); 
     cls_constant.userid=auth; 
     if(auth.equals("0")) 
     { 

     TextView errorTextview=(TextView)findViewById(R.id.tv_error); 
     errorTextview.setText("Username & Password incorrect."); 
     } 
     else { 

     Intent intnt=new Intent(getBaseContext(), aftersignin.class); 
     startActivity(intnt); 
     } 
     return null; // don't interact with the ui! 
    } 


    protected void onPostExecute(Void result) 
    { 

      if (this.dialog.isShowing()) 
      { 
      this.dialog.dismiss(); 
      } 


    } 

我想OT顯示錯誤MGS這樣的,但我不知道什麼即時做錯了 或如何使用onPostExecute作秀味精 感謝

回答

3

你不能在不同的線程更新的TextView(因爲ASYN在一個新的線程做的工作),因爲它屬於UI線程。所以,解決您的doInBackground如下:

protected Void doInBackground(final Void... unused) { 
    String auth=calllogin(Username,Passowrd); 
    cls_constant.userid=auth; 
    if(auth.equals("0")) 
    { 
     YourActivityClass.this.runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      TextView errorTextview=(TextView)findViewById(R.id.tv_error); 
       errorTextview.setText("Username & Password incorrect."); 
     } 
     }); 
    } 
    else { 

    Intent intnt=new Intent(getBaseContext(), aftersignin.class); 
    startActivity(intnt); 
    } 
    return null; // don't interact with the ui! 
} 
+0

我哪裏改變這個我沒有得到我M在Android的 – Dharmeshsharma 2012-03-16 13:34:50

+0

我剛剛更新了我的答案新。將** YourActivityClass **替換爲您的Activity類的名稱 – waqaslam 2012-03-16 13:36:29

+0

爲什麼當你有'onPostExecute()'的時候,你會這麼做,讓你在UI線程中使用它? – wnafee 2012-03-16 14:06:51

0

你幾乎與你的AsyncTask由你到if(auth.equals("0"))的時間內完成,所以你可以說整個塊進入onPostExecute()

0

你不能在後臺線程中觸摸任何東西(如異步任務) 正確的方法是使用一個處理程序,當auth.equals(「0」)你必須調用該處理程序並更新ui有...

if(auth.equals("0")){ 
    Message msg = handler.obtainMessage(); 
    msg.what = "error"; 
    msg.obj = "Username & Password incorrect."; 
    handler.sendMessage(msg); 
} 

在UI線程:

final Handler handler = new Handler(){ 
    @Override 
    public void handleMessage(Message msg) { 
    if(msg.what==error){ 
     TextView errorTextview=(TextView)findViewById(R.id.tv_error); 
     errorTextview.setText(msg.what.toString()); 
    } 
    super.handleMessage(msg); 
    } 
}; 
0

另一種方式做到這一點,沒有runOnUiThread或處理器額外的代碼。在doInBackground()中捕獲錯誤/成功,然後在onPostExecute()中更新TextView或相應地啓動活動。

0

onPostExecute有幫助你。所以你不妨使用它。

問題是doInBackgoround沒有在UI線程上運行。但onPostExecute是。所以它在那裏幫助你。

您的AsyncTask更改爲這樣的事情

class MyTask extends AsyncTask<Void, Void, Boolean> { 

    onPreExecute(....) 

    protected Boolean doInBackground(final Void... unused) { 
    String auth=calllogin(Username,Passowrd); 
    cls_constant.userid=auth; 
    if(auth.equals("0")) 
     return false; 
    else 
     return true; 
    } 

    protected void onPostExecute(Boolean result) { 
    if (this.dialog.isShowing()) 
     this.dialog.dismiss(); 

    if (!result) { 
     TextView errorTextview=(TextView)MyActivity.this.findViewById(R.id.tv_error); 
     errorTextview.setText("Username & Password incorrect."); 
    } else { 
     Intent intnt=new Intent(getBaseContext(), aftersignin.class); 
     MyActivity.this.startActivity(intnt); 
    } 
}