[SOLVED] Add TextView programatically to a layout in onCreateViewHolder

Issue

This Content is from Stack Overflow. Question asked by user18668690

I am new to android developemnt. I have a adapter class where currectly, in onCreateViewHolder method, a fragment(XML) is displayed. I want to add a textView to this fragment only if a ‘if condition’ is met. This is what i have but I am getting null pointer

java.lang.NullPointerException: Attempt to invoke virtual method void android.widget.RelativeLayout.addView(android.view.View, android.view.ViewGroup$LayoutParams) on a null object reference
 @NonNull
 @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        if(AppUtil.isIT(taskList)){
            RelativeLayout ig_msg_layout = (RelativeLayout) viewGroup.findViewById(R.id.ig_message_layout);
            RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.FILL_PARENT);

                TextView if_tv = new TextView(context);
                inop_fuel_tv.setText("Test");
                inop_fuel_tv.setTextColor(context.getResources().getColor(R.color.error_red));
                inop_fuel_tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
                inop_fuel_tv.setTypeface(null, Typeface.BOLD);
                inop_gauge_msg_layout.addView(if_tv, params1);
            }

            View view = LayoutInflater.from(context).inflate(R.layout.fragment_task_list, viewGroup, false);
            return new ViewHolder(view);
    }
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fuel_list_rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/spinner_border">

    <RelativeLayout
        android:id="@+id/ig_message_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="5dp"
        android:layout_marginEnd="5dp"
        android:orientation="vertical">

    </RelativeLayout>
</RelativeLayout>



Solution

I’m not sure but I think you are trying to find a view in the parent view group. You should find it in the view that you are inflating.

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(context).inflate(R.layout.fragment_task_list, viewGroup, false);
    RelativeLayout ig_msg_layout = (RelativeLayout) view.findViewById(R.id.ig_message_layout);
    RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.FILL_PARENT);

    if(AppUtil.isIT(taskList)){
        TextView if_tv = new TextView(context);
        inop_fuel_tv.setText("Test");
        inop_fuel_tv.setTextColor(context.getResources().getColor(R.color.error_red));
        inop_fuel_tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
        inop_fuel_tv.setTypeface(null, Typeface.BOLD);
        inop_gauge_msg_layout.addView(if_tv, params1);
    }
    return new ViewHolder(view);
}


This Question was asked in StackOverflow by user18668690 and Answered by James It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?