Method getText must be called form the UI thread (Error)

448 Views Asked by At

I'm making the register in Android Studio, and I found a problem on

String name = names. getText (). toString (); 

String address = address. getText (). toString (); 

String telephone = telephone. getText (). toString (); 

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

String password = pass. getText (). toString ();,

Possible solutions?

Here is my Activity code:

public class ActivityRegister extends Activity implements View.OnClickListener {

    private EditText user, pass, nama, alamat, telpon;
    private TextView t1;
    private RadioButton rb1, rb2;
    private Button mRegister;
    private ProgressDialog pDialog;

    JSONParser jsonParser = new JSONParser();

    private static final String LOGIN_URL = "http://10.0.2.2/coba/api/register.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.register);

        ActionBar bar = getActionBar();
        bar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.header)));
        bar.setTitle("Register Distro App");

        nama = (EditText) findViewById(R.id.nama);
        alamat = (EditText) findViewById(R.id.alamat);
        telpon = (EditText) findViewById(R.id.telpon);
        user = (EditText) findViewById(R.id.username);
        pass = (EditText) findViewById(R.id.password);
        rb1=(RadioButton)findViewById(R.id.option1);
        rb2=(RadioButton)findViewById(R.id.option2);
        t1=(TextView)findViewById(R.id.TextView01);

        mRegister = (Button)findViewById(R.id.register);
        mRegister.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        new CreateUser().execute();
    }

    class CreateUser extends AsyncTask<String, String, String> {
        boolean failure = false;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(ActivityRegister.this);
            pDialog.setMessage("Mendaftar Member...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... args) {
            int success;
            String namanya = nama.getText().toString();
            String alamatnya = alamat.getText().toString();
            String telponnya = telpon.getText().toString();
            String username = user.getText().toString();
            String password = pass.getText().toString();

            if(rb1.isChecked() == true)
                t1.setText(rb1.getText());
            if(rb2.isChecked() == true)
                t1.setText(rb2.getText());
            if(rb1.isChecked() == false && rb2.isChecked() == false)
                t1.setText("");
            String jenkel = t1.getText().toString();

            try {
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("username", username));
                params.add(new BasicNameValuePair("password", password));
                params.add(new BasicNameValuePair("nama", namanya));
                params.add(new BasicNameValuePair("alamat", alamatnya));
                params.add(new BasicNameValuePair("telpon", telponnya));
                params.add(new BasicNameValuePair("jenkel", jenkel));
                Log.d("request!", "starting");

                JSONObject json = jsonParser.makeHttpRequest(
                        LOGIN_URL, "POST", params);
                Log.d("Register attempt", json.toString());

                success = json.getInt(TAG_SUCCESS);
                if (success == 1) {
                    Log.d("User Created!", json.toString());
                    finish();
                    return json.getString(TAG_MESSAGE);
                }else{
                    Log.d("Register Failure!", json.getString(TAG_MESSAGE));
                    return json.getString(TAG_MESSAGE);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

        protected void onPostExecute(String file_url) {
            pDialog.dismiss();
            if (file_url != null){
                Toast.makeText(ActivityRegister.this, file_url, Toast.LENGTH_LONG).show();
            }
        }
    }
}
2

There are 2 best solutions below

0
Andrii Abramov On

The reason is in AsyncTask behavior.

Methods onPreExecute(), onPostExecute are called from UI Thread.

But doInBackground() is called in separate thread.

The solution is to pass text parameters to AsyncTask#execute.

See: AsyncTask tutorial

0
ddarellis On

You have a background thread but when you access UI components you have to access them through the man UI thread. You can do this by

youActivity.runOnUiThread(new Runnable() {
    public void run() {
        t1.setText(rb1.getText());
    }
});