如何修复片段中的 RecyclerView.setLayoutManager NullPointerException

how to fix RecyclerView.setLayoutManager NullPointerException in fragment

错误:

process: tifawt.com.yourschool, PID: 8372
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{tifawt.com.yourschool/tifawt.com.yourschool.activities.main_activity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2957)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
                      at android.app.ActivityThread.-wrap11(Unknown Source:0)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
                      at android.os.Handler.dispatchMessage(Handler.java:105)
                      at android.os.Looper.loop(Looper.java:164)
                      at android.app.ActivityThread.main(ActivityThread.java:6944)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
                      at tifawt.com.yourschool.fragments.schools_list_fragment.onCreateView(schools_list_fragment.java:69)
                      at android.support.v4.app.Fragment.performCreateView(Fragment.java:2439)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
                      at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
                      at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802)
                      at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
                      at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
                      at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
                      at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
                      at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3273)
                      at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3229)
                      at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:201)
                      at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:620)
                      at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
                      at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1340)
                      at android.app.Activity.performStart(Activity.java:7200)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2920)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032) 
                      at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696) 
                      at android.os.Handler.dispatchMessage(Handler.java:105) 
                      at android.os.Looper.loop(Looper.java:164) 
                      at android.app.ActivityThread.main(ActivityThread.java:6944) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)  Application terminated.

代码:

public class schools_list_fragment extends Fragment {

    school_view_model school_model ;


    school new_school ;



    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        }

        getActivity().setTitle("The Best Schools of All time");

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_card, container, false);

        RecyclerView recyclerView = view.findViewById(R.id.card_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setHasFixedSize(true);

        final school_adapter adapter = new school_adapter();
        recyclerView.setAdapter(adapter);

        school_model = ViewModelProviders.of(this).get(school_view_model.class);


        school_model.get_all_schools().observe(this, new Observer<List<school>>() {
            @Override
            public void onChanged(@Nullable List<school> notes) {
                adapter.setSchools_list(notes);
            }
        });



        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }


  }

我发现在 XML 中设置布局管理器要容易得多 app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"

如果你没有使用androidx app:layoutManager="android.support.v7.widget.LinearLayoutManager"

如果你想在Java中完成,那么你需要将它放在生命周期的onViewCreated中或之后。

请检查 view.findViewById(R.id.card_view) 如果 id 是 card_view,我个人认为你弄错了 id。这只是一个猜测,因为您还没有在此处粘贴您的布局文件。希望对您有所帮助!!!