我正试图使用cardview显示RecycerView的联系人,但我得到了这个错误:
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;
}
}
<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>
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);
}
}
}
<?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>
您在错误的位置查找r.id.recycer_view
。您需要从膨胀的视图(contact_fragment
)访问它,而不是从活动中访问它。因此,在contact_list_fragment.java
中更改以下行:
recyclerView = (RecyclerView) getActivity().findViewById(R.id.recycler_view)
致:
recyclerView = (RecyclerView) contact_fragment.findViewById(R.id.recycler_view)
问题内容: 我不知道我的代码有什么问题 这是我的片段课 这是我的SessionManager.java 运行项目时出现的错误: java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“ void com.example.gandi.symanlub.SessionManager.logoutUser()” 问题答案: 您收到错误消息是因为在此行您正在调用: 会
因此,我试图从firebase向recyclerview显示图像,但它抛出了一个错误。错误提示E/nite competitive:[qarth _ debug:]get patch store::createDisableExceptionQarthFile方法失败。E/AndroidRuntime:致命异常:主进程:com . example . fortnite competitive,PI
我试图在android中设置回收器视图,但适配器类给了我一个错误,如标题中所述。我搜索并找到了这个。这里的答案是命名不匹配。我检查了我的名字,但仍然有错误。 这是适配器: 以下是回收器将膨胀的xml文件(项目3): 以下是日志: ImageUploadInfo: 请告诉我哪里出错了。
我是Android应用程序开发的新手。我一直试图用Firebase电子邮件和密码auth构建一个应用程序,并试图在注册过程中将手机的保存在Firebase数据库中。
我正在尝试使用适配器运行我的应用程序,但在执行此操作时,我遇到了以下错误: ReviewActivity StandupPlayerMain.java
我正在尝试使用MaterialNavigationDrawer库。维基上说: MaterialNavigationDrawer是ActionBarActivity,但有一些重要的更改: null 尝试对Android.support.v7.widget.recyclerview.onmeasure(Recyclerview.java:1764)上的空对象引用调用虚拟方法“void android.