【问题标题】:how to use doInBackground in android for show error msg如何在android中使用doInBackground显示错误信息
【发布时间】:2012-03-16 13:29:46
【问题描述】:

您好,我正在使用 AsyncTask 进行 Web 服务调用。当我输入正确的用户名和密码时,它工作正常,当我输入错误时,它的用户名和密码如何错误,但在我的 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(); 
            }


     } 

我想显示这样的错误信息,但我不知道我做错了什么 或如何使用 onPostExecute 显示味精 谢谢

【问题讨论】:

    标签: android android-asynctask


    【解决方案1】:

    您不能在不同的线程上更新 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! 
    }
    

    【讨论】:

    • 我在哪里改变了这个我不知道我是 android 的新手
    • 我刚刚更新了我的答案。将 YourActivityClass 替换为您的 Activity 类的名称
    • 当你有 onPostExecute() 供你使用时,你为什么要这样做?
    • 因为他想知道问题所在,而我已经指出了问题所在并进行了修复。虽然他正在关闭 postExecute 上的对话,但如何编码取决于他:)
    • 您不认为他还从这个答案中学到了一些新东西,即只能在 UI 线程上更改视图吗?所以放心吧兄弟,我们是来帮忙的,不是为了积分:)
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      您不能从后台线程(如异步任务)触摸 ui 中的任何内容 正确的做法是使用处理程序,当 auth.equals("0") 必须调用该处理程序并从那里更新用户界面...

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

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          【解决方案5】:

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

          【讨论】:

            猜你喜欢
            • 2020-02-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-03-05
            • 1970-01-01
            相关资源
            最近更新 更多