unpix
unpix

Reputation: 853

Problems with getText() from EditText

http://pastebin.com/babqrrqa

Like the title says, i have problems with getting the text from a EditText.

My app crashes when reads this line :

String value = aux.getText().toString();, probably because aux.getText() is null still i have setText("lol"), at this method

 public boolean onOptionsItemSelected(MenuItem item) {  
    switch (item.getItemId()) {  
    case R.id.adicionar:  
        //Toast.makeText(this, "This is the Toast message", Toast.LENGTH_LONG).show();
        final AlertDialog.Builder alert = new AlertDialog.Builder(this);
        final EditText input = new EditText(this);

         LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         LinearLayout dialerLayout = (LinearLayout) layoutInflater.inflate(R.layout.input, null);
         LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
            dialerLayout.setLayoutParams(params);


        alert.setView(dialerLayout);



        alert.setPositiveButton("Adicionar", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {


                aux = (EditText)findViewById(R.id.cadeira);
                aux.setText("lol");
                String value = aux.getText().toString();
                //String value2="TESTE";

            lista.add(value);
             adapter.notifyDataSetChanged();

            }

        });

        alert.setNegativeButton("Cancelar",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        dialog.cancel();
                    }
                });
        alert.show();



    return true;  
    default: return super.onOptionsItemSelected(item);  
    }
    }  

Upvotes: 0

Views: 1461

Answers (3)

Abdul Wahab
Abdul Wahab

Reputation: 819

there might be human error with declaring or initializing "aux", though you haven't shown the exactly error log wording, if you can detail it more, this will fix the prob faster.

Upvotes: 0

Dimitris Makris
Dimitris Makris

Reputation: 5183

Probably the "aux" is null.

As I suppose from your code the "cadeira" EditText is in the LinearLayout that you inflate. But when you execute "findViewByById" it searches the main layout. Thus you should try:

aux = (EditText) dialerLayout.findViewById(R.id.cadeira);

Upvotes: 1

Rupert Horlick
Rupert Horlick

Reputation: 681

This might be because of the .toString()! The result of the getText() will be a string already so the toString() might cause it to crash.

Upvotes: 0

Related Questions