java – Android Studio错误:“必须从UI线程调用方法getText(),当前推断的线程是worker

前端之家收集整理的这篇文章主要介绍了java – Android Studio错误:“必须从UI线程调用方法getText(),当前推断的线程是worker前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在 android studio中创建一个CRUD操作,但我不断收到错误.错误是当我检查LogCat这是他们告诉我的

line 156-158
1907-1931/com.example.casquejo.loginadmin E/AndroidRuntime﹕ FATAL
EXCEPTION: AsyncTask #2
Process: com.example.casquejo.loginadmin,PID: 1907
java.lang.RuntimeException: An error occured while executing
doInBackground()
Caused by: java.lang.NullPointerException atcom.example.casquejo.loginadmin.NewProductActivity$CreateNewProduct.doInBackground(NewProductActivity.java:85)
at com.example.casquejo.loginadmin.NewProductActivity$CreateNewProduct.doInBackground(NewProductActivity.java:58)
atcom.example.casquejo.loginadmin.NewProductActivity$CreateNewProduct.onPreExecute(NewProductActivity.java:67)
atcom.example.casquejo.loginadmin.NewProductActivity$1.onClick(NewProductActivity.java:53)

有人可以帮我这个或者有人可以提出一个想法如何解决这个问题下面是我的java类EditProductActivity.class的代码

package com.example.casquejo.loginadmin;

        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;

        /**
        * Created by Casquejo on 9/14/2015.
        */
        public class NewProductActivity extends Activity {
    private ProgressDialog pDialog;

    JSONParser jsonParser = new JSONParser();
    EditText inputName;
    EditText inputPrice;
    EditText inputDesc;

    private static String url_create_product = "http://10.0.2.2/android_connect/create_product.PHP";

    private static final String TAG_SUCCESS = "success";

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

        inputName = (EditText) findViewById(R.id.inputName);
        inputPrice = (EditText) findViewById(R.id.inputPrice);
        inputDesc = (EditText) findViewById(R.id.inputDesc);

        Button btnCreateProduct = (Button) findViewById(R.id.btnCreateProduct);
        btnCreateProduct.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                String name = inputName.getText().toString();
                String price = inputPrice.getText().toString();
                String description = inputDesc.getText().toString();
                new CreateNewProduct().execute(name,price,description);
            }
        });
    }

    class CreateNewProduct extends AsyncTask<String,String,String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(NewProductActivity.this);
            pDialog.setMessage("Creating Product..");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();

        }

        protected String doInBackground(String... args) {

            String name = args[0],price = args[1],description = args[2];

            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("name",name));
            params.add(new BasicNameValuePair("price",price));
            params.add(new BasicNameValuePair("description",description));

            JSONObject json = jsonParser.makeHttpRequest(url_create_product,"POST",params);

            Log.d("Create Response",json.toString());

            try {
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    Intent i = new Intent(getApplicationContext(),AllProductsActivity.class);
                    startActivity(i);
                    finish();
                }
                else {

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        protected void onPostExecute(String file_url) {
            pDialog.dismiss();
        }

    }
}

解决方法

ide指的是
String name = txtName.getText().toString();
  String price = txtPrice.getText().toString();
  String description = txtDesc.getText().toString();

读取值不应该是一个问题,但为了摆脱这个警告/错误,您可以将其移动到onClick并通过execute()传递值.例如.

btnSave.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View arg0) {
        String name = txtName.getText().toString();
        String price = txtPrice.getText().toString();
        String description = txtDesc.getText().toString();
        new SaveProductDetails().execute(name,description);
    }
});

调用doInBackground时,你可以通过前params,String … args读取那些bac.三个点构造保留为varargs,并且varargs可以使用[]表示法像数组一样访问.在示例的情况下,

args [0]包含name的值,args [1]包含price的值,args [2]包含description的值.

原文链接:https://www.f2er.com/android/127255.html

猜你在找的Android相关文章