gpt4 book ai didi

java - RecyclerView 空指针异常

转载 作者:行者123 更新时间:2023-11-29 07:40:46 24 4
gpt4 key购买 nike

此刻我正在玩 RecyclerView。我一直遇到一些 NullPointer 异常,但我找不到原因。

java.lang.RuntimeException: Unable to start activity ComponentInfo{de.lueth.erik.birthdays/de.lueth.erik.birthdays.TodaysBirthdays.TodaysBirthdaysActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2331)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5349)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
at de.lueth.erik.birthdays.TodaysBirthdays.TodaysBirthdaysActivity.onCreate(TodaysBirthdaysActivity.java:23)
at android.app.Activity.performCreate(Activity.java:6020)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2284)...

这是我的 TodaysBirthdaysActivity.classonCreate 部分看起来像 Google 在其 RecyclerView 教程中展示的部分。

public class TodaysBirthdaysActivity extends Activity{
private RecyclerView recList;
private RecyclerView.Adapter adp;
private RecyclerView.LayoutManager llm;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_birthday_list);
recList = (RecyclerView) findViewById(R.id.cardList);
recList.setHasFixedSize(true);
llm = new LinearLayoutManager(this);
recList.setLayoutManager(llm);

adp = new Adapter(createList(30));
recList.setAdapter(adp);
}

private List<ContactInfo> createList(int size) {

List<ContactInfo> result = new ArrayList<ContactInfo>();
for (int i=1; i <= size; i++) {
ContactInfo ci = new ContactInfo();
ci.name = ContactInfo.NAME_PREFIX + i;
ci.surname = ContactInfo.SURNAME_PREFIX + i;
ci.birthday = ContactInfo.Birthday_PREFIX + i ;

result.add(ci);

}

return result;
}}

这是我的 activity_birthday_list.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=".TodaysBirthdaysActivity">

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

这是我的 Adapter.class

public class Adapter extends RecyclerView.Adapter<ContactViewHolder> {

private List<ContactInfo> contactList;

public Adapter(List<ContactInfo> contactList) {
this.contactList = contactList;
}

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

@Override
public void onBindViewHolder(ContactViewHolder contactViewHolder, int i) {
ContactInfo ci = contactList.get(i);
contactViewHolder.vName.setText(ci.name);
contactViewHolder.vSurname.setText(ci.surname);
contactViewHolder.vBirthday.setText(ci.birthday);
contactViewHolder.vTitle.setText(ci.name + " " + ci.surname);
}

@Override
public ContactViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.
from(viewGroup.getContext()).
inflate(R.layout.contact_card, viewGroup, false);

return new ContactViewHolder(itemView);
}}

这里是我的 ContactInfo.java

public class ContactInfo {
protected String name;
protected String surname;
protected String birthday;
protected static final String NAME_PREFIX = "Name_";
protected static final String SURNAME_PREFIX = "Surname_";
protected static final String Birthday_PREFIX = "Birthday_";}

这里是我的 ContentViewHolder.java

public class ContactViewHolder extends RecyclerView.ViewHolder {
protected TextView vName;
protected TextView vSurname;
protected TextView vBirthday;
protected TextView vTitle;

public ContactViewHolder(View v) {
super(v);
vName = (TextView) v.findViewById(R.id.txtName);
vSurname = (TextView) v.findViewById(R.id.txtSurname);
vBirthday = (TextView) v.findViewById(R.id.txtBirthday);
vTitle = (TextView) v.findViewById(R.id.title);
}}

和卡片 contact_card.xml 的 xml

<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
card_view:cardCornerRadius="4dp"
android:layout_margin="5dp">

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

<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="20dp"
android:background="@color/cardview_dark_background"
android:text="contact det"
android:gravity="center_vertical"
android:textColor="@android:color/white"
android:textSize="14dp"/>

<TextView
android:id="@+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:gravity="center_vertical"
android:textSize="10dp"
android:layout_below="@id/title"
android:layout_marginTop="10dp"
android:layout_marginLeft="5dp"/>

<TextView
android:id="@+id/txtSurname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Surname"
android:gravity="center_vertical"
android:textSize="10dp"
android:layout_below="@id/txtName"
android:layout_marginTop="10dp"
android:layout_marginLeft="5dp"/>

<TextView
android:id="@+id/txtBirthday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Birthday"
android:textSize="10dp"
android:layout_marginTop="10dp"
android:layout_alignParentRight="true"
android:layout_marginRight="150dp"
android:layout_alignBaseline="@id/txtName"/>

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

最佳答案

这一切都在你的 logcat 输出中:

at de.lueth.erik.birthdays.TodaysBirthdays.TodaysBirthdaysActivity.onCreate(TodaysBirthdaysActivity.java:23)

那行 TodaysBirthdaysActivity.java:23 给你 NPE。这意味着,您的变量 recListnull。为什么?很可能是因为在布局 R.layout.activity_birthday_list 中没有 ID 为 R.id.cardList 的对象。

关于java - RecyclerView 空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30485913/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com