首页 > 解决方案 > 空对象引用上的 void android.widget.ImageView.setEnabled(boolean)'

问题描述

我正在为撤消和重做文本使用一个简单的 java 库文件,如教程和示例 android 应用程序中所示,但对我来说,当我运行该应用程序时,它向我显示以下错误

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)

以下是我在 GitHub 上关注的库和方法,在此处输入链接描述

正是我正在做以下事情

 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;

标签: javaandroid

解决方案


您的代码没有“setContentView(R.layout.{file name of layout})”。

你能检查一下吗?它应该在使用 findViewById 方法之前执行。


推荐阅读