S Shah
S Shah

Reputation: 428

void android.widget.ImageView.setEnabled(boolean)' on a null object reference

I am using a simple java library file for Undo and Redo text as shown in the tutorial and sample android app but for me when I run the app it shows me the following error

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setEnabled(boolean)' on a null object reference
    at com.apps.primalnotes.Fragments.EditorFragment.textAction(EditorFragment.java:1023)
    at com.apps.primalnotes.Fragments.EditorFragment.onCreateView(EditorFragment.java:84)

Following is the library and method I am following on GitHub enter link description here

And exactly i am doing the following

 EditorFragment extends Pantalla  implements TextUndoRedo.TextChangeInfo, View.OnClickListener{
    private TextUndoRedo TUR;
    private ImageView undo, redo;

 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.editor, container, false);

        getActivity().setTitle("Editor");

        setHasOptionsMenu(true);

        imm =(InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        base = new DataBase(context, null);


    text = (EditText) view.findViewById(R.id.texto);
    undo = (ImageView)view.findViewById(R.id.undo);
    redo = (ImageView)view.findViewById(R.id.redo);
    TUR = new TextUndoRedo(text, this);

            textAction(); "Showing error here"

            undo.setOnClickListener(this);
            redo.setOnClickListener(this);
    }

    @Override
        public void textAction() {
            undo.setEnabled(TUR.canUndo());  "Showing error here"
            redo.setEnabled(TUR.canRedo());
        }

    @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.undo:
                    TUR.exeUndo();
                    break;
                case R.id.redo:
                    TUR.exeRedo();
                    break;

Upvotes: 0

Views: 1021

Answers (3)

F4y5
F4y5

Reputation: 43

Check your XML. Your ImageView for undo & redo should have an id that looks like this,

android:id="@+id/undo

or

android:id="@+id/redo"

Upvotes: 0

MrX
MrX

Reputation: 993

Here the code from the Github source

btn_undo = (Button) findViewById(R.id.btn_undo);
btn_undo.setOnClickListener(this);
btn_redo = (Button) findViewById(R.id.btn_redo);
btn_redo.setOnClickListener(this);

And here the code from your code :

text = (EditText) view.findViewById(R.id.texto);
undo = (ImageView)view.findViewById(R.id.undo);
redo = (ImageView)view.findViewById(R.id.redo);
TUR = new TextUndoRedo(text, this);

The sample using Button for the undo/redo and you using ImageView. Check what is your ImageView ID in xml. Same with other here, your undo/redo ImageView is Null.

Upvotes: 0

Seungmin Ha
Seungmin Ha

Reputation: 151

Your code doesn't have a "setContentView(R.layout.{file name of layout})".

Could you check it? It should be performed before using findViewById method.

Upvotes: 1

Related Questions