0

i'm trying to create android app with inserting information but i get this error " method gettext must be called from the ui thread " and i get red lines under editTextName.getText() and under editTextAdd.getText() this is my code how can i fix it explain to me with details please thanks for helping

import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

public class Inserting extends ActionBarActivity {

    private EditText editTextName;
    private EditText editTextAdd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.inserting);

        editTextName = (EditText) findViewById(R.id.editTextName);
        editTextAdd = (EditText) findViewById(R.id.editTextAddress);


    }

    public void insert(View view){
        String name = editTextName.getText().toString();
        String add = editTextAdd.getText().toString();

        insertToDatabase(name,add);
    }

    private void insertToDatabase(String name, String add){
        class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
            @Override
            protected String doInBackground(String... params) {
                String paramUsername = params[0];
                String paramAddress = params[1];


                String name = editTextName.getText().toString();
                String add = editTextAdd.getText().toString();

                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("name", name));
                nameValuePairs.add(new BasicNameValuePair("address", add));

                try {
                    HttpClient httpClient = new DefaultHttpClient();
                    HttpPost httpPost = new HttpPost(
                            "http://192.168.215.14/insert-db.php");
                    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                    HttpResponse response = httpClient.execute(httpPost);

                    HttpEntity entity = response.getEntity();


                } catch (ClientProtocolException e) {

                } catch (IOException e) {

                }
                return "success";
            }

            @Override
            protected void onPostExecute(String result) {

                super.onPostExecute(result);

                Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
                TextView textViewResult = (TextView) findViewById(R.id.textViewResult);
                textViewResult.setText("Inserted");
            }
        }
        SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
        sendPostReqAsyncTask.execute(name, add);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
mh9
  • 45
  • 1
  • 9
  • Well then you'll have to run `gettext` on the UI thread. Use your favourite search engine on the terms `android` and `run on UI thread` - you'll find kajillion of answers. – m02ph3u5 Apr 24 '16 at 17:04
  • Possible duplicate of [What is the Android UiThread (UI thread)](http://stackoverflow.com/questions/3652560/what-is-the-android-uithread-ui-thread) – m02ph3u5 Apr 24 '16 at 17:04
  • 1
    Also subclass AppCompatActivity class in your activities – Eenvincible Apr 24 '16 at 17:25
  • i saw kajillion of answers but i just prefer to ask by myself to let someone explain with millions details to me cuz really i'm to bigginer in android, thank u too – mh9 Apr 24 '16 at 17:30

2 Answers2

0

Do not refer to widgets from inside doInBackground(). Pass in the values you are getting from those EditText widgets, either to a constructor on your SendPostReqAsyncTask, or as additional strings to your execute() call.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0
...
private void insertToDatabase(String n, String a){
    final String name = editTextName.getText().toString();
    final String add = editTextAdd.getText().toString();
    class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... params) {
            String paramUsername = params[0];
            String paramAddress = params[1];

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
...
F43nd1r
  • 7,690
  • 3
  • 24
  • 62
  • you mean i just need to change this section of my code with the section u write ? `private void insertToDatabase(String name, String add){ class SendPostReqAsyncTask extends AsyncTask { @Override protected String doInBackground(String... params) { String paramUsername = params[0]; String paramAddress = params[1]; String name = editTextName.getText().toString(); String add = editTextAdd.getText().toString(); List nameValuePairs = new ArrayList();` – mh9 Apr 24 '16 at 17:10
  • `you mean i just need to change this section of my code with the section u write ?` yes. I can't read the code in your comment though. – F43nd1r Apr 24 '16 at 17:12
  • ok i change it but =( now i have red lines under name and add in this line of code : `final String name = editTextName.getText().toString();` – mh9 Apr 24 '16 at 17:19
  • That is a name conflict with the parameters. Rename either the final variables or the method parameters. – F43nd1r Apr 24 '16 at 17:22
  • no red lines now thank you really soooooooooooo mush – mh9 Apr 24 '16 at 17:26