简体   繁体   中英

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;

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.

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.

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM