我想使用recyclerview,但是出现了空指针。
所以我搜索了这个问题,他们说我必须像下面那样设置LinearLayoutManager,我做到了:
setLayoutManager(new LinearLayoutManager(context))
但是,它不起作用。
我的错误日志是:
java.lang.NullPointerException
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:1694)
at android.view.View.measure(View.java:16628)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16628)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:883)
at android.view.View.measure(View.java:16628)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16628)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16628)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16628)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16628)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2298)
at android.view.View.measure(View.java:16628)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5610)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:212)
at android.app.ActivityThread.main(ActivityThread.java:5137)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:718)
at dalvik.system.NativeStart.main(Native Method)
我的代码:
GridLayoutManager gridLayoutManager = new GridLayoutManager(IndexActivity.this , 2 );
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(gridLayoutManager);
recyclerView.setAdapter(mAdapter);
xml:
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:horizontalSpacing="@dimen/index_gridview_horizontal_spacing"
android:verticalSpacing="@dimen/index_gridview_vertical_spacing"
android:numColumns="2"
android:id="@+id/recyclerView" />
通过更改gradle解决了这个问题。
我的参考资料如下:beecanvas.com/yjcSh8Tp
答案 0 :(得分:1)
试试这样:
RecyclerView recyclerView = (RecyclerView) findViewById(
R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
recyclerView.setAdapter(new NumberedAdapter(30));
这里我使用的跨度数为2,你将有30个元素,你可以根据需要进行修改。希望这会对你有所帮助。
答案 1 :(得分:0)
我猜你使用了错误的GridLayoutManager
。请确保您拥有import android.support.v7.widget.GridLayoutManager
,然后:
//for Activity
recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
or //for Fragment
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
应该没问题。
[<强>更新强>]
您只能为LayoutManager
,RecyclerView
或LinearLayoutManager
或GridLayoutManager
设置一个StaggeredGridLayoutManager
,而不是两者。对于您的情况,只需设置GridLayoutManager
。
希望这有帮助!