Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recycle view - java.lang.NullPointerException

I am getting following exception while i'm try to display recycle view:

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

and my xml is this:

<?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>

and my java code is this:

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();
            }
        });
    }
}

i'm also try to set the layout_height of the recycle view as match_parent and wrap_contant but still i get the same error

like image 360
gil cohen Avatar asked May 17 '15 08:05

gil cohen


2 Answers

Here is how I finally solved this problem.

I removed android:scrollbars="vertical" from RecyclerView declaration:

<android.support.v7.widget.RecyclerView
    android:id="@+id/task_recycler_view"
    android:scrollbars="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
like image 161
windflyer Avatar answered Nov 08 '22 19:11

windflyer


Remove this line from the xml:

android:scrollbars="vertical"

like image 27
FlipNovid Avatar answered Nov 08 '22 20:11

FlipNovid