Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.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 错误:必须从UI线程调用getText_Java_Android_Json_Listview - Fatal编程技术网

Java 错误:必须从UI线程调用getText

Java 错误:必须从UI线程调用getText,java,android,json,listview,Java,Android,Json,Listview,我正在尝试将姓名、电话和电子邮件输入mysql数据库。我在doInBackground方法中遇到一个错误,需要从ui线程调用getText 这是我的用户详细信息文件: import java.util.ArrayList; import java.util.List; import org.apache.http.NameValuePair; import org.apache.http.message.BasicNameValuePair; import org.json.JSONExcep

我正在尝试将姓名、电话和电子邮件输入mysql数据库。我在doInBackground方法中遇到一个错误,需要从ui线程调用getText

这是我的用户详细信息文件:

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class UserDetails extends Activity {

// Progress Dialog
private ProgressDialog pDialog;

JSONParser1 jsonParser = new JSONParser1();
EditText inputName;
EditText inputPhone;
EditText inputEmail;

// url to create new product
private static String url_create_product = "http://bookit.net16.net//book.php";

// JSON Node names
private static final String TAG_SUCCESS = "success";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.content_user_details);

    // Edit Text
    inputName = (EditText) findViewById(R.id.inputName);
    inputPhone = (EditText) findViewById(R.id.inputPhone);
    inputEmail = (EditText) findViewById(R.id.inputEmail);


    // Create button
    Button btnCreateProduct = (Button) findViewById(R.id.proceed);

    // button click event
    btnCreateProduct.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            // creating new product in background thread
            new CreateNewProduct().execute();
        }
    });
}

/**
 * Background Async Task to Create new product
 * */
class CreateNewProduct extends AsyncTask<String, String, String> {


    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(UserDetails.this);
        pDialog.setMessage("Creating Product..");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    /**
     * Creating product
     * */
    protected String doInBackground(String... args) {
     String name = inputName.getText().toString();
      String phone = inputPhone.getText().toString();
    String email = inputEmail.getText().toString();



        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("name", name));
        params.add(new BasicNameValuePair("phone", phone));
        params.add(new BasicNameValuePair("email", email));

        // getting JSON Object
        // Note that create product url accepts POST method
        JSONObject json = JSONParser1.makeHttpRequest(url_create_product,
                "POST", params);

        // check log cat fro response
        Log.d("Create Response", json.toString());

        // check for success tag
        try {
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // successfully created product


                // closing this screen
                finish();
            } else {
                // failed to create product
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
        // dismiss the dialog once done
        pDialog.dismiss();
    }

}
import java.util.ArrayList;
导入java.util.List;
导入org.apache.http.NameValuePair;
导入org.apache.http.message.BasicNameValuePair;
导入org.json.JSONException;
导入org.json.JSONObject;
导入android.app.Activity;
导入android.app.ProgressDialog;
导入android.content.Intent;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.view;
导入android.widget.Button;
导入android.widget.EditText;
公共类UserDetails扩展活动{
//进度对话框
私人对话;
JSONParser1 jsonParser=新的JSONParser1();
编辑文本输入名;
编辑文本输入电话;
编辑文本输入电子邮件;
//创建新产品的url
私有静态字符串url\u创建\u产品=”http://bookit.net16.net//book.php";
//JSON节点名称
私有静态最终字符串标记_SUCCESS=“SUCCESS”;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.content\u user\u详细信息);
//编辑文本
inputName=(EditText)findViewById(R.id.inputName);
inputPhone=(EditText)findViewById(R.id.inputPhone);
inputEmail=(EditText)findViewById(R.id.inputEmail);
//创建按钮
按钮btnCreateProduct=(按钮)findViewById(R.id.procedure);
//按钮点击事件
btnCreateProduct.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
//在后台线程中创建新产品
新建CreateNewProduct().execute();
}
});
}
/**
*创建新产品的后台异步任务
* */
类CreateNewProduct扩展了AsyncTask{
/**
*在启动后台线程显示进度对话框之前
* */
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=newprogressdialog(UserDetails.this);
pDialog.setMessage(“创建产品…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(真);
pDialog.show();
}
/**
*创造产品
* */
受保护的字符串doInBackground(字符串…args){
字符串名称=inputName.getText().toString();
字符串phone=inputPhone.getText().toString();
字符串email=inputEmail.getText().toString();
//建筑参数
List params=new ArrayList();
参数添加(新的BasicNameValuePair(“名称”),名称);
参数添加(新的BasicNameValuePair(“电话”,电话));
参数添加(新的BasicNameValuePair(“电子邮件”),电子邮件);
//获取JSON对象
//请注意,创建产品url接受POST方法
JSONObject json=JSONParser1.makeHttpRequest(url\u create\u product,
“POST”,params);
//检查cat fro响应日志
d(“创建响应”,json.toString());
//检查成功标签
试一试{
int success=json.getInt(TAG_success);
如果(成功==1){
//已成功创建产品
//关闭此屏幕
完成();
}否则{
//未能创建产品
}
}捕获(JSONException e){
e、 printStackTrace();
}
返回null;
}
/**
*完成后台任务后,关闭“进度”对话框
* **/
受保护的void onPostExecute(字符串文件\u url){
//完成后关闭对话框
pDialog.disclose();
}
}

}对视图的任何访问都必须在UI线程上完成,而不是在另一个线程或异步任务上完成。将它们作为参数传入,而不是在doInBackground中查询它们。在onPreExecute中请求它们是完全可以的。

