Android Studio错误:必须从UI线程调用方法getText(),当前推断的线程是worker [英] Android Studio error: "Method getText() must be called from the UI Thread, currently inferred thread is worker

查看:247
本文介绍了Android Studio错误:必须从UI线程调用方法getText(),当前推断的线程是worker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在android studio中创建一个CRUD操作,但我一直在收到错误。错误是当我检查LogCat时这是他们给我看的东西

i'm creating a CRUD operation in android studio but i kept getting errors. the error is when i check the LogCat this is what they show me


156-158行

1907-1931 /com.example.casquejo.loginadmin E / AndroidRuntime:FATAL
EXCEPTION:AsyncTask#2
进程:com.example.casquejo.loginadmin,PID:1907
java.lang.RuntimeException:An执行
时发生错误doInBackground()
引起: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)

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

can someone help me with this or can someone give an idea how to fix this` below is the code for my java class 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()传递值。例如

reading the values shouldn't be a problem, but in order to get rid of this warning/error, you can move it into the onClick and pass the values through execute(). E.g.

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, price, description);
    }
});

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

when doInBackground is invoked, you can read those bac, through the former params, String... args. The three dots construct stays for varargs, and varargs can be access like an array, using the [] notation. In the case of the example,

args [0] 包含 name的值 args [1] 包含价格的值args [2] 包含 description 的值。

args[0] contains the value of name, args[1] contains the value of price and args[2] contains the value of description.

这篇关于Android Studio错误:必须从UI线程调用方法getText(),当前推断的线程是worker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