как исправить RecyclerView.setLayoutManager NullPointerException во фрагменте - PullRequest
0 голосов
/ 08 февраля 2019

Ошибка:

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);

    }


  }

Ответы [ 2 ]

0 голосов
/ 08 февраля 2019

Пожалуйста, отметьте view.findViewById (R.id.card_view) , если id является card_view, лично я думаю, что вы получаете неправильный идентификатор.Это всего лишь предположение, поскольку вы не вставили сюда файл макетаНадеюсь, это поможет !!!

0 голосов
/ 08 февраля 2019

Мне гораздо проще установить layoutManager в XML app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"

, если вы не используете androidx app:layoutManager="android.support.v7.widget.LinearLayoutManager"

Если вы хотите сделать это на Java, тогда выпотребуется поместить его в или после onViewCreated в жизненном цикле.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...