【问题标题】:Error : " 'void RecyclerView.setLayoutManager(RecyclerView$LayoutManager)' on a null object reference"错误:“空对象引用上的'void RecyclerView.setLayoutManager(RecyclerView$LayoutManager)'”
【发布时间】:2018-07-05 20:44:44
【问题描述】:

我正在尝试使用 cardview 显示带有 recyclerview 的联系人,但出现此错误:

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)”

Fragment Parent 是 BottomNavigationViewTab - 我正在尝试显示联系人。

Contact_List_Fragment.java

public class Contact_List_Fragment extends android.support.v4.app.Fragment {

    RecyclerView recyclerView;
    ArrayList<String> contact_names = new ArrayList<>(), mobile_numbers = new ArrayList<>();

    public Contact_List_Fragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View contact_fragment =  inflater.inflate(R.layout.contact_list_fragment, container, false);

        String[] PROJECTION_MAIN = new String[]{
                ContactsContract.Contacts._ID,
                ContactsContract.CommonDataKinds.Phone.NUMBER,
                ContactsContract.Contacts.DISPLAY_NAME
        };

        Cursor cursor = getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                PROJECTION_MAIN, null, null, null);

        while (cursor.moveToNext()) {
            String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            String phone = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            contact_names.add(name);
            mobile_numbers.add(phone);
        }

        recyclerView = (RecyclerView) getActivity().findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(new RecyclerAdapter(contact_names, mobile_numbers));

        return contact_fragment;
    }

}

contact_list_fragment.xml(布局文件代码)

<FrameLayout 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:id="@+id/contact_list_container"
    android:theme="@style/ThemeOverlay.AppCompat.Dark"
    tools:context="vwc.com.stayconnected.Contact_List_Fragment">

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

</FrameLayout>

RecyclerAdapter.java

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.RecyclerViewHolder> {

    ArrayList<String> names, contact_nos;

    public RecyclerAdapter(ArrayList<String> Names, ArrayList<String> Contact_Nos)
    {
        this.names = Names;
        this.contact_nos = Contact_Nos;
    }

    @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View view = layoutInflater.inflate(R.layout.recycler_item_layout, parent, false);
        return new RecyclerViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerViewHolder holder, int position) {
        String cname = names.get(position);
        String cnumber = contact_nos.get(position);
        holder.textView.setText(cname);
        holder.textView2.setText(cnumber);
    }

    @Override
    public int getItemCount() {
        return names.size();
    }

    public class RecyclerViewHolder extends RecyclerView.ViewHolder{

        TextView textView, textView2;
        CardView cardView;

        public RecyclerViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.contact_name);
            textView2 = (TextView) itemView.findViewById(R.id.contact_number);
            cardView = (CardView) itemView.findViewById(R.id.card_view);
        }
    }
}

recycler_item_layout.xml(卡片视图布局)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:background="@drawable/rounded_corners"
    android:backgroundTint="@color/white"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:padding="10dp">

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:background="@drawable/rounded_corners"
        android:layout_height="60dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:id="@+id/contact_image"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_centerVertical="true"
                android:layout_marginStart="11dp"
                android:src="@drawable/contact_person" />

            <TextView
                android:layout_marginTop="5sp"
                android:textSize="20sp"
                android:id="@+id/contact_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="25dp"
                android:layout_toEndOf="@+id/contact_image"
                android:text="@string/contact_name" />

            <TextView
                android:layout_marginTop="3sp"
                android:layout_marginStart="25dp"
                android:textSize="15sp"
                android:layout_below="@id/contact_name"
                android:id="@+id/contact_number"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toEndOf="@+id/contact_image"
                android:text="@string/contact_number" />

        </RelativeLayout>

    </android.support.v7.widget.CardView>

</LinearLayout>

我在这里做错了什么?

【问题讨论】:

  • recyclerView = (RecyclerView) getActivity().findViewById(R.id.recycler_view); 我认为应该是:recyclerView = (RecyclerView) contact_fragment.findViewById(R.id.recycler_view);

标签: java android xml android-recyclerview android-cardview


【解决方案1】:

您在错误的地方寻找R.id.recycler_view。您需要从膨胀视图 (contact_fragment) 访问它,而不是您的 Activity。所以在Contact_List_Fragment.java改行:

recyclerView = (RecyclerView) getActivity().findViewById(R.id.recycler_view)

收件人:

recyclerView = (RecyclerView) contact_fragment.findViewById(R.id.recycler_view)

【讨论】:

  • 现在我有空白卡片视图,为什么?
  • @KeyurSureliya 那将是一个完全不同的问题。你确定你的onBindViewHolder 方法工作正常吗?
  • 是的,只是 textviews 上没有文字。
  • 那么肯定是数据绑定问题。在您的onBindViewHolder 中添加一些日志记录,以确保您从cnamecnumber 中提取有效值
猜你喜欢
  • 2021-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-03
  • 1970-01-01
  • 2022-06-15
  • 1970-01-01
  • 2020-06-12
相关资源
最近更新 更多