【发布时间】:2022-12-12 23:09:54
【问题描述】:
**我试图在 recyclerView 中实现一个 TextSwitcher,但每次我调试它时都会返回一条错误消息: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.view.View.getLayoutParams()' on a null object reference这是我的代码 XML
<TextSwitcher
android:id="@+id/textSwitcher_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textAlignment="textStart"
android:lineSpacingExtra="5dp"
android:textColor="@color/white"
android:textSize="@dimen/appbar_padding"
android:layout_gravity="start"
android:text=""/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textAlignment="textStart"
android:lineSpacingExtra="5dp"
android:textColor="@color/white"
android:textSize="@dimen/appbar_padding"
android:layout_gravity="start"
/>
</TextSwitcher>
这是我的 Java 代码:来自 RecyclerView Adapter 的 onBindViewHolder
@Override protected void onBindViewHolder(@NonNull ViewHolderCard holder, int position, @NonNull ModelBet model) { TextSwitcher textSwitcher=holder.getTextSwitcher(); //textSwitcher.setText(TEXT[mPosition]); textSwitcher.setFactory(new ViewSwitcher.ViewFactory() { @Override public View makeView() { TextView textView=new TextView(context); textView.setTextColor(context.getResources().getColor(R.color.white)); textView.setTextSize(16); return null; } }); }这是我的 ViewHolder
public class ViewHolderCard extends RecyclerView.ViewHolder { ... public TextSwitcher textSwitcher; public ViewHolderCard(@NonNull View itemView) { super(itemView); view=itemView; ... textSwitcher=(TextSwitcher)view.findViewById(R.id.textSwitcher_card); } @NonNull public TextSwitcher getTextSwitcher() { return textSwitcher; }虽然我已经初始化了我的 TextSwitcher。我真的不知道问题出在哪里
【问题讨论】:
标签: java android-studio textswitcher