0

我试图进入TextViewFragment 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

4 回答 4

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);
于 2016-09-03T07:14:48.363 回答
2

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

android:layout_toRightOf="@+id/textViewNamere"

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

于 2016-09-03T07:16:07.733 回答
1

尝试在onViewCreated()方法中而不是在onCreateView. 此方法将返回视图对象。用它来初始化你的 textview。还要确保通过在 xml 中定义的正确 id 引用它。

于 2016-09-03T07:14:13.757 回答
1

在您的 xmltextViewNamere中未定义。您用作textViewNamere参考之类的android:layout_toRightOf="@+id/textViewNamere". 您的实际 TextView id 是textViewOpen. 所以投你的 TextView 喜欢

TextView tv = (TextView) view1.findViewById(R.id.textViewOpen);
于 2016-09-03T07:22:19.013 回答