简体   繁体   English

错误:“空对象引用上的'void RecyclerView.setLayoutManager(RecyclerView $ LayoutManager)'”

[英]Error : “ 'void RecyclerView.setLayoutManager(RecyclerView$LayoutManager)' on a null object reference”

I am trying to show contacts with recyclerview using cardview and i am getting this error : 我正在尝试使用cardview显示与recyclerview的联系,并且出现此错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView $ LayoutManager)'

Fragment Parent is BottomNavigationViewTab - where I am trying to show contacts. 片段父是BottomNavigationViewTab我试图在其中显示联系人。

Contact_List_Fragment.java 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 (Layout File Code) 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 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 (CardView Layout) recycler_item_layout.xml(CardView布局)

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

What am i doing wrong here? 我在这里做错了什么?

You're looking for R.id.recycler_view in the wrong place. 您在错误的位置寻找R.id.recycler_view You need to access it from your inflated view ( contact_fragment ), not your Activity. 您需要从展开视图( contact_fragment ),而不是从Activity中访问它。 So in Contact_List_Fragment.java change the line: 因此,在Contact_List_Fragment.java更改以下行:

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

To: 至:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager) 在 null object 参考 - void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager) on a null object reference NPE在RecyclerView.setLayoutManager上 - NPE on RecyclerView.setLayoutManager RecyclerView setLayoutManager 的 logcat 错误 - Error in logcat for RecyclerView setLayoutManager 尝试调用虚方法 &#39;void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager - Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager 错误:在空对象引用上,SavedInstanceState旋转recyclerview - Error : on a null object reference, SavedInstanceState rotation recyclerview 空对象引用上的 RecyclerView - RecyclerView on a null object reference Android Studio 错误中的 Recyclerview - “null object 参考” - Recyclerview in Android Studio Error - "null object reference" 遇到在 null object 参考上调用虚拟方法 'void androidx.recyclerview.widget.RecyclerView.setHasFixedSize(boolean)' 的尝试 - Encountering a Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference RecyclerView null object 重新填充时参考错误 - RecyclerView null object reference error when repopulate LayoutManager上的空对象引用 - Null object reference on LayoutManager
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM