java - 空对象引用上的 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;
解决方案
您的代码没有“setContentView(R.layout.{file name of layout})”。
你能检查一下吗?它应该在使用 findViewById 方法之前执行。
推荐阅读
- razor - 将背景图像设置为 ``
- pine-script - 无法编译 MVWAP 指标
- c++ - 对假设数组元素的访问肯定是 UB 吗?
- c# - 用于 asp.net 核心的 Django shell 类似功能
- sustainsys-saml2 - sustainsys:无法解析属性“x509FindType”的值
- javascript - Uncaught (in promise) TypeError: this.loadCategories is not a function in React
- javascript - Javascript:网页上的条件计数时间和广播自定义事件
- html - 试图使无线电输入/标签响应
- apache-kafka - 当我尝试从 kafka 服务器上传数据时,德鲁伊出现 504 错误
- r - 在R中按ID组合数据