在 Fragment onCreateView 中获取 TextView

Getting TextView in Fragment onCreateView

我正在尝试在 Fragment onCreateView() 中获取 TextView 但它是 return null。 下面是代码片段。

     View view1 = inflater.inflate(R.layout.fragment_employee_repotee, container,    false);
     TextView tv = (TextView) view1.findViewById(R.id.textViewNamere);
     tv.setText(EmpDirectoryListingFragment.emp_name);

我正在获取电视值 null。下面是布局的XML。

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="h2h.telenor.com.fragments.TAFListingFragment">
<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp"

    android:background="#006400">
<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#e4e4e4">
    <LinearLayout
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:paddingTop="5dp"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#f1f1f1">
        <TextView
            android:id="@+id/textViewOpen"
            android:paddingTop="10dp"
            android:layout_toRightOf="@+id/textViewNamere"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18dp"
            android:gravity="center"
            android:layout_gravity="center"
            android:text="Muhamamd Faisal Shahzad"/>
        <ImageView
            android:id="@+id/imageViewOpen"
            android:layout_alignParentLeft="true"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/up_arrow"/>

    </LinearLayout>
    <ListView
        android:layout_marginTop="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/listViewEmpDownHierarchy"
        android:drawSelectorOnTop="true">
    </ListView>
    </LinearLayout>
</LinearLayout>

尝试在 onViewCreated() 方法中而不是 onCreateView 中执行此操作。此方法将 return 您的视图对象。使用它来初始化您的文本视图。还要确保您通过在 xml.

中定义的正确 ID 引用它
 View view1 = inflater.inflate(R.layout.fragment_employee_repotee, container,    false);
 TextView tv = (TextView) view1.findViewById(R.id.textViewOpen);//since there is not textview in your layout with id R.id.textViewNamere
 tv.setText(EmpDirectoryListingFragment.emp_name);

在你的布局中只有一行使用 textViewNamere:

android:layout_toRightOf="@+id/textViewNamere"

这一行创建了一个 id R.id.textViewNamere。但是 ID 与任何视图都不对应,因此 view1.findViewById(R.id.textViewNamere) 始终 return null 但不会在此处显示错误。

在你的xml中textViewNamere没有定义。您使用 textViewNamere 作为参考,如 android:layout_toRightOf="@+id/textViewNamere"。您实际的 TextView id 是 textViewOpen。因此,将您的 TextView 投射为

TextView tv = (TextView) view1.findViewById(R.id.textViewOpen);