创建一个类,扩展应用程序的类,并在其上定义一个处理程序

public class Global extends Application{
    public static Handler HANDLER = new Handler();
    onCreate(){
    }
}
然后,关于代码使用:

Global.HANDLER.post(new Runable(){
     // do anythin with UI you like
});

不要从
doInBackground
获取字符串,而是在调用异步任务并将其作为参数传递之前获取字符串

btnCreateProduct.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            // creating new product in background thread
               String name = inputName.getText().toString();
                String phone = inputPhone.getText().toString();
                String email = inputEmail.getText().toString();

                new CreateNewProduct(name,phone,email).execute();
        }
    });
并在异步任务中创建构造函数

class CreateNewProduct extends AsyncTask<String, String, String> {
        String name,  phone,  email;

        public CreateNewProduct(String param1, String param2, String param3) {
            name= param1;
            phone =param2;
            email = param3;
        }
   @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(UserDetails.this);
        pDialog.setMessage("Creating Product..");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    /**
     * Creating product
     * */
    protected String doInBackground(String... args) {

        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("name", name));
        params.add(new BasicNameValuePair("phone", phone));
        params.add(new BasicNameValuePair("email", email));

        // getting JSON Object
        // Note that create product url accepts POST method
        JSONObject json = JSONParser1.makeHttpRequest(url_create_product,
                "POST", params);

        // check log cat fro response
        Log.d("Create Response", json.toString());

        // check for success tag
        try {
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // successfully created product


                // closing this screen
                finish();
            } else {
                // failed to create product
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
        // dismiss the dialog once done
        pDialog.dismiss();
    }

}
class CreateNewProduct扩展了异步任务{
字符串名称、电话、电子邮件;
public CreateNewProduct(字符串param1、字符串param2、字符串param3){
name=param1;
电话=2;
email=param3;
}
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=newprogressdialog(UserDetails.this);
pDialog.setMessage(“创建产品…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(真);
pDialog.show();
}
/**
*创造产品
* */
受保护的字符串doInBackground(字符串…args){
//建筑参数
List params=new ArrayList();
参数添加(新的BasicNameValuePair(“名称”),名称);
参数添加(新的BasicNameValuePair(“电话”,电话));
参数添加(新的BasicNameValuePair(“电子邮件”),电子邮件);
//获取JSON对象
//请注意,创建产品url接受POST方法
JSONObject json=JSONParser1.makeHttpRequest(url\u create\u product,
“POST”,params);
//检查cat fro响应日志
d(“创建响应”,json.toString());
//检查成功标签
试一试{
int success=json.getInt(TAG_success);
如果(成功==1){
//已成功创建产品
//关闭此屏幕
完成();
}否则{
//未能创建产品
}
}捕获(JSONException e){
e、 printStackTrace();
}
返回null;
}
/**
*完成后台任务后,关闭“进度”对话框
* **/
受保护的void onPostExecute(字符串fil