1

**I'm trying to implement a TextSwitcher in a recyclerView but every time I debug it return an error message : java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.view.View.getLayoutParams()' on a null object reference Here is my Code 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>

Here is my Java Code :onBindViewHolder from RecyclerView Adapter

  @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;
        }
    });
        }

Here my 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;
}

While i have initialized my TextSwitcher. I don't really know where the problem comes from

1 Answers1

0

I found a solution. if it can help someone I have Implemented the TextSwitcher Dynamically and it works https://abhiandroid.com/ui/textswitcher

the TextSwitcher gave this error message because it asks to be initialized not by findViewById but TextSwitcher switcher = new TextSwitcher(); I found the code in the link above

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 15 '22 at 16:40