在onOptionsItemSelected中创建recyclerview,而不是在活动

时间:2015-08-10 19:19:56

标签: android android-recyclerview

我有一个在onCreate中找不到的recyclelerview,但在onOptionsItemSelected中找不到。原因如下:当我点击选项菜单中的某个项目时,我会显示一个底部工作表,请参阅:(google specslibrary)。该库采用xml-layout,这是我的recyclerview所在的位置。问题是,当我将布局管理器附加到recyclerview时,我得到了这个例外:

  

java.lang.NullPointerException:尝试调用虚方法   '布尔   android.support.v7.widget.RecyclerView $ LayoutManager.canScrollVertically()'   在null对象引用上               在android.support.v7.widget.RecyclerView.computeVerticalScrollRange(RecyclerView.java:1545)               在android.view.View.onDrawScrollBars(View.java:12943)               在android.view.View.draw(View.java:15237)               在android.support.v7.widget.RecyclerView.draw(RecyclerView.java:3037)               在android.view.View.updateDisplayListIfDirty(View.java:14167)               在android.view.View.getDisplayList(View.java:14189)               在android.view.View.draw(View.java:14959)               在android.view.ViewGroup.drawChild(ViewGroup.java:3405)               在android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)               在android.view.View.draw(View.java:15234)               在android.view.View.updateDisplayListIfDirty(View.java:14167)               在android.view.View.getDisplayList(View.java:14189)               在android.view.View.draw(View.java:14959)               在android.view.ViewGroup.drawChild(ViewGroup.java:3405)               在android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)               在android.view.View.updateDisplayListIfDirty(View.java:14162)               在android.view.View.getDisplayList(View.java:14189)               在android.view.View.draw(View.java:14959)               在android.view.ViewGroup.drawChild(ViewGroup.java:3405)               在android.support.v4.widget.DrawerLayout.drawChild(DrawerLayout.java:1089)               在android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)               在android.view.View.updateDisplayListIfDirty(View.java:14162)               在android.view.View.getDisplayList(View.java:14189)               在android.view.View.draw(View.java:14959)               在android.view.ViewGroup.drawChild(ViewGroup.java:3405)               在android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)               在android.view.View.updateDisplayListIfDirty(View.java:14162)               在android.view.View.getDisplayList(View.java:14189)               在android.view.View.draw(View.java:14959)               在android.view.ViewGroup.drawChild(ViewGroup.java:3405)               在android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)               在android.view.View.updateDisplayListIfDirty(View.java:14162)               在android.view.View.getDisplayList(View.java:14189)               在android.view.View.draw(View.java:14959)               在android.view.ViewGroup.drawChild(ViewGroup.java:3405)               在android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)               在android.view.View.updateDisplayListIfDirty(View.java:14162)               在android.view.View.getDisplayList(View.java:14189)               在android.view.View.draw(View.java:14959)               在android.view.ViewGroup.drawChild(ViewGroup.java:3405)               在android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)               在android.view.View.updateDisplayListIfDirty(View.java:14162)               在android.view.View.getDisplayList(View.java:14189)               在android.view.View.draw(View.java:14959)               在android.view.ViewGroup.drawChild(ViewGroup.java:3405)               在android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)               在android.view.View.draw(View.java:15234)               在android.widget.FrameLayout.draw(FrameLayout.java:598)               在com.android.internal.policy.impl.PhoneWindow $ DecorView.draw(PhoneWindow.java:2650)               在android.view.View.updateDisplayListIfDirty(View.java:14167)               在android.view.View.getDisplayList(View.java:14189)               在android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:273)               在android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:279)               在android.view.ThreadedRenderer.draw(ThreadedRenderer.java:318)               在android.view.ViewRootImpl.draw(ViewRootImpl.java:2530)               在android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2352)               在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1982)               在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)               在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:5885)               在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:767)               在android.view.Choreographer.doCallbacks(Choreographer.java:580)               在android.view.Choreographer.doFrame(Choreographer.java:550)               在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreo

所以我的问题是:除了onCreate of activity或onCreateView of fragment之外,如何在其他任何地方创建一个recyclerview?

非常感谢任何帮助。感谢。

以下是我创建recyclerview的过程:首先我实例化底部工作表。见下面的代码

private BottomSheetLayout bottomSheet;

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ...

        bottomSheet = (BottomSheetLayout) findViewById(R.id.bottomsheet);
}

在onOptionsItemSelected中,我创建了recyclerview。见下面的代码

List<BottomSheetData> mCategoryList = new ArrayList<>();

BottomSheetAdapter bottomAdapter = new BottomSheetAdapter(mCategoryList); //Recyclerview adapter

RecyclerView mRecyclerView = (RecyclerView) bottomSheet.findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); //error caused here 
mRecyclerView.setAdapter(bottomAdapter);

这是我的底部工作表布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FAF9F7">

    <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:cacheColorHint="@android:color/transparent"
        android:clipToPadding="false"
        android:scrollbars="vertical"
        />

</LinearLayout>

1 个答案:

答案 0 :(得分:2)

//error caused here

不直接。如果是,你会在堆栈跟踪中看到它。

现在,您的行号与the RecyclerView source codemaster分支不匹配,因此请确保您使用最新的recyclerview-v7(22.2.1),以防万一正在绊倒一个RecyclerView错误。

实际上,is (or was) a bug with RecyclerView模仿了您的堆栈跟踪,与您拥有android:scrollbars属性相关联。该错误与IDE呈现代码有关; this bug本身就是RecyclerView。因此,如果移至22.2.1时问题仍然存在,请尝试删除android:scrollbars,看看是否有帮助。请注意,如果它在22.2.1中没有修复,我怀疑它是否会被修复,因为Google拒绝了第二个错误报告。