【问题标题】:Programmatically setting TextView backgroundcolor in ExpandableListView以编程方式在 ExpandableListView 中设置 TextView 背景色
【发布时间】: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


【解决方案1】:

如果您的 TextView lblTb1Head 在布局 tb1_group.xml 中,那么您应该将布局视图膨胀为:

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

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

【讨论】:

  • 好的,谢谢,没有空指针了,但现在可扩展列表视图除了应用的颜色之外什么都不显示。
  • 您的问题已得到解答(setBackgroundColor 导致 NPE),应接受该答案。如果您还有其他问题,请随时提出其他问题。
  • @HrundiV.Bakshi,你是对的。想想该睡觉了,明天又是新的一天。
猜你喜欢
  • 1970-01-01
  • 2021-10-18
  • 2014-06-24
  • 2017-02-18
  • 1970-01-01
  • 1970-01-01
  • 2011-01-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多