回收视图 - java.lang.NullPointerException

时间:2015-05-17 08:38:28

标签: android nullpointerexception android-recyclerview

我在尝试显示回收视图时遇到异常:

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设置为match_parent和wrap_contant,但我仍然得到相同的错误

5 个答案:

答案 0 :(得分:16)

以下是我最终解决这个问题的方法。

我从RecyclerView声明中删除了android:scrollbars="vertical"

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

答案 1 :(得分:9)

从xml:

中删除此行
  

机器人:滚动条=&#34;垂直&#34;

答案 2 :(得分:3)

我认为你必须在java代码链接中初始化RecyclerView

//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);

答案 3 :(得分:2)

您是否在任何未在Java控制器(活动/片段)中处理过的布局文件中声明了recyclelerView?

我在两个片段中声明了两个回收站布局,我在我的java代码中处理第一个回收器,没有处理第二个回收器。删除fragment2-layout-file中的recyclerView声明后,它解决了空指针问题。

答案 4 :(得分:0)

替换

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

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