[英]Android app crashes with “Attempt to invoke virtual method on a null object reference”
I have a "Bottom navigation bar app" with 3 tabs or fragments.我有一个带有 3 个选项卡或片段的“底部导航栏应用程序”。 On the first fragment I have a PaintView that I can draw on.
在第一个片段上,我有一个可以绘制的 PaintView。 Drawing works great.
绘图效果很好。 But I'm trying to wire up a clear screen function that is invoked via an options menu dropdown.
但我正在尝试连接一个通过选项菜单下拉调用的清晰屏幕 function。 When I select the option to wipe the screen from the options menu, the app crashes with the following error:
当我 select 从选项菜单中选择擦除屏幕时,应用程序崩溃并出现以下错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.mobile_testapp_android_2.ui.home.PaintView.clearView()' on a null object reference
Here is the code in the HomeFragment.java file for the options menu:这是选项菜单的 HomeFragment.java 文件中的代码:
public class HomeFragment extends Fragment {
private PaintView paintView;
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
PaintView homeFragmentView = new PaintView(requireContext());
return homeFragmentView;
}
//Enable Clear Menu in this fragment
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
setHasOptionsMenu(true);
super.onCreate(savedInstanceState);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
//inflate menu
inflater.inflate(R.menu.menu_swipes, menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
//Handle menu item clicks here
int id = item.getItemId();
if (id == R.id.action_clearScreen) {
//do clear function here:
paintView.clearView();
Toast.makeText(getActivity(), "Clear Screen", Toast.LENGTH_SHORT).show();
}
return super.onOptionsItemSelected(item);
}
}
And here is the code in the PaintView file which contains the clearView() method that I am trying to invoke:这是 PaintView 文件中的代码,其中包含我试图调用的 clearView() 方法:
public class PaintView extends View {
public ViewGroup.LayoutParams params;
private Path myPath = new Path();
private Paint brush = new Paint();
private boolean forceClear;
public PaintView(Context context) {
super(context);
brush.setAntiAlias(true);
brush.setColor(Color.MAGENTA);
brush.setStyle(Paint.Style.STROKE);
brush.setStrokeJoin(Paint.Join.ROUND);
brush.setStrokeWidth(8f);
params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
//Clear function
public void clearView() {
try {
forceClear = true;
invalidate();
} catch (Exception ee) {
Log.d("Clear Button: ", "We blew up!! " + ee);
}
}
I'm thinking that the problem is due to the way that I instantiate the PaintView class in the HomeFragment.java file, but I don't get any compilation errors and I can use dot notation paintView.clearView()
to access the methods within the PaintView class just fine.我认为问题是由于我在 HomeFragment.java 文件中实例化 PaintView class 的方式,但我没有得到任何编译错误,我可以使用点表示法
paintView.clearView()
来访问其中的方法PaintView class 就好了。
Any tips on what I am doing wrong would be greatly appreciated!任何关于我做错了什么的提示将不胜感激!
Here are a few screenshots:以下是一些屏幕截图:
Try to use the variables, which you define:尝试使用您定义的变量:
// PaintView homeFragmentView = new PaintView(requireContext());
this.paintView = new PaintView(requireContext());
It looks like you declare, but never actually instantiate the paintView
member variable.看起来您声明了,但从未实际实例化
paintView
成员变量。
Try this:尝试这个:
private PaintView paintView;
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
this.paintView = new PaintView(requireContext());
return this.paintView;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.