Method getText() must be called from the UI Thread (Android Studio)

前端 未结 5 864
花落未央
花落未央 2020-11-30 14:44

I\'m trying to create a login for an application. However I have a problem.

This is my code:

package com.forgetmenot.loginregister;


import java.uti         


        
相关标签:
5条回答
  • 2020-11-30 14:56

    Try to pass Your values to Login AsyncTask via execute(param1, param1, ..., paramN) method:

    submit.setOnClickListener(new View.OnClickListener() {
    
               @Override
               public void onClick(View arg0) {
    
                   String username = uname.getText().toString();
                   String pass = password.getText().toString();
                   new Login().execute(username, pass);
    
               }
           });
        }
        private void findViewsById() {
    
            uname = (EditText) findViewById(R.id.txtUser);
            password = (EditText) findViewById(R.id.txtPass);
            submit = (Button) findViewById(R.id.login);
        }
        private class Login extends AsyncTask<String, String, String>{
    
            @Override
            protected String doInBackground(String... args) {
                // Getting username and password from user input
    
                String username = args[0];
                String pass = args[1];
    
    0 讨论(0)
  • 2020-11-30 15:06

    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.

    0 讨论(0)
  • 2020-11-30 15:14

    make username and pass login class variable and override onPreExcecute() and do this:

        @Override
        protected void onPreExecute() {
    
            username = uname.getText().toString();
            pass = password.getText().toString();
    
        }
    
    0 讨论(0)
  • 2020-11-30 15:18

    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 Strings then send them when creating your task.

    private class Login extends AsyncTask<String, String, String>{
    
        // member variables of the task class
        String uName, pwd
        public Login(String userName, String password) {
            uName = userName;
            pwd = password;
        }
    
        @Override
        protected String doInBackground(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();
     }
    
    0 讨论(0)
  • 2020-11-30 15:20

    You're accessing the UI thread from a background thread here:

    String username = uname.getText().toString();
    String pass = 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;
    public Login(String username, String password({
        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();
    
    0 讨论(0)
提交回复
热议问题