RecyclerView在片段崩溃后启动

时间:2017-12-06 19:33:26

标签: android android-fragments android-recyclerview

请帮忙。我在片段中使用了RecycleView。但是当我开始活动时...崩溃!我认为是从视角造成的。但不知道为什么! 有谁有想法? 嗨。请帮助。我在片段中使用了RecycleView。但是当我开始活动时...崩溃!我认为是从视角造成的。但不知道为什么! 有谁有想法?

fragmentHome.java

                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
                                                                          at android.app.ActivityThread.access$600(ActivityThread.java:156)
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                          at android.os.Looper.loop(Looper.java:153)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5297)
                                                                          at java.lang.reflect.Method.invokeNative(Native Method)
                                                                          at java.lang.reflect.Method.invoke(Method.java:511)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
                                                                          at dalvik.system.NativeStart.main(Native Method)
                                                                       Caused by: java.lang.NullPointerException
                                                                          at ir.dadevarzjooya.MainActivity2.HomeFragment2.onCreateView(HomeFragment2.java:60)
                                                                          at android.app.Fragment.performCreateView(Fragment.java:1695)
                                                                          at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
                                                                          at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
                                                                          at android.app.BackStackRecord.run(BackStackRecord.java:694)
                                                                          at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
                                                                          at android.app.Activity.performStart(Activity.java:5131)
                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2279)
                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358) 
                                                                          at android.app.ActivityThread.access$600(ActivityThread.java:156) 
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340) 
                                                                          at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                          at android.os.Looper.loop(Looper.java:153) 
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5297) 
                                                                          at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                          at java.lang.reflect.Method.invoke(Method.java:511) 
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) 
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 
                                                                          at dalvik.system.NativeStart.main(Native Method) 

和logcat中的错误是:

{{1}}

任何人都有想法? 我认为这个问题是由视图引起的......

1 个答案:

答案 0 :(得分:1)

问题出在这条线上。

recyclerView = getActivity().findViewById(R.id.recyclerview1);

用此

替换该行
recyclerView = v.findViewById(R.id.recyclerview1);

您应该从片段布局中获取您的回收者视图,该布局在方法的开头被初始化,而不是来自活动