我正在尝试使用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(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>
我在这里做错了什么?
答案 0 :(得分:4)
你在错误的地方寻找R.id.recycler_view
。您需要从充气视图(contact_fragment
)访问它,而不是您的活动。所以在Contact_List_Fragment.java
更改行:
recyclerView = (RecyclerView) getActivity().findViewById(R.id.recycler_view)
要:
recyclerView = (RecyclerView) contact_fragment.findViewById(R.id.recycler_view)