必须从UI线程调用方法getText(错误)

时间:2016-12-27 08:54:57

标签: android android-asynctask gettext

我正在Android Studio中注册,我在

上发现了一个问题
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 ();,

可能的解决方案?

这是我的Activity代码:

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 个答案:

答案 0 :(得分:1)

原因在于AsyncTask行为。

从UI Thread调用方法onPreExecute()onPostExecute

doInBackground()在单独的线程中被调用。

解决方案是将文本参数传递给AsyncTask#execute

请参阅:AsyncTask tutorial

答案 1 :(得分:1)

您有一个后台线程但是当您访问UI组件时,您必须通过man UI线程访问它们。你可以通过

来做到这一点
youActivity.runOnUiThread(new Runnable() {
    public void run() {
        t1.setText(rb1.getText());
    }
});