-2

我的问题是在执行此任务时显示警告消息,请为后台进程发布正确的代码。

public class loginTask extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(LoginActivity.this);
        pDialog.setMessage("Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    @Override
    protected String doInBackground(String... urls) {


        ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
        postParameters.add(new BasicNameValuePair("username", editTextUsername.getText().toString()));
        postParameters.add(new BasicNameValuePair("password", editTextPassword.getText().toString()));
        String response = null;String res=null;
        try {
            System.out.println("Called for login server");
            response = CustomHttpClient.executeHttpPost("login.php", postParameters);
            System.out.println("connect");
            res=response.toString();
            res= res.replaceAll("\\s+","");


            }

        } catch (Exception e) {
            e.printStackTrace();

        }
        return res;


    }
    protected void onPostExecute(String result) {
        if(res.equals("success"))
            {
                Intent mainintent=new Intent(LoginActivity.this, MainActivity.class);
                startActivity(mainintent);
                finish();
                //error.setText("Correct Username or Password");
            }
            else
            {
                /*Toast.makeText(getApplicationContext(), "Sorry!! Incorrect Username or Password", Toast.LENGTH_LONG).show();
                editTextUsername.setText("");
                editTextPassword.setText("");
                editTextUsername.requestFocus();*/
                error.setText("Sorry!! Incorrect Username or Password");
        pDialog.dismiss();
    }
}

日志猫

09-24 09:07:54.894: W/System.err(4212): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
09-24 09:07:54.946: W/System.err(4212):     at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:5908)
09-24 09:07:54.954: W/System.err(4212):     at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:837)
09-24 09:07:54.954: W/System.err(4212):     at android.view.View.requestLayout(View.java:15792)
09-24 09:07:54.964: W/System.err(4212):     at android.view.View.requestLayout(View.java:15792)
09-24 09:07:54.964: W/System.err(4212):     at android.view.View.requestLayout(View.java:15792)
09-24 09:07:54.984: W/System.err(4212):     at android.view.View.requestLayout(View.java:15792)
09-24 09:07:54.984: W/System.err(4212):     at android.widget.ScrollView.requestLayout(ScrollView.java:1440)
09-24 09:07:54.993: W/System.err(4212):     at android.view.View.requestLayout(View.java:15792)
09-24 09:07:54.993: W/System.err(4212):     at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:358)
09-24 09:07:55.014: W/System.err(4212):     at android.view.View.requestLayout(View.java:15792)
09-24 09:07:55.014: W/System.err(4212):     at android.widget.TextView.checkForRelayout(TextView.java:6524)
09-24 09:07:55.034: W/System.err(4212):     at android.widget.TextView.setText(TextView.java:3771)
09-24 09:07:55.034: W/System.err(4212):     at android.widget.TextView.setText(TextView.java:3629)
09-24 09:07:55.054: W/System.err(4212):     at android.widget.TextView.setText(TextView.java:3604)
09-24 09:07:55.054: W/System.err(4212):     at com.loginpack.LoginActivity$loginTask.doInBackground(LoginActivity.java:111)
09-24 09:07:55.064: W/System.err(4212):     at com.loginpack.LoginActivity$loginTask.doInBackground(LoginActivity.java:1)
09-24 09:07:55.084: W/System.err(4212):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
09-24 09:07:55.084: W/System.err(4212):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
09-24 09:07:55.104: W/System.err(4212):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
09-24 09:07:55.114: W/System.err(4212):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
09-24 09:07:55.134: W/System.err(4212):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
09-24 09:07:55.154: W/System.err(4212):     at java.lang.Thread.run(Thread.java:841)

执行后,它移动到另一个活动,但用户名和密码错误,它没有在我的屏幕上点击后显示 UI,它只显示错误登录消息

4

5 回答 5

0

您需要将任何更新 UI 的代码(例如error.setText)移动到onPostExecute.

于 2013-09-24T13:12:25.653 回答
0

doInBackground根据 if/else 条件将布尔变量设置为true/false,并签入onPostExecute以启动新 Intent 或显示错误消息

boolean isSuccess=false;

doInBackground(){
   if(res.equals("success"))
            {
              //do your work
              isSuccess=true;
            }
            else
            {
              //do your work
              isSuccess=false;

            }

}

在 onPostExecute

protected void onPostExecute(String result) {

 if(isSuccess)
    {
        Intent mainintent=new Intent(LoginActivity.this, MainActivity.class);
        startActivity(mainintent);
        finish();

    }
    else
    {
        /*Toast.makeText(getApplicationContext(), "Sorry!! Incorrect Username or Password", Toast.LENGTH_LONG).show();
        editTextUsername.setText("");
        editTextPassword.setText("");
        editTextUsername.requestFocus();*/
        error.setText("Sorry!! Incorrect Username or Password");

    }


pDialog.dismiss();
}
于 2013-09-24T13:28:18.833 回答
0
       Intent mainintent=new Intent(LoginActivity.this, MainActivity.class);
            startActivity(mainintent);
            finish();

此代码应在 onPostExecute() 方法中。

于 2013-09-24T13:12:53.290 回答
0

你必须改变这个..

protected void onPostExecute(String result) {

     if(result.equals("success"))
        {
            Intent mainintent=new Intent(LoginActivity.this, MainActivity.class);
            startActivity(mainintent);
            finish();
            //error.setText("Correct Username or Password");
        }
        else
        {
            /*Toast.makeText(getApplicationContext(), "Sorry!! Incorrect Username or Password", Toast.LENGTH_LONG).show();
            editTextUsername.setText("");
            editTextPassword.setText("");
            editTextUsername.requestFocus();*/
            error.setText("Sorry!! Incorrect Username or Password");

        }


    pDialog.dismiss();
}

这是因为你不能更新你的 UI doInBackground()方法.. 你应该使用onPostExecute()方法..

于 2013-09-24T13:18:24.333 回答
0

doInBackground()是一个后台非 UI 线程,因此它无法访问应用程序中定义的布局。因此,任何与 UI 相关的代码都不能在其中编写。

另一方面,onPostExecute()UI 线程通常用于在 AsyncTask 完成后对 UI 进行更改。

你需要做什么 :

在 AsyncTask 类中定义一个标志,并在内部使用它doInBackground()来设置它是否成功。然后,在内部onPostEXecute(),检查标志的值并显示相应的消息。

添加boolean successFlag你的类定义。

设置标志的值doInBackground()

doInBackground()
{
...

            if(res.equals("success"))
            {
             successFlag = true;
            }
            else
            {
             successFlag = false;
            }

...

}

检查此标志的值onPostExecute()

onPostExecute()
{
     if(successFlag)
{
                Intent mainintent=new Intent(LoginActivity.this, MainActivity.class);
                startActivity(mainintent);
                finish();
}
      else
{
                error.setText("Sorry!! Incorrect Username or Password");
}
}
于 2013-09-24T13:16:19.823 回答