嵌套的RecyclerView会抛出NPE

时间:2015-03-20 21:48:54

标签: android nullpointerexception android-recyclerview

当我在Android应用程序中创建嵌套的RecyclerView时,它会抛出NPE。

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


    <android.support.v7.widget.RecyclerView
        android:id="@+id/groupList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</RelativeLayout>

group_row.xml

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

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/group"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="#FF0000"/>
        <android.support.v7.widget.RecyclerView
            android:id="@+id/shortcutList"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </FrameLayout>
</LinearLayout>

这是例外

java.lang.NullPointerException
        at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:1764)
        at android.view.View.measure(View.java:16538)

我需要帮助。为什么它是NullPointerException?我能解决这个问题吗?

更新

MainActivity.java

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

    RecyclerView groupList = (RecyclerView) findViewById(R.id.groupList);
    groupList.setAdapter(new GroupAdapter());
}

GroupAdapter.java

public class GroupAdapter extends RecyclerView.Adapter<GroupAdapter.ViewHolder> {


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.group_row, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {

    }

    @Override
    public int getItemCount() {
        return 0;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        public ViewHolder(View itemView) {
            super(itemView);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

添加:

LinearLayoutManager llm1 = new LinearLayoutManager(this);
groupList.setLayoutManager(llm1);