BottomSheetListener 即使在下面的 onAttach 中初始化后也会抛出空指针异常

BottomSheetListener throws null pointer exception even after initializing it below in onAttach

我正在创建一个模态底部 sheet,它将从用户那里获取过滤条件。我想将它与 Fragment (HomeFragment) 一起使用,并通过在我的 BottomSheetFragment 接口中的 onButtonClicked 方法中传递字符串值并在我的 HomeFragment 中获取这些值来获取从我的 HomeFragment 中的 BottomSheet 选择的按钮。即使在 onAttach 中初始化它之后,当我在 Log.i.

中显示它的状态时,它也会显示 null

这是我的模态底部sheet class:

public class StartupSearchBottomSheet extends BottomSheetDialogFragment {
    private StartupSearchBottomSheetListener sbsListener;
    Button show;
    String checkedCatButton, checkedSortButton;

    public View onCreateView(LayoutInflater inflater,  @Nullable ViewGroup container, @Nullable  Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.startupsearch_bottomsheet, container, false);

        show = v.findViewById(R.id.show);

        checkedCatButton = "Technology";
        checkedSortButton = "companyName";

        show.setOnClickListener(v -> {
            Log.i(TAG, "432: " + sbsListener); //shows me null here
            sbsListener.onButtonClicked(checkedCatButton, checkedSortButton);
            dismiss();
        });
        return v;
    }
    
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try{
            Log.i(TAG,  "431: onAttach invoked");
            sbsListener = (StartupSearchBottomSheetListener) getParentFragment();//initialized my listener here
        }catch(ClassCastException e){
            throw new ClassCastException(context + " must implement StartupSearchBottomSheetListener.");
        }
    }
}

这是我遇到的错误:

2022-04-03 21:38:25.330 4465-4465/com.phtlearning.nivesh E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.phtlearning.nivesh, PID: 4465
    java.lang.NullPointerException: Attempt to invoke interface method 'void com.phtlearning.nivesh.StartupSearchBottomSheet$StartupSearchBottomSheetListener.onButtonClicked(java.lang.String, java.lang.String)' on a null object reference
        at com.phtlearning.nivesh.StartupSearchBottomSheet.onClick(StartupSearchBottomSheet.java:183)
        at android.view.View.performClick(View.java:7575)
        at android.view.View.performClickInternal(View.java:7548)
        at android.view.View.access00(View.java:837)
        at android.view.View$PerformClick.run(View.java:28933)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:236)
        at android.app.ActivityThread.main(ActivityThread.java:8057)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:620)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1011)

听起来您的片段嵌套不正确,因此没有父片段。确保在添加时使用 getChildFragmentManager 而不是 getSupportFragmentManager,否则 getParentFragment() 将 return null.

像这样添加空检查也是一个好主意

sbsListener = (StartupSearchBottomSheetListener) getParentFragment();
if( sbsListener == null ) {
    throw new RuntimeException("Could not get listener");
}

因此,如果您期望 non-null 的变量为空,您会立即知道。