【发布时间】:2016-01-17 19:35:06
【问题描述】:
我正在尝试设置 textview (lblTb1Head) 的简单背景色,它是 片段中的可扩展列表视图的一部分。 不幸的是,我总是在 below line 上得到一个空指针异常,我找不到原因?
投反对票的人,请温柔,这对我来说是一个漫长的夜晚,也许太长了。
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>
带有文本视图的 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>
【问题讨论】:
-
好的。
txtGroup在您的 Java 代码中声明为 TextView 的位置在哪里?旁注:fill_parent已于 2010 年弃用(使用 Froyo)。现在有match_parent。 -
@HrundiV.Bakshi,我将 txtGroup 的声明放在我的问题中。感谢 match_parent 的提示。
-
可以发一下布局文件
tb1_kltgegevens.xml吗?
标签: android android-fragments expandablelistview