在ExpandableListView

时间:2016-01-17 19:35:07

标签: android android-fragments expandablelistview

我正在尝试设置textview(lblTb1Head)的简单backgroundcolor,它是片段中的 expandablelistview的一部分。 不幸的是,我总是在下面上得到一个nullpointerexception,我找不到原因?

Downvoters,请保持温和,这对我来说是一个漫长的夜晚,也许太长了。

txtGroup.setBackgroundColor(Color.parseColor("#E8EBED"));

txtGroup声明:

public class Tb1_KltGegevens extends Fragment {

    private View v, mProgressView;
    private TextView txtGroup;
...
}

我的碎片onCreateView:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.tb1_kltgegevens, container, false);
        ExpandList = (ExpandableListView) v.findViewById(R.id.expListViewTb1);
        txtGroup = (TextView) v.findViewById(R.id.lblTb1Head);

        SharedPreferences settings1 = getActivity().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        newTheme = settings1.getInt("themeCustom", 0);

        if (newTheme == THEME_LIGHT) {
            txtGroup.setBackgroundColor(Color.parseColor("#E8EBED"));
        }

        return v;
    }

我的tb1_kltgegevens的布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:layout_margin="8dp"
    tools:context=".MainActivity" >

    <ExpandableListView
        android:id="@+id/expListViewTb1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ExpandableListView>    
</LinearLayout> 

带有textview的 tb1_group.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="8dp">

    <TextView
        android:id="@+id/lblTb1Head"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
        android:textSize="17dp" />    
</LinearLayout>

1 个答案:

答案 0 :(得分:1)

如果您的TextView lblTb1Head位于布局tb1_group.xml中,那么您应该将布局视图充气为:

v = inflater.inflate(R.layout.tb1_group, container, false);

执行此操作然后txtGroup = (TextView) v.findViewById(R.id.lblTb1Head);不会返回null。