我的问题是在执行此任务时显示警告消息,请为后台进程发布正确的代码。
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,它只显示错误登录消息