我正在开发的应用程序有一个连接到 mysql 数据库的登录/注册,起初我在UI Thread
我后来发现它不起作用,因为反对在 Android 上运行长代码UI Thread
。我尝试编辑代码以在新的计算机上运行长任务Thread
我补充说。
。现在我的应用程序注册了,我在 mysql 中看到结果,但由于此错误,我的应用程序不断关闭
android.view.ViewRootImpl$CalledFromWrongThreadException:
Only the original thread that created a view hierarchy can touch its views.
该错误是可以理解的,但我不知道如何运行View
or View
回到UI Thread
.
我做了一些关于runOnuithread
但我不知道将它放在我的代码中的什么位置,也不知道我是否放置了新的Thread
我之前在错误的地方添加了请
谁能帮我解决这个问题
这是代码片段
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
}
});
话虽如此,我建议使用异步任务 http://developer.android.com/reference/android/os/AsyncTask.html用于您的网络运营。这使得它变得更加容易,因为它已经具有后台内容和更新的功能UI
。您开始任务并doInBackground()
运行,这就是您执行所有网络操作的地方。然后您可以更新UI
在任何一个AsyncTasks
其他3种方法。
看到这个答案 https://stackoverflow.com/questions/18898039/using-asynctask/18898105#18898105举个使用的例子AsyncTask
,以及上面文档的链接。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)