1

This seems to have been asked in stackoverflow a bit before with no convincing answers for most questions. anyways, i attempt again. I have an Android App where am displaying a list where each row has an Edittext and Button component as seen below

enter image description here

The Edittext is non-editable when the view appears. I do this with the code below:

private void setNameAsEditable (View rowView, boolean setToEditable) {

    EditText textView = (EditText) rowView
            .findViewById(R.id.edittext_name);
    textView.setFocusableInTouchMode(setToEditable);
    textView.setFocusable(setToEditable);

    ImageButton button = (ImageButton) rowView
            .findViewById(R.id.button_save_name);

            if ( setToEditable ) {

                    button.setVisibility (View.VISIBLE);  // nullpointerexception here
    } else {
        button.setVisibility (View.GONE);
    }

When I long-press, and setToEditable is true, it throws nullpointerexception in the line indicated above. it doesn't seem to find the button component. However, 'button.setVisibility (View.GONE);' is executed when setToEditable is false without any issue.

Can someone please help?

user2903200
  • 708
  • 2
  • 7
  • 19
  • 3
    possible duplicate of [Android : show keyboard in edittext](http://stackoverflow.com/questions/20737686/android-show-keyboard-in-edittext) – Avijit Dec 23 '13 at 07:12
  • 2
    You no need to ask a same question in different post. – Avijit Dec 23 '13 at 07:17
  • can u post ur xml file which consist button and edittext – Kaushik Dec 23 '13 at 07:22
  • Must sure that your button is ImageButton or Button? – Piyush Dec 23 '13 at 07:37
  • @Andru this is not a duplicate of that. That is about keyboard showing up. This is about button visibility set to on/off. They are two different issues. – user2903200 Dec 23 '13 at 11:06
  • can you please post your xml for reference. i think you are missing id, can you please show exception log or xml which you have created – Amit Dec 23 '13 at 11:30

2 Answers2

1

rowView appears to be your EditText while you actually want the parent view that has the button inside of it. If you call findViewById and the id doesn't exist inside of that view, it will return null.

Kanwaljit Singh
  • 4,339
  • 2
  • 18
  • 21
Gabe
  • 1,239
  • 1
  • 13
  • 20
0

I think issue is initialization of view control. You should initialize EditText & ImageButton at the top.

      EditText textView;
      ImageButton button ;
      ...
      ...
      ...
      EditText textView = (EditText) rowView.findViewById(R.id.edittext_name);
      ImageButton button = (ImageButton) rowView.findViewById(R.id.button_save_name);

And next thing is try to write your code in try & catch block so you get exact idea what is wrong and where is bug.

Chintan Khetiya
  • 15,962
  • 9
  • 47
  • 85