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

Android 必须从UI线程调用方法getText(错误),android,android-asynctask,gettext,Android,Android Asynctask,Gettext,我在安卓工作室注册,我发现了一个问题 String name = names. getText (). toString (); String address = address. getText (). toString (); String telephone = telephone. getText (). toString (); String username = user. getText (). toString (); String password = pass

我在安卓工作室注册,我发现了一个问题

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 ();,
可能的解决方案

这是我的
活动
代码:

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();
            }
        }
    }
}
公共类ActivityRegister扩展活动实现View.OnClickListener{
私有EditText用户、pass、nama、alamat、telpon;
私有文本视图t1;
专用单选按钮rb1、rb2;
私人按钮注册器;
私人对话;
JSONParser JSONParser=新的JSONParser();
私有静态最终字符串登录\u URL=”http://10.0.2.2/coba/api/register.php";
私有静态最终字符串标记_SUCCESS=“SUCCESS”;
私有静态最终字符串标记_MESSAGE=“MESSAGE”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
ActionBar=getActionBar();
setBackgroundDrawable(新的ColorDrawable(getResources().getColor(R.color.header)));
bar.setTitle(“注册发行版应用程序”);
nama=(编辑文本)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=(单选按钮)findViewById(R.id.option1);
rb2=(单选按钮)findViewById(R.id.option2);
t1=(TextView)findViewById(R.id.TextView01);
mRegister=(按钮)findviewbyd(R.id.register);
mRegister.setOnClickListener(此);
}
@凌驾
公共void onClick(视图v){
新建CreateUser().execute();
}
类CreateUser扩展异步任务{
布尔失败=假;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=newprogressdialog(ActivityRegister.this);
pDialog.setMessage(“Mendaftar成员…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(真);
pDialog.show();
}
@凌驾
受保护的字符串doInBackground(字符串…args){
成功;
字符串namanya=nama.getText().toString();
字符串alamatnya=alamat.getText().toString();
字符串telponnya=telpon.getText().toString();
字符串username=user.getText().toString();
字符串密码=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(“”);
字符串jenkel=t1.getText().toString();
试一试{
List params=new ArrayList();
添加(新的BasicNameValuePair(“用户名”,用户名));
添加(新的BasicNameValuePair(“密码”,password));
参数添加(新的BasicNameValuePair(“nama”,namanya));
参数添加(新的BasicNameValuePair(“alamat”,alamatnya));
添加参数(新的BasicNameValuePair(“telpon”,telponnya));
参数添加(新的BasicNameValuePair(“jenkel”,jenkel));
Log.d(“请求!”,“启动”);
JSONObject json=jsonParser.makeHttpRequest(
登录URL,“POST”,参数);
Log.d(“注册尝试”,json.toString());
success=json.getInt(TAG_success);
如果(成功==1){
Log.d(“用户创建的!”,json.toString());
完成();
返回json.getString(TAG_消息);
}否则{
Log.d(“注册失败!”,json.getString(TAG_MESSAGE));
返回json.getString(TAG_消息);
}
}捕获(JSONException e){
e、 printStackTrace();
}
返回null;
}
受保护的void onPostExecute(字符串文件\u url){
pDialog.disclose();
如果(文件url!=null){
Toast.makeText(ActivityRegister.this,file_url,Toast.LENGTH_LONG.show();
}
}
}
}

原因在于异步任务行为

从UI线程调用方法
onPreExecute()
onPostExecute

但是
doInBackground()
在单独的线程中调用

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


请参阅:

原因在于异步任务行为

从UI线程调用方法
onPreExecute()
onPostExecute

但是
doInBackground()
在单独的线程中调用

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


请参阅:

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

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

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

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

在onpreExecute()中调用这些方法并使字符串变量成为全局变量。这样您就可以在异步任务中使用它了。异常(您在这里没有包括)非常具体。您可以使用搜索栏轻松找到答案。在onpreExecute()中调用这些方法,并将字符串变量设置为全局。这样您就可以在异步任务中使用它了……异常(此处未包括)非常具体。您可以使用搜索栏轻松找到答案。