-1

I have some TextViews in a Fragment that I would like to access, but findViewById returns null. R.id.textView works fine (it returns the correct int ) , but view.findViewById() does not. I have inflated the view and have attempted a lot of fixes, but none work. This method does not work in any of the methods of the Fragment class.

I tried to use findViewByTag, but that returns null, too. I also tried to use Databinding, but that does not work either.

Here is my Fragment Class:

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view =  inflater.inflate(R.layout.fragment_sales_report_item, container, false);

        nameText = (TextView) view.findViewById(R.id.name_text);
        dateText = (TextView) view.findViewById(R.id.date_text);
        emailText = (TextView) view.findViewById(R.id.email_text);
        priceText = (TextView) view.findViewById(R.id.price_text);

        return view;
    }

Here is my XML.

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="@drawable/sales_report_item_shape"
    tools:context=".salesreport.fragments.SalesReportItemFragment">

    <TextView
        android:id="@+id/time_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="10dp"
        android:fontFamily="sans-serif"
        android:text="10:23AM"
        android:tag="time_text"
        android:textColor="@color/colorGreyVeryDark"
        android:textSize="14sp"
        app:layout_constraintEnd_toStartOf="@+id/date_text"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/email_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginBottom="28dp"
        android:fontFamily="@font/montserrat"
        android:text="vigneshmalikandan@hotmail.com"
        android:textColor="@color/colorGreyVeryDark"
        android:textSize="14sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/price_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:layout_marginEnd="16dp"
        android:fontFamily="@font/montserrat"
        android:text="$50000"
        android:textColor="#27AE60"
        android:textSize="32sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/date_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="8dp"
        android:fontFamily="sans-serif"
        android:text="4/30/19"
        android:textColor="@color/colorGreyVeryDark"
        android:textSize="14sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/name_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:fontFamily="@font/montserrat"
        android:text="Vignesh Manikandan"
        android:textColor="@android:color/black"
        android:textSize="20sp"
        android:textStyle="bold"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

</layout>

I expect that findViewById() returns a TextView, but it returns null.

Ansh Gupta
  • 47
  • 1
  • 6
  • is it correct layout (fragment_sales_report_item ) which you have inflated? – Vicky May 03 '19 at 04:51
  • Most probably this is not the layout you have inflated check layout file name again. Inflation of view does not matter here cause you are calling `view.findViewById()` i.e you already have the parent view's reference .. – ADM May 03 '19 at 04:59
  • 1
    You are using data binding. So you have to bind your layout not inflating it. Check this [link](https://stackoverflow.com/questions/34706399/how-to-use-data-binding-with-fragment). – Surender Kumar May 03 '19 at 05:05
  • @Vicky, yes, I have put in the correct R.layout file. – Ansh Gupta May 03 '19 at 05:08
  • @Ansh Try to inflate like this DataBindingUtil.inflate(inflater, R.layout.yourlayout, container, false) – Vicky May 03 '19 at 05:51
  • I think you are called setText() method before initiate objects. please update you full fragment code.on issue – Rajasekaran M May 03 '19 at 06:02
  • @Vicky as I mentioned above, I tried that method but dataBinding.nameText still returns null – Ansh Gupta May 03 '19 at 13:06
  • Just for curiosity , why are you `finding View By Tag` in one case and the others By id – coroutineDispatcher May 03 '19 at 15:14

2 Answers2

0

Your code is fine tryout this in onViewCreated()

 @Override 
 public void onViewCreated(View view, Bundle savedInstanceState) {
   super.onViewCreated(view, savedInstanceState)

    nameText = (TextView) view.findViewById(R.id.name_text);
    timeText = (TextView) view.findViewWithTag("time_text");
    dateText = (TextView) view.findViewById(R.id.date_text);
    emailText = (TextView) view.findViewById(R.id.email_text);
    priceText = (TextView) view.findViewById(R.id.price_text);
}

And your onCreateView() will be like this

@Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view =  inflater.inflate(R.layout.fragment_sales_report_item, container, false);
    return view;
}
Arbaz Pirwani
  • 935
  • 7
  • 22
  • I tried this, but unfortunately, it did not work...do you have any other suggestions? Here is the error: `I/com.mainsoftdc.costoma.salesreport.fragments.SalesReportFragment: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference ` – Ansh Gupta May 03 '19 at 05:05
-1

You are trying to do findViewById() before view is created.

Add your view.findViewById() on onViewCreated() method.

Gokul Nath KP
  • 15,485
  • 24
  • 88
  • 126