英文:
Attempt to invoke virtual method - Null object reference
问题
以下是您提供的代码的翻译部分:
我正在尝试实现一个外部颜色选择器,并且它应该出现在片段中,但我的应用程序一直崩溃。我认为我理解了为什么会发生这种情况,但在尝试修复了大约6小时后,我快要放弃了。我知道以前有人问过这个问题,但我无法为我的特定问题找到解决方法。这是我的代码:
public class ColorchoiceFragment extends Fragment {
private ColorchoiceViewModel galleryViewModel;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
galleryViewModel =
ViewModelProviders.of(this).get(ColorchoiceViewModel.class);
View root = inflater.inflate(R.layout.fragment_colorchoice, container, false);
galleryViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
@Override
public void onChanged(@Nullable String s) {
}
});
ColorPickerView colorPickerView = (ColorPickerView) root.findViewById(R.id.colorPickerView);
colorPickerView.setColorListener(new ColorListener() {
@Override
public void onColorSelected(ColorEnvelope colorEnvelope) {
Toast.makeText(getActivity(), "颜色:" + colorEnvelope.getColorRGB(), Toast.LENGTH_SHORT).show();
}
});
return root;
}
}
这是Logcat错误:
java.lang.NullPointerException: 尝试在空对象引用上调用虚拟方法 'void com.skydoves.colorpickerpreference.ColorPickerView.setColorListener(com.skydoves.colorpickerpreference.ColorListener)'
at com.lsh.homeauto.ui.colorchoice.ColorchoiceFragment.onCreateView(ColorchoiceFragment.java:43)
这是我的XML,如您所请求:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="horizontal"
android:gravity="center">
<com.skydoves.colorpickerpreference.ColorPickerView
android:id="@+id/colorPickerView"
android:layout_width="300dp"
android:layout_height="300dp"
app:palette="@drawable/palette"
app:selector="@drawable/wheel" />
</LinearLayout>
希望这能帮助您解决问题。如果您需要更多帮助,请告诉我。
英文:
I am trying to implement an external colorwheel and the fragment where it should appear in keeps crashing my app. I think I understood why this happen´s, but after around 6 hours of trying to fix it I´m about to give up. I know this was asked befor, but I could not derive a fix for my specific problem. Here ist my code:
private ColorchoiceViewModel galleryViewModel;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
galleryViewModel =
ViewModelProviders.of(this).get(ColorchoiceViewModel.class);
View root = inflater.inflate(R.layout.fragment_colorchoice, container, false);
galleryViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
@Override
public void onChanged(@Nullable String s) {
}
});
ColorPickerView colorPickerView = (ColorPickerView) root.findViewById(R.id.colorPickerView);
colorPickerView.setColorListener (new ColorListener() {
@Override
public void onColorSelected(ColorEnvelope colorEnvelope) {
Toast.makeText(getActivity(), "Color:" + colorEnvelope.getColorRGB(), Toast.LENGTH_SHORT).show();
}
});
return root;
}
}
And this is the Logcat-Error:
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.skydoves.colorpickerpreference.ColorPickerView.setColorListener(com.skydoves.colorpickerpreference.ColorListener)' on a null object reference
at com.lsh.homeauto.ui.colorchoice.ColorchoiceFragment.onCreateView(ColorchoiceFragment.java:43)
It would be really great if someone could help me fix this. Thanks in advance^^
Edit: Here´s my XML, as requested:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="horizontal"
android:gravity="center">
<com.skydoves.colorpickerpreference.ColorPickerView
android:id="@+id/colorPickerView"
android:layout_width="300dp"
android:layout_height="300dp"
app:palette="@drawable/palette"
app:selector="@drawable/wheel" />
</LinearLayout>
答案1
得分: 1
这可能是因为您正在尝试在尚未创建的视图上使用 setColorListener
。请注意,您正在 onCreateView
方法中使用此方法。我建议您将这些行移到 onViewCreated
方法中。
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ColorPickerView colorPickerView = (ColorPickerView) view.findViewById(R.id.colorPickerView);
colorPickerView.setColorListener(new ColorListener() {
@Override
public void onColorSelected(ColorEnvelope colorEnvelope) {
Toast.makeText(getActivity(), "Color:" + colorEnvelope.getColorRGB(), Toast.LENGTH_SHORT).show();
}
});
}
英文:
This might be because you are trying to use setColorListener
on a view which is not even created. Note that you are using this method in onCreateView
method. I suggest you to move those lines in onViewCreated
method.
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ColorPickerView colorPickerView = (ColorPickerView) view.findViewById(R.id.colorPickerView);
colorPickerView.setColorListener (new ColorListener() {
@Override
public void onColorSelected(ColorEnvelope colorEnvelope) {
Toast.makeText(getActivity(), "Color:" + colorEnvelope.getColorRGB(), Toast.LENGTH_SHORT).show();
}
});
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论