Android Studio error: "The getText () method must be called from the user interface thread, the current output is working

I am creating a CRUD operation in Android studio, but I kept getting errors. The error when I check LogCat is what they show me.

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

can someone help me with this or can someone let me know 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(); } } } 
+6
source share
5 answers

ideal means

  String name = txtName.getText().toString(); String price = txtPrice.getText().toString(); String description = txtDesc.getText().toString(); 

reading values ​​should not be a problem, but in order to get rid of this warning / error, you can move it to onClick and pass the values ​​through execute() . For instance.

 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); } }); 

when doInBackground is doInBackground , you can read those bac through the previous String... args parameters. Three points are built for varargs, and varargs can be accessed as an array using the [] notation. In case of an example

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

+6
source

You call getText() from the background thread created by AsyncTask .

Extract the text first and then call your async task. Here is an example

 new SaveProductDetails() .execute(txtName.getText().toString(), txtPrice.getText().toString(), txtDesc.getText().toString()); 

And inside the SaveProductDetails doInBackground method:

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

In asynctask, the doInBackground(...) method works in a background (non-UI) thread. As you can see in the error above, you are not allowed to interact with user interface elements from the background thread.

You can either pass arguments to the background thread, as suggested in one of the other answers, or change your synthesis so that the values ​​of the user interface string are read in the onPreExecute() method, which is executed in the thread user interface (like the onPostExecute() method) .

 class SaveProductDetails extends AsyncTask<String, String, String> { private String name, price, description; @Override protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(EditProductActivity.this); pDialog.setMessage("Saving product ..."); pDialog.setIndeterminate(false); pDialog.setCancelable(true); pDialog.show(); name = txtName.getText().toString(); price = txtPrice.getText().toString(); description = txtDesc.getText().toString(); } protected String doInBackground(String... args) { //... Use as you would before 

I would suggest taking a look at a blog post, for example this one , to learn more about AsyncTasks, how they work, how to use them including data such as which method runs in this thread.

+2
source

You can read this Using variables in a user interface thread from a workflow .
In your question, you are trying to access TextView text from a background thread. To be consistent, since you should not do this, because it may be the possibility that the main thread (TextView UI thread at the same time). To avoid such landscapes, you can do something like this:

 class SaveProductDetails extends AsyncTask<String, String, String>{ //create constructor and pass values of text view in it String textViewValue1; public SaveProductDetails (String textViewValue1 ){ this.textViewValue1=textViewValue1 } //other code below } 
0
source

You do not need to pass a value to the execute method. Set the name, price and description variables as global. To get the value from EditTest, click the Code button, as shown below:

Every time you need to click on a button to get JSON data.

 btnSave.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { name = txtName.getText().toString(); price = txtPrice.getText().toString(); description = txtDesc.getText().toString(); new CreateNewProduct().execute(); } }); 

Now the name, price and description have the meaning that you entered.

Now your CreateNewProduct class looks like this:

 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) { 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(); } 

}

0
source

All Articles