android–RecyclerView不能在onBindViewHolder()中使用setText()

android–RecyclerView不能在onBindViewHolder()中使用setText(),第1张

概述在我的应用程序中,我试图在recyclerView中使用两个布局一切正常,直到我尝试应用程序并崩溃说:尝试在空对象引用上调用虚方法’voidandroid.widget.TextView.setText(int)’.错误是在这一行,每次我尝试在第一个布局中setText:groupsViewHolder.fTeamName.setText(R.string.russia)

在我的应用程序中,我试图在recyclerVIEw中使用两个布局一切正常,直到我尝试应用程序并崩溃说:
尝试在空对象引用上调用虚方法’voID android.Widget.TextVIEw.setText(int)’.
错误是在这一行,每次我尝试在第一个布局中setText:

groupsVIEwHolder.fTeamname.setText(R.string.russia);

第二个布局工作正常,但第一个布局没有.
我想要的是解决错误,如果有比使用开关多次更好的方法.
谢谢您的帮助.
这是我的代码:

MatchesAdapter.java:

public class MatchesAdapter extends RecyclerVIEw.Adapter<RecyclerVIEw.VIEwHolder> {final private ListItemClickListener mOnClickListener;private Context context;private int mNumberItems;public MatchesAdapter(Context context, int numberOfItems, ListItemClickListener Listener) {    mNumberItems = numberOfItems;    mOnClickListener = Listener;    this.context = context;}@OverrIDepublic int getItemVIEwType(int position) {    return position;}@NonNull@OverrIDepublic RecyclerVIEw.VIEwHolder onCreateVIEwHolder(@NonNull VIEwGroup vIEwGroup, int vIEwType) {    Context context = vIEwGroup.getContext();    LayoutInflater inflater = LayoutInflater.from(context);    if (vIEwType >= 0 && vIEwType <= 47) {        int layoutIDForListItem = R.layout.matches_groups_item;        VIEw vIEw = inflater.inflate(layoutIDForListItem, vIEwGroup, false);        Log.e("QQ","Groups");        return new GroupsVIEwHolder(vIEw);    } else {        int layoutIDForListItem = R.layout.matches_rounds_item;        VIEw vIEw = inflater.inflate(layoutIDForListItem, vIEwGroup, false);        Log.e("QQ","Rounds");        return new RoundsVIEwHolder(vIEw);    }}@OverrIDepublic voID onBindVIEwHolder(@NonNull RecyclerVIEw.VIEwHolder mainHolder, int position) { mainHolder;        String[] array = context.getResources().getStringArray(R.array.times);        final int itemType = getItemVIEwType(position);        if (itemType >= 0 && itemType <= 47) {            GroupsVIEwHolder groupsVIEwHolder = (GroupsVIEwHolder) mainHolder;            groupsVIEwHolder.bind(position);            switch (position) {                case 0:                    groupsVIEwHolder.ListItemDate.setText(array[position]);                    groupsVIEwHolder.fTeamname.setText(R.string.russia);                    groupsVIEwHolder.sTeamname.setText(R.string.saudi_arabia);                    break;                case 1:                   groupsVIEwHolder.ListItemDate.setText(array[position]);                    groupsVIEwHolder.fTeamname.setText(R.string.egypt);                                        groupsVIEwHolder.sTeamname.setText(R.string.uruguay);                    break;//.... many cases until case 47 then} else {            RoundsVIEwHolder roundsVIEwHolder = (RoundsVIEwHolder) mainHolder;            roundsVIEwHolder.bind(position);            switch (position) {                 case 48:                roundsVIEwHolder.ListItemTitle.setText(R.string.round16);                roundsVIEwHolder.ListItemDate.setText(array[position]);                roundsVIEwHolder.fTeamname.setText(R.string.group_a_winner);                roundsVIEwHolder.sTeamname.setText(R.string.group_b_runner_up);                break;            case 49:                roundsVIEwHolder.ListItemTitle.setText(R.string.round16);                roundsVIEwHolder.ListItemDate.setText(array[position]);                roundsVIEwHolder.fTeamname.setText(R.string.group_c_winner);                roundsVIEwHolder.sTeamname.setText(R.string.group_d_runner_up);                break;// until the end of cases then    @OverrIDepublic int getItemCount() {    return mNumberItems;}public interface ListItemClickListener {    voID onListItemClick(int clickedItemIndex);}class GroupsVIEwHolder extends RecyclerVIEw.VIEwHolder        implements OnClickListener {TextVIEw ListItemDate, ListItemmatchNumber, fTeamname, sTeamname;VIEw fTeamVIEw;VIEw sTeamVIEw;public GroupsVIEwHolder(VIEw itemVIEw) {    super(itemVIEw);ListItemDate = itemVIEw.findVIEwByID(R.ID.match_date);ListItemmatchNumber = itemVIEw.findVIEwByID(R.ID.match_number);fTeamVIEw = itemVIEw.findVIEwByID(R.ID.groups_match_t1);fTeamname = fTeamVIEw.findVIEwByID(R.ID.match_team_name_for_groups);sTeamVIEw = itemVIEw.findVIEwByID(R.ID.groups_match_t2);sTeamname = sTeamVIEw.findVIEwByID(R.ID.match_team_name_for_groups);itemVIEw.setonClickListener(this);}voID bind(int ListIndex) {    ListItemmatchNumber.setText(String.valueOf(ListIndex + 1));}@OverrIDepublic voID onClick(VIEw v) {    int clickedposition = getAdapterposition();    mOnClickListener.onListItemClick(clickedposition);}}class RoundsVIEwHolder extends RecyclerVIEw.VIEwHolder        implements OnClickListener {    TextVIEw ListItemTitle, ListItemDate, ListItemmatchNumber, fTeamname, sTeamname;    VIEw fTeamVIEw;    VIEw sTeamVIEw;public RoundsVIEwHolder(VIEw itemVIEw) {    super(itemVIEw);ListItemTitle = itemVIEw.findVIEwByID(R.ID.round_card_Title);ListItemDate = itemVIEw.findVIEwByID(R.ID.round_match_date);ListItemmatchNumber = itemVIEw.findVIEwByID(R.ID.round_match_number);fTeamVIEw = itemVIEw.findVIEwByID(R.ID.round_match_t1);fTeamname = fTeamVIEw.findVIEwByID(R.ID.match_team_name);sTeamVIEw = itemVIEw.findVIEwByID(R.ID.round_match_t2);sTeamname = sTeamVIEw.findVIEwByID(R.ID.match_team_name);itemVIEw.setonClickListener(this);}voID bind(int ListIndex) {    ListItemmatchNumber.setText(String.valueOf(ListIndex + 1));}@OverrIDepublic voID onClick(VIEw v) {    int clickedposition = getAdapterposition();    mOnClickListener.onListItemClick(clickedposition);}}}

matches_groups_item.xml:

<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"xmlns:card_vIEw="http://schemas.androID.com/apk/res-auto"androID:layout_wIDth="match_parent"androID:layout_height="wrap_content"><androID.support.v7.Widget.CardVIEw    androID:ID="@+ID/mathes_card"    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content"    androID:layout_margin="3dp"    card_vIEw:cardCornerRadius="4dp">    <TextVIEw        androID:ID="@+ID/match_number"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:layout_gravity="center_horizontal"        androID:layout_margintop="5dp"        androID:textcolor="@color/red"        androID:textSize="18sp" />    <TextVIEw        androID:ID="@+ID/match_date"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:layout_gravity="center_horizontal"        androID:layout_margintop="30dp"        androID:text="@string/select_winner" />    <linearLayout        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent"        androID:layout_below="@+ID/tv_informer"        androID:layout_margintop="45dp"        androID:baselineAligned="false"        androID:orIEntation="horizontal">        <include            androID:ID="@+ID/groups_match_t1"            layout="@layout/team_no_buttons"            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:layout_marginleft="20dp"            androID:layout_marginStart="20dp" />        <include            androID:ID="@+ID/groups_match_t2"            layout="@layout/team_no_buttons"            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content" />    </linearLayout></androID.support.v7.Widget.CardVIEw></relativeLayout>

team_no_button.xml:

<TextVIEw xmlns:androID="http://schemas.androID.com/apk/res/androID"androID:ID="@+ID/match_team_name_for_groups"androID:layout_wIDth="match_parent"androID:layout_height="wrap_content"androID:gravity="center_horizontal" />

matches_rounds_items.xml:

<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"xmlns:card_vIEw="http://schemas.androID.com/apk/res-auto"androID:layout_wIDth="match_parent"androID:layout_height="wrap_content"><androID.support.v7.Widget.CardVIEw    androID:ID="@+ID/mathes_card"    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content"    androID:layout_margin="3dp"    card_vIEw:cardCornerRadius="4dp">    <linearLayout        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent"        androID:orIEntation="vertical">        <linearLayout            androID:layout_wIDth="match_parent"            androID:layout_height="wrap_content"            androID:orIEntation="vertical">            <TextVIEw                androID:ID="@+ID/round_card_Title"                androID:layout_wIDth="wrap_content"                androID:layout_height="wrap_content"                androID:layout_gravity="center_horizontal"                androID:layout_margintop="5dp"                androID:textcolor="@color/red"                androID:textSize="18sp" />            <TextVIEw                androID:ID="@+ID/round_match_number"                androID:layout_wIDth="wrap_content"                androID:layout_height="wrap_content"                androID:layout_gravity="center_horizontal"                androID:layout_margintop="5dp"                androID:textcolor="@color/red"                androID:textSize="18sp" />            <TextVIEw                androID:ID="@+ID/round_match_date"                androID:layout_wIDth="wrap_content"                androID:layout_height="wrap_content"                androID:layout_gravity="center_horizontal"                androID:layout_margintop="5dp"                androID:text="@string/select_winner" />        </linearLayout>        <linearLayout            androID:layout_wIDth="match_parent"            androID:layout_height="match_parent"            androID:layout_margintop="4dp"            androID:baselineAligned="false"            androID:orIEntation="horizontal">            <include                androID:ID="@+ID/round_match_t1"                layout="@layout/team_no_buttons_shirts"                androID:layout_wIDth="wrap_content"                androID:layout_height="wrap_content"                androID:layout_marginleft="20dp"                androID:layout_marginStart="20dp" />            <TextVIEw                androID:layout_wIDth="0dp"                androID:layout_height="wrap_content"                androID:layout_gravity="center_vertical"                androID:layout_weight="2"                androID:gravity="center_horizontal"                androID:text="@string/vs" />            <include                androID:ID="@+ID/round_match_t2"                layout="@layout/team_no_buttons_shirts"                androID:layout_wIDth="wrap_content"                androID:layout_height="wrap_content"                androID:layout_marginEnd="20dp"                androID:layout_marginRight="20dp" />        </linearLayout>    </linearLayout></androID.support.v7.Widget.CardVIEw>

team_no_buttons_shirts.xml

<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"androID:layout_wIDth="wrap_content"androID:layout_height="wrap_content"androID:orIEntation="vertical"><TextVIEw    androID:ID="@+ID/match_team_name"    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content"    androID:gravity="center_horizontal"    androID:textSize="20sp" />

Logcat:

06-26 11:58:13.111 4407-4407/sy.aya.ayaworldcup2018 E/QQ: Groups06-26 11:58:13.114 4407-4407/sy.aya.ayaworldcup2018 D/AndroIDRuntime: Shutting down VM06-26 11:58:13.128 4407-4407/sy.aya.ayaworldcup2018 E/AndroIDRuntime: FATAL EXCEPTION: main    Process: sy.aya.ayaworldcup2018, PID: 4407    java.lang.NullPointerException: Attempt to invoke virtual method 'voID androID.Widget.TextVIEw.setText(int)' on a null object reference        at sy.aya.ayaworldcup2018.MatchesAdapter.onBindVIEwHolder(MatchesAdapter.java:72)        at androID.support.v7.Widget.RecyclerVIEw$Adapter.onBindVIEwHolder(RecyclerVIEw.java:6673)        at androID.support.v7.Widget.RecyclerVIEw$Adapter.bindVIEwHolder(RecyclerVIEw.java:6714)        at androID.support.v7.Widget.RecyclerVIEw$Recycler.tryBindVIEwHolderByDeadline(RecyclerVIEw.java:5647)        at androID.support.v7.Widget.RecyclerVIEw$Recycler.tryGetVIEwHolderForpositionByDeadline(RecyclerVIEw.java:5913)        at androID.support.v7.Widget.RecyclerVIEw$Recycler.getVIEwForposition(RecyclerVIEw.java:5752)        at androID.support.v7.Widget.RecyclerVIEw$Recycler.getVIEwForposition(RecyclerVIEw.java:5748)        at androID.support.v7.Widget.linearlayoutmanager$LayoutState.next(linearlayoutmanager.java:2232)        at androID.support.v7.Widget.linearlayoutmanager.layoutChunk(linearlayoutmanager.java:1559)        at androID.support.v7.Widget.linearlayoutmanager.fill(linearlayoutmanager.java:1519)        at androID.support.v7.Widget.linearlayoutmanager.onLayoutChildren(linearlayoutmanager.java:614)        at androID.support.v7.Widget.RecyclerVIEw.dispatchLayoutStep2(RecyclerVIEw.java:3812)        at androID.support.v7.Widget.RecyclerVIEw.dispatchLayout(RecyclerVIEw.java:3529)        at androID.support.v7.Widget.RecyclerVIEw.onLayout(RecyclerVIEw.java:4082)        at androID.vIEw.VIEw.layout(VIEw.java:19679)        at androID.vIEw.VIEwGroup.layout(VIEwGroup.java:6075)        at androID.Widget.FrameLayout.layoutChildren(FrameLayout.java:323)        at androID.Widget.FrameLayout.onLayout(FrameLayout.java:261)        at androID.vIEw.VIEw.layout(VIEw.java:19679)        at androID.vIEw.VIEwGroup.layout(VIEwGroup.java:6075)        at androID.Widget.FrameLayout.layoutChildren(FrameLayout.java:323)        at androID.Widget.FrameLayout.onLayout(FrameLayout.java:261)        at androID.vIEw.VIEw.layout(VIEw.java:19679)        at androID.vIEw.VIEwGroup.layout(VIEwGroup.java:6075)        at androID.support.v7.Widget.ActionbarOverlayLayout.onLayout(ActionbarOverlayLayout.java:443)        at androID.vIEw.VIEw.layout(VIEw.java:19679)        at androID.vIEw.VIEwGroup.layout(VIEwGroup.java:6075)        at androID.Widget.FrameLayout.layoutChildren(FrameLayout.java:323)        at androID.Widget.FrameLayout.onLayout(FrameLayout.java:261)        at androID.vIEw.VIEw.layout(VIEw.java:19679)        at androID.vIEw.VIEwGroup.layout(VIEwGroup.java:6075)        at androID.Widget.linearLayout.setChildFrame(linearLayout.java:1791)        at androID.Widget.linearLayout.layoutVertical(linearLayout.java:1635)        at androID.Widget.linearLayout.onLayout(linearLayout.java:1544)        at androID.vIEw.VIEw.layout(VIEw.java:19679)        at androID.vIEw.VIEwGroup.layout(VIEwGroup.java:6075)        at androID.Widget.FrameLayout.layoutChildren(FrameLayout.java:323)        at androID.Widget.FrameLayout.onLayout(FrameLayout.java:261)        at com.androID.internal.policy.DecorVIEw.onLayout(DecorVIEw.java:761)        at androID.vIEw.VIEw.layout(VIEw.java:19679)        at androID.vIEw.VIEwGroup.layout(VIEwGroup.java:6075)        at androID.vIEw.VIEwRootImpl.performlayout(VIEwRootImpl.java:2511)        at androID.vIEw.VIEwRootImpl.performTraversals(VIEwRootImpl.java:2227)        at androID.vIEw.VIEwRootImpl.doTraversal(VIEwRootImpl.java:1407)        at androID.vIEw.VIEwRootImpl$TraversalRunnable.run(VIEwRootImpl.java:6783)        at androID.vIEw.Choreographer$CallbackRecord.run(Choreographer.java:911)        at androID.vIEw.Choreographer.doCallbacks(Choreographer.java:723)        at androID.vIEw.Choreographer.doFrame(Choreographer.java:658)        at androID.vIEw.Choreographer$FramedisplayEventReceiver.run(Choreographer.java:897)        at androID.os.Handler.handleCallback(Handler.java:790)        at androID.os.Handler.dispatchMessage(Handler.java:99)        at androID.os.Looper.loop(Looper.java:164)        at androID.app.ActivityThread.main(ActivityThread.java:6499)        at java.lang.reflect.Method.invoke(Native Method)        at com.androID.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440)06-26 11:58:13.129 4407-4407/sy.aya.ayaworldcup2018 E/AndroIDRuntime:     at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:807)        at de.robv.androID.xposed.XposedBrIDge.main(XposedBrIDge.java:108)

解决方法:

正如@Cheticamp所说,你在< include />中指定的ID(groups_match_t1) tag将覆盖team_no_buttons.xml中的ID(match_team_name_for_groups),因此您只能找到ID为group_match_t1的视图,因此这一行:

fTeamVIEw = itemVIEw.findVIEwByID(R.ID.groups_match_t1);

会找到一个视图,它实际上是TextVIEw本身,然后:

fTeamname = fTeamVIEw.findVIEwByID(R.ID.match_team_name_for_groups);

将返回null,因为fTeamVIEw中没有TextVIEw.所以你可以使用:

fTeamname = itemVIEw.findVIEwByID(R.ID.groups_match_t1);

获取TextVIEw,然后删除fTeamVIEw.

并且因为team_no_buttons_shirts.xml有一个额外的linearLayout,所以ID match_team_name不会被覆盖,所以:

fTeamVIEw = itemVIEw.findVIEwByID(R.ID.round_match_t1);

将返回linearLayout,和

fTeamname = fTeamVIEw.findVIEwByID(R.ID.match_team_name);

将找到正确的TextVIEw,它是linearLayout的子项.

此外,Tor norbye wrote:

The <include> tag is not a real vIEw, so findByVIEw will not find it. The @ID attribute (and any other attributes you’ve set on the include tag) gets applIEd on the root tag of the included layout instead. So your activity.getVIEw(R.ID.included1) should in fact be the <TextVIEw> itself.

总结

以上是内存溢出为你收集整理的android – RecyclerView不能在onBindViewHolder()中使用setText()全部内容,希望文章能够帮你解决android – RecyclerView不能在onBindViewHolder()中使用setText()所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1108985.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存