Recycle view - java.lang.NullPointerException

Я получаю следующее исключение, пока я пытаюсь отобразить представление в виде корзины:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.support.v7.widget.RecyclerView$LayoutManager.canScrollVertically()' on a null object reference
at android.support.v7.widget.RecyclerView.computeVerticalScrollRange(RecyclerView.java:1216)
    at android.view.View.onDrawScrollBars(View.java:13900)
    at android.view.View.draw(View.java:16275)
    at android.support.v7.widget.RecyclerView.draw(RecyclerView.java:2440)
    at android.view.View.updateDisplayListIfDirty(View.java:15192)
    at android.view.View.getDisplayList(View.java:15215)
    at android.view.View.draw(View.java:15990)
    at android.view.ViewGroup.drawChild(ViewGroup.java:3708)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3502)
    at android.view.View.draw(View.java:16272)
    at android.view.View.updateDisplayListIfDirty(View.java:15192)
    at android.view.View.getDisplayList(View.java:15215)
    at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3692)
    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3671)
    at android.view.View.updateDisplayListIfDirty(View.java:15152)
    at android.view.View.getDisplayList(View.java:15215)
    at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3692)
    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3671)
    at android.view.View.updateDisplayListIfDirty(View.java:15152)
    at android.view.View.getDisplayList(View.java:15215)
    at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:362)
    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3671)
    at android.view.View.updateDisplayListIfDirty(View.java:15152)
    at android.view.View.getDisplayList(View.java:15215)
    at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3692)
    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3671)
    at android.view.View.updateDisplayListIfDirty(View.java:15152)
    at android.view.View.getDisplayList(View.java:15215)
    at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3692)
    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3671)
    at android.view.View.updateDisplayListIfDirty(View.java:15152)
    at android.view.View.getDisplayList(View.java:15215)
    at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3692)
    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3671)
    at android.view.View.updateDisplayListIfDirty(View.java:15152)
    at android.view.View.getDisplayList(View.java:15215)
    at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3692)
    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3671)
    at android.view.View.updateDisplayListIfDirty(View.java:15152)
    at android.view.View.getDisplayList(View.java:15215)
    at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3692)
    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3671)
    at android.view.View.updateDisplayListIfDirty(View.java:15152)
    at android.view.View.getDisplayList(View.java:15215)
    at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3692)
    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3671)
    at android.view.View.updateDisplayListIfDirty(View.java:15152)
    at android.view.View.getDisplayList(View.java:15215)
    at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:307)
    at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:313)
    at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:352)
    at android.view.ViewRootImpl.draw(ViewRootImpl.java:2811)
    at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2634)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2222)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1230)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6726)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
    at android.view.Choreographer.doCallbacks(Choreographer.java:590)
    at android.view.Choreograph

и мой xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/global_screen_linear_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/lilAdvancedSearchParent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/gray_general"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:padding="10dp" >

        <LinearLayout
            android:id="@+id/lilAdvancedSearch"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="6"
            android:gravity="center_vertical" >

            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/arrow_white" />

            <TextView
                android:id="@+id/txvAvencedSearch"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginRight="10dp"
                android:layout_weight="4.5"
                android:text="@string/global_fragment_advenced_search"
                android:textColor="@color/white"
                android:textSize="@dimen/global_screen_layout_text_view_advenced_search"
                android:textStyle="bold" />
        </LinearLayout>

        <EditText
            android:id="@+id/edtSearch"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="9"
            android:drawableLeft="@drawable/navigation_search"
            android:ems="10"
            android:imeOptions="actionSearch"
            android:inputType="text"
            android:textColor="@color/white"
            android:textColorHint="@color/white"
            android:textSize="15sp" >

            <requestFocus />
        </EditText>
    </LinearLayout>

    <android.support.v7.widget.RecyclerView
                android:id="@+id/my_recycler_view"
                android:layout_width="match_parent"
                android:layout_height="800dp"
                android:scrollbars="vertical" >
            </android.support.v7.widget.RecyclerView>

</LinearLayout>

и мой код java:

