gpt4 book ai didi

php - 安卓 : CalledFromWrongThreadException even when used runOnUIThread

转载 作者:行者123 更新时间:2023-11-29 03:02:09 26 4
gpt4 key购买 nike

我正在构建一个应用程序,它通过与本地主机中的 php 文件通信来发送登录数据并进行身份验证(引用本教程 http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/)。我按照教程所说(在主线程中)进行了尝试,但它给出了 NetworkOnMainThreadException。但是,当我在 AsyncTask 中尝试以下操作时,即使我正在使用 runOnUIThread,它也会给出 CalledFromWrongThreadException。

@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 线程上。您无法从后台线程更新用户界面。

您还可以在单​​击按钮时从 edittext 中获取文本,并将其作为参数直接传递给 doInBackground

Asynctask 有 onPreExecuteonPostExecute 是在 ui 线程上调用的。您应该考虑在这些方法中更新 ui。在 doInbackground 中完成所有后台计算。我看不到需要 runOnUiThread

关于php - 安卓 : CalledFromWrongThreadException even when used runOnUIThread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21693372/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com