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

前端 未结 5 888
花落未央
花落未央 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 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{
    
        // 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();
     }
    

提交回复
热议问题