public class TendersFragment extends BaseFragment{



    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.global_screen_layout, container, false);
        initUI(v);
        initListeners();
        setEditSearchHint(getString(R.string.tenders_fragment_search_tenders));
        setAdvancedSearchBackground(getResources().getColor(R.color.green_general));
        return v;
    }

    @Override
    protected void initUI(View view) {
        lsvTendersJobsMessages = (ListView) view.findViewById(R.id.lsvGlobal);

        mRecyclerView = (RecyclerView) view.findViewById(R.id.my_recycler_view);
        setAdapterForListview(lsvTendersJobsMessages);  

        edtSearch = (EditText) view.findViewById(R.id.edtSearch);
        txvAdvencedSearch = (TextView) view.findViewById(R.id.txvAvencedSearch);
        lilAdvancedSearch = (LinearLayout) view.findViewById(R.id.lilAdvancedSearch);       
        lilAdvancedSearchParent = (LinearLayout) view.findViewById(R.id.lilAdvancedSearchParent);


    }

    @Override
    protected void setAdapterForListview(ListView lsv) {
        mRecyclerView.setHasFixedSize(true);
        mLinearLayoutManager = new LinearLayoutManager(getActivity());
        mLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);      
        mRecyclerView.setLayoutManager(mLinearLayoutManager);

        ArrayList<ItemType> arlItems = new ArrayList<ItemType>();
        arlItems.add(ItemType.MAIN_ITEM);
        arlItems.add(ItemType.MAIN_ITEM);
        arlItems.add(ItemType.MAIN_ITEM);
        arlItems.add(ItemType.MAIN_ITEM);
        arlItems.add(ItemType.MAIN_ITEM);
        arlItems.add(ItemType.MAIN_ITEM);
        arlItems.add(ItemType.MAIN_ITEM);
        arlItems.add(ItemType.MAIN_ITEM);
        arlItems.add(ItemType.MAIN_ITEM);

        // specify an adapter (see also next example)
        mainItemAdapter = new MainItemAdapter(arlItems, getActivity());
        mRecyclerView.setAdapter(mainItemAdapter);
    }

    @Override
    protected void onServiceConnected() {
        // TODO Auto-generated method stub

    }


    protected void initListeners() {

        final AdvancedSearchFragment advancedSearchFragment = new AdvancedSearchFragment(); 
        Bundle bundle = new Bundle();       
        bundle.putInt(FragmentConst.BUNDLE_KEY_INT_WHICH_FRAGMENT,FragmentConst.TENDERS_ID);    
        advancedSearchFragment.setArguments(bundle);
        lilAdvancedSearch.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {                   
                   getActivity().getSupportFragmentManager().beginTransaction()
                    .add(android.R.id.content,advancedSearchFragment).addToBackStack(null)
                    .commit();
            }
        });
    }
}

Я также пытаюсь установить layout_height из представления recycle как match_parent и wrap_contant, но все же я получаю ту же ошибку

Ответ 1

Вот как я, наконец, решил эту проблему.

Я удалил android:scrollbars="vertical" из объявления RecyclerView:

<android.support.v7.widget.RecyclerView
    android:id="@+id/task_recycler_view"
    android:scrollbars="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

Ответ 2

Удалите эту строку из xml:

андроид: скроллбары = "вертикальный"

Ответ 3

Я думаю, что вы должны инициализировать RecyclerView в java-кодовом коде, это

//1. get a reference to recyclerView 
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);

//2. set layoutManger
 recyclerView.setLayoutManager(new LinearLayoutManager(this));

// 3. create an adapter 
MyAdapter mAdapter = new MyAdapter(itemsData);

// 4. set adapter
 recyclerView.setAdapter(mAdapter);

Ответ 4

Вы объявляете recyclerView в любом файле макета, который вы не рассматривали в java-контроллере (Activity/Fragment)?

Я объявляю два ресайклинга в двух фрагментах, я обрабатываю первый recycler в своих java-кодах, не рассматривая второй. После того как я удалю объявление recyclerView в файле layout2-layout-file, он решил проблему с нулевым указателем.

Ответ 5

Заменить

mLinearLayoutManager = new LinearLayoutManager(getActivity());
mLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);   
mRecyclerView.setLayoutManager(mLinearLayoutManager);

с

final LinearLayoutManager mLayoutManager= new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, true);            
mRecyclerView.setLayoutManager(mLayoutManager);