即使先放大视图,View.findViewById仍返回null

时间:2019-05-03 04:45:37

标签: java android

我想访问一个片段中的一些TextView,但是findViewById返回null。 R.id.textView可以正常工作(它返回正确的int),但是view.findViewById()不能。我夸大了视图并尝试了许多修复程序,但没有任何效果。该方法不适用于Fragment类的任何方法。

我尝试使用findViewByTag,但是也返回null。我也尝试使用数据绑定,但这也不起作用。

这是我的片段类:

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

这是我的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>

我希望findViewById()返回一个TextView,但它返回null.

2 个答案:

答案 0 :(得分:0)

您的代码很好,可以在 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);
}

您的 onCreateView()将会是这样

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

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

答案 1 :(得分:-1)

您正在尝试创建视图之前进行findViewById()

view.findViewById()方法上添加onViewCreated()