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

时间:2019-02-08 17:46:41

标签: android android-fragments android-recyclerview linearlayoutmanager

错误:

SELECT * FROM smth WHERE CAST(? as integer) > 2

代码:

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.

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

我发现在XML中设置layoutManager更容易 app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"

如果您未使用androidx app:layoutManager="android.support.v7.widget.LinearLayoutManager"

如果要使用Java进行操作,则需要将其放入生命周期中的onViewCreated中或之后。