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.