我正在开发的应用程序有一个连接到 mysql 数据库的登录/注册,起初我在 UI 线程
上运行所有内容,后来我发现它无法工作,因为在 Android 的 UI 线程上运行长代码。我尝试编辑代码以在我添加的新Thread
上运行长任务。
。现在我的应用程序注册了,我在 mysql 中看到了结果,但我的应用程序由于此错误而不断关闭
android.view.ViewRootImpl$CalledFromWrongThreadException:
Only the original thread that created a view hierarchy can touch its views.
该错误是可以理解的,但我不知道如何将 View
或 View
运行回 UI 线程
。
我已经对 runOnuithread
进行了一些研究,但我不知道将其放置在代码中的何处,也不知道我是否将之前添加的新 Thread
放置在请从错误的地方开始
谁能帮我解决这个问题
这是代码 fragment
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
// Importing all assets like buttons, text fields
inputFullName = (EditText) findViewById(R.id.registerName);
inputEmail = (EditText) findViewById(R.id.registerEmail);
inputPassword = (EditText) findViewById(R.id.registerPassword);
btnRegister = (Button) findViewById(R.id.btnRegister);
btnLinkToLogin = (Button) findViewById(R.id.btnLinkToLoginScreen);
registerErrorMsg = (TextView) findViewById(R.id.register_error);
// Register Button Click event
btnRegister.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
/** According with the new StrictGuard policy, running long tasks on the Main UI thread is not possible
So creating new thread to create and execute http operations */
new Thread (new Runnable() {
@Override
public void run() {
//
String name = inputFullName.getText().toString();
String email = inputEmail.getText().toString();
String password = inputPassword.getText().toString();
UserFunctions userFunction = new UserFunctions();
JSONObject json = userFunction.registerUser(name, email, password);
// check for login response
try {
//
//
if (json.getString(KEY_SUCCESS) != null) {
registerErrorMsg.setText("");
String res = json.getString(KEY_SUCCESS);
if(Integer.parseInt(res) == 1){
// user successfully registred
// Store user details in SQLite Database
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
JSONObject json_user = json.getJSONObject("user");
// Clear all previous data in database
userFunction.logoutUser(getApplicationContext());
db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));
// Launch Dashboard Screen
Intent dashboard = new Intent(getApplicationContext(), MainActivity.class);
// Close all views before launching Dashboard
dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(dashboard);
// Close Registration Screen
finish();
}else{
// Error in registration
registerErrorMsg.setText("Error occured in registration");
}
}//
} catch (JSONException e) {
e.printStackTrace();
}
}
}).start();
}
});
// Link to Login Screen
btnLinkToLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),
LoginActivity.class);
startActivity(i);
// Close Registration View
finish();
}
});
}
}
最佳答案
由于您似乎将网络代码与 UI
代码混合在一起,因此我不会尝试为您编写它。我可以告诉你它应该是怎样的,并假设你可以自己重新排列代码,因为你知道它的作用。
好的,在您的 run()
中,您输入网络代码,然后当您需要更新 UI
时,您就可以拥有
runOnUiThread(new Runnable()
{
@Override
public void run()
{
// code to update UI
}
});
话虽如此,我建议使用 AsyncTask为了您的网络运营。这使得它变得更加容易,因为它已经具有后台内容和更新 UI 的功能。您启动任务并运行 doInBackground()
,这就是您执行所有网络操作的地方。然后,您可以在 AsyncTasks
其他 3 种方法中的任何一个中更新 UI
。
See this answer有关使用 AsyncTask
的示例,以及上面文档的链接。
关于java - 如何使用runOnUithread运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21212631/