如何在android中使用doInBackground显示错误消息

时间:2012-03-16 13:29:47

标签: android android-asynctask

您好我正在使用AsyncTask进行webservice调用。当我输入正确的用户名和密码时它工作正常,当我输入错误然后它的msg错误的用户名和密码,但在我的doInBackground区域的代码中它的抛出或应用程序粗糙 私有类LoginTask扩展了AsyncTask {

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


     } 

我想要显示错误mgs像这样,但我不知道我做错了什么 或如何使用onPostExecute for show msg 感谢

5 个答案:

答案 0 :(得分: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! 
}

答案 1 :(得分:0)

到达if(auth.equals("0"))时,您已经完成了AsyncTask,因此您可以将整个块移动到onPostExecute()

答案 2 :(得分:0)

你无法从后台线程中触摸ui中的任何内容(如异步任务) 正确的方法是使用一个处理程序,当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);
  }
};

答案 3 :(得分:0)

另一种方法,没有runOnUiThread或Handler额外的代码。在doInBackground()中捕获错误/成功,然后在onPostExecute()中更新TextView或相应地启动活动。

答案 4 :(得分: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);
  } 
}