Method Gettext() Must Be Called From The Ui Thread (android Studio)
Solution 1:
Try to pass Your values to Login AsyncTask
via execute(param1, param1, ..., paramN)
method:
submit.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View arg0) {
String username = uname.getText().toString();
String pass = password.getText().toString();
newLogin().execute(username, pass);
}
});
}
privatevoidfindViewsById() {
uname = (EditText) findViewById(R.id.txtUser);
password = (EditText) findViewById(R.id.txtPass);
submit = (Button) findViewById(R.id.login);
}
privateclassLoginextendsAsyncTask<String, String, String>{
@OverrideprotectedStringdoInBackground(String... args) {
// Getting username and password from user inputString username = args[0];
String pass = args[1];
Solution 2:
make username
and pass
login
class variable and override onPreExcecute()
and do this:
@OverrideprotectedvoidonPreExecute() {
username = uname.getText().toString();
pass = password.getText().toString();
}
Solution 3:
You're accessing the UI thread from a background thread here:
Stringusername= uname.getText().toString();
Stringpass= password.getText().toString();
What you want to do is just pass the username/password strings in to your background task constructor or you could pass them directly to the execute method. I prefer to define them in to the constructor if they are going to be required (like yours are).
Define your LoginTask like
String uname;
String password;
publicLogin(String username, Stringpassword({
this.uname = username;
this.password = password;
}
Then in doInBackground() you use the members instead.
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("u",this.username));
params.add(new BasicNameValuePair("p",this.pass));
json = jParser.makeHttpRequest(url_login, "GET", params);
Edit - so then your new Login().execute() call would look more like this
new Login(uname.getText().toString(), password.getText().toString()).execute();
Solution 4:
Unless something has changed that I'm not aware of, that shouldn't be a problem. UI elements can't be updated from the background but accessing their getters has never been an issue.
Anyway, you can get around this by adding a constructor to your AsyncTask
which would take the two String
s then send them when creating your task.
privateclassLoginextendsAsyncTask<String, String, String>{
// member variables of the task classString uName, pwd
publicLogin(String userName, String password) {
uName = userName;
pwd = password;
}
@OverrideprotectedStringdoInBackground(String... args) {...}
and pass them in your onClick()
@Override
public void onClick(View arg0) {
// execute method invokes doInBackground() where we open a Http URL connection using the given Servlet URL//and get output response from InputStream and return it.// pass them here
new Login(uname.getText().toString(), password.getText().toString()).execute();
}
Solution 5:
The exception is thrown because doInBackground()
is invoked from a background thread. I would add two String parameters to the constructor of your Login
class.
I would like to advise you to gain more knowledge about AsyncTasks
and Threading on Android. For example, there is a page in the official docs: http://developer.android.com/guide/components/processes-and-threads.html.
You might also take a look at this course: https://www.udacity.com/course/developing-android-apps--ud853. You can learn a lot of basics of the Android framework.
Post a Comment for "Method Gettext() Must Be Called From The Ui Thread (android Studio)"