尝试调用虚拟方法 – 空对象引用

huangapple go评论71阅读模式
英文:

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&lt;String&gt;() {
            @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(), &quot;Color:&quot; + colorEnvelope.getColorRGB(), Toast.LENGTH_SHORT).show();
            }
        });



        return root;
    }
}

And this is the Logcat-Error:

java.lang.NullPointerException: Attempt to invoke virtual method &#39;void com.skydoves.colorpickerpreference.ColorPickerView.setColorListener(com.skydoves.colorpickerpreference.ColorListener)&#39; 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:

&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    android:paddingLeft=&quot;16dp&quot;
    android:paddingRight=&quot;16dp&quot;
    android:orientation=&quot;horizontal&quot;
    android:gravity=&quot;center&quot;&gt;

    &lt;com.skydoves.colorpickerpreference.ColorPickerView
        android:id=&quot;@+id/colorPickerView&quot;
        android:layout_width=&quot;300dp&quot;
        android:layout_height=&quot;300dp&quot;
        app:palette=&quot;@drawable/palette&quot;
        app:selector=&quot;@drawable/wheel&quot; /&gt;


&lt;/LinearLayout&gt;

答案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(), &quot;Color:&quot; + colorEnvelope.getColorRGB(), Toast.LENGTH_SHORT).show();
            }
        });
}

huangapple
  • 本文由 发表于 2020年10月11日 03:04:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/64297191.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定