在Fragment onCreateView中获取TextView

时间:2016-09-03 07:08:58

标签: android android-studio-2.2

我想在TextView Fragment中获取onCreateView(),但返回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>

4 个答案:

答案 0 :(得分:2)

 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);

答案 1 :(得分:2)

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

android:layout_toRightOf="@+id/textViewNamere"

此行创建一个ID R.id.textViewNamere。但是id与任何视图都不对应,因此view1.findViewById(R.id.textViewNamere)总是返回null但不显示错误。

答案 2 :(得分:1)

尝试使用void pictureBox_Click(object sender, EventArgs e) { pictureBox1.Image = ((PictureBox)sender).Image; } 方法而不是onViewCreated()执行此操作。此方法将返回视图对象。用它来初始化textview。另外,请确保使用您在xml中定义的正确ID来引用它。

答案 3 :(得分:1)

在您的xml textViewNamere中未定义。您使用textViewNamere作为android:layout_toRightOf="@+id/textViewNamere"之类的参考。您的实际TextView标识为textViewOpen。所以将你的TextView转换为

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