Android将EditText转换为String

时间:2015-11-24 15:45:40

标签: java android android-studio

我知道如何将EditText转换为字符串,不知道为什么它不起作用。

String username = user.getText().toString();

user = (EditText)findViewById(R.id.username);

我收到此错误:“必须从UI线程调用方法getText,当前推断的线程是worker”

完整代码:

public class Login extends Activity implements OnClickListener{
private EditText user;
private Button bLogin;
// Progress Dialog
private ProgressDialog pDialog;
// JSON parser class
JSONParser jsonParser = new JSONParser();
private static final String LOGIN_URL = "http://testapp.comlu.com/login.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    user = (EditText)findViewById(R.id.username);
    String aID = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
    bLogin = (Button)findViewById(R.id.login);
    bLogin.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
        case R.id.login:
            new AttemptLogin().execute();
            // here we have used, switch case, because on login activity you may //also want to show registration button, so if the user is new ! we can go the //registration activity , other than this we could also do this without switch //case.
        default:
            break;
    }
}

class AttemptLogin extends AsyncTask<String, String, String> {
    /**
     * Before starting background thread Show Progress Dialog
     * */
    boolean failure = false;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(Login.this);
        pDialog.setMessage("Attempting for login...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    @Override
    protected String doInBackground(String... args) {
        // TODO Auto-generated method stub
        // here Check for success tag
        int success;
        String username = user.getText().toString();
        String androidID = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
        try {

            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("username", username));
            params.add(new BasicNameValuePair("androidID", androidID));

            Log.d("request!", "starting");

            JSONObject json = jsonParser.makeHttpRequest(
                    LOGIN_URL, "POST", params);

            // checking  log for json response
          Log.d("Login attempt", json.toString());

            // success tag for json
            success = 1;
            if (success == 1) {
                Log.d("Successfully Login!", json.toString());

                Intent ii = new Intent(Login.this,Menu.class);
                finish();
                // this finish() method is used to tell android os that we are done with current //activity now! Moving to other activity
                startActivity(ii);
                return json.getString(TAG_MESSAGE);
            }else{

                return json.getString(TAG_MESSAGE);

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }
    /**
     * Once the background process is done we need to  Dismiss the progress dialog asap
     * **/
    protected void onPostExecute(String message) {

        pDialog.dismiss();
        if (message != null){
            Toast.makeText(Login.this, message, Toast.LENGTH_LONG).show();
        }
    }
}

有什么想法吗? 谢谢, 耀西

2 个答案:

答案 0 :(得分:0)

您无法从后台线程操纵UI元素。您正尝试使用doInBackground方法访问UI元素:

String username = user.getText().toString();

而不是那样,您应该将数据传递给异步任务,如:

new AttemptLogin().execute(user.getText().toString());

您还从doInBackground方法开始活动。您应该将该段代码移动到onPostExecute方法。

答案 1 :(得分:0)

  

我收到此错误:“必须从UI调用方法getText   线程,当前推断的线程是worker“

你可以移动

String username = user.getText().toString();
在你的onClick方法中

,并将String传递给AsyncTask,如

new AttemptLogin().execute(username);

调用doInBackground后,您可以通过String... args访问它,例如。 args[0]