Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android将编辑文本转换为字符串_Java_Android_Android Studio - Fatal编程技术网

Java Android将编辑文本转换为字符串

Java Android将编辑文本转换为字符串,java,android,android-studio,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 Edi

我知道如何将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();
        }
    }
}
公共类登录扩展活动实现OnClickListener{
私人编辑文本用户;
私人按钮博客;
//进度对话框
私人对话;
//JSON解析器类
JSONParser JSONParser=新的JSONParser();
私有静态最终字符串登录\u URL=”http://testapp.comlu.com/login.php";
私有静态最终字符串标记_SUCCESS=“SUCCESS”;
私有静态最终字符串标记_MESSAGE=“MESSAGE”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u登录);
user=(EditText)findViewById(R.id.username);
String-aID=Settings.Secure.getString(getContentResolver(),Settings.Secure.ANDROID);
bLogin=(按钮)findviewbyd(R.id.login);
setOnClickListener(this);
}
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
开关(v.getId()){
案例R.id.login:
新建AttemptLogin().execute();
//这里我们使用了switch case,因为在登录活动中,您可能//还希望显示注册按钮,因此如果用户是新用户!我们可以进行//注册活动,除此之外,我们也可以不使用switch//case来进行此操作。
违约:
打破
}
}
类AttemptLogin扩展了AsyncTask{
/**
*在启动后台线程显示进度对话框之前
* */
布尔失败=假;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=newprogressdialog(Login.this);
setMessage(“正在尝试登录…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(真);
pDialog.show();
}
@凌驾
受保护的字符串doInBackground(字符串…args){
//TODO自动生成的方法存根
//在这里检查成功标签
成功;
字符串username=user.getText().toString();
String ANDROID=Settings.Secure.getString(getContentResolver(),Settings.Secure.ANDROID);
试一试{
List params=new ArrayList();
添加(新的BasicNameValuePair(“用户名”,用户名));
添加参数(新的BasicNameValuePair(“Android”,Android));
Log.d(“请求!”,“启动”);
JSONObject json=jsonParser.makeHttpRequest(
登录URL,“POST”,参数);
//检查日志中的json响应
Log.d(“登录尝试”,json.toString());
//json的成功标记
成功=1;
如果(成功==1){
Log.d(“成功登录!”,json.toString());
意图ii=新意图(Login.this、Menu.class);
完成();
//此finish()方法用于告诉android操作系统当前//活动已完成,请转到其他活动
星触觉(ii);
返回json.getString(TAG_消息);
}否则{
返回json.getString(TAG_消息);
}
}捕获(JSONException e){
e、 printStackTrace();
}
返回null;
}
/**
*后台处理完成后,我们需要尽快关闭进度对话框
* **/
受保护的void onPostExecute(字符串消息){
pDialog.disclose();
如果(消息!=null){
Toast.makeText(Login.this,message,Toast.LENGTH_LONG).show();
}
}
}
有什么想法吗? 谢谢
Yoshi

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

String username = user.getText().toString();
相反,您应该将数据传递给异步任务,如下所示:

new AttemptLogin().execute(user.getText().toString());
另外,您正在从
doInBackground
方法启动一个活动。您应该将该代码段移动到
onPostExecute
方法

我得到了这个错误:“必须从UI调用方法getText 线程,当前推断的线程为工作线程“

你可以走了

String username = user.getText().toString();
在onClick方法中,将字符串传递给AsyncTask,如

new AttemptLogin().execute(username);
调用
doInBackground
时,您可以通过
String…args
访问它,例如
args[0]