我正在构建一个应用程序,它通过与本地主机中的php文件通信来发送登录数据并进行身份验证(请参阅本教程)。我试过了,就像教程中说的那样(在主线程中),但是它给出了NetworkOnMainThreadException。但是,当我在AsyncTask中尝试执行以下操作时,即使在使用runOnUIThread时,也会出现calledFromWromThreadException。
@Override
protected Void doInBackground(Void... arg0) {
String username = etUsername.getText().toString();
String password = etPass.getText().toString();
UserFunctions userFunctions = new UserFunctions();
final JSONObject json = userFunctions.loginUser(username, password);
try {
if (json.getString(KEY_SUCCESS) != null) {
String res = json.getString(KEY_SUCCESS);
if (Integer.parseInt(res) == 1) {
// user successfully logged in
runOnUiThread(new Runnable() {
@Override
public void run() {
errorTxt.setText("");
//if (pDialog.isShowing())
//pDialog.dismiss();
Intent intent = new Intent(
getApplicationContext(),
AgentHome.class);
startActivity(intent);
}
});
}
} else if(json.getString(KEY_ERROR) != null) {
runOnUiThread(new Runnable() {
@Override
public void run() {
//if (pDialog.isShowing())
//pDialog.dismiss();
errorTxt.setText("Invalid username or password");
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
编辑:我把errorTxt.setText(“”)放在runOnUIThread中,但它仍然会给出以下错误
02-11 10:27:20.397: E/AndroidRuntime(4957): FATAL EXCEPTION: AsyncTask #1
02-11 10:27:20.397: E/AndroidRuntime(4957): Process: collector.lbfinance, PID: 4957
02-11 10:27:20.397: E/AndroidRuntime(4957): java.lang.RuntimeException: An error occured while executing doInBackground()
02-11 10:27:20.397: E/AndroidRuntime(4957): at android.os.AsyncTask$3.done(AsyncTask.java:300)
02-11 10:27:20.397: E/AndroidRuntime(4957): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
02-11 10:27:20.397: E/AndroidRuntime(4957): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
02-11 10:27:20.397: E/AndroidRuntime(4957): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
02-11 10:27:20.397: E/AndroidRuntime(4957): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
02-11 10:27:20.397: E/AndroidRuntime(4957): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
02-11 10:27:20.397: E/AndroidRuntime(4957): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
02-11 10:27:20.397: E/AndroidRuntime(4957): at java.lang.Thread.run(Thread.java:841)
02-11 10:27:20.397: E/AndroidRuntime(4957): Caused by: java.lang.NullPointerException
02-11 10:27:20.397: E/AndroidRuntime(4957): at collector.lbfinance.MainActivity$GetPassword.doInBackground(MainActivity.java:108)
02-11 10:27:20.397: E/AndroidRuntime(4957): at collector.lbfinance.MainActivity$GetPassword.doInBackground(MainActivity.java:1)
02-11 10:27:20.397: E/AndroidRuntime(4957): at android.os.AsyncTask$2.call(AsyncTask.java:288)
02-11 10:27:20.397: E/AndroidRuntime(4957): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
02-11 10:27:20.397: E/AndroidRuntime(4957): ... 4 more
最佳答案
errorTxt.setText("");
不在ui线程上。不能从后台线程更新ui。
此外,您还可以从edittext on button click获取文本,并将其作为参数直接传递到doInBackground
。
Asynctask有onPreExecute
和onPostExecute
在ui线程上调用。您应该考虑在这些方法中更新ui。在doInbackground
中进行所有背景计算。我不认为需要runOnUiThread
。
关于php - Android:即使在使用runOnUIThread时,也会调用CalledFromWrongThreadException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21693372/