【问题标题】:Android - setting the background color of a listView's child in a Fragments onResume function gives a NullPointerExceptionAndroid - 在 Fragments onResume 函数中设置 listView 子项的背景颜色会产生 NullPointerException
【发布时间】:2015-05-07 12:58:27
【问题描述】:

美好的一天。

我正在构建一个 Android 应用程序。在我的主要活动中,我有一个FragmentPageAdpater,我基本上做的是我有这个可重用的 Fragment 类,我实例化了 3 次。

我的片段包含三个不同“页面”的数据。我在我的操作栏中使用当前的“活动选项卡”来找出并过滤我应该向用户显示哪些数据。数据基本上是一个列表(狗、猫、鸟),用户可以选择一个品种。狗片段首先开始,当用户选择一个时,我的应用程序会突出显示所选行并继续到下一个(猫片段)。重复该过程,直到用户能够从所有三个类别中选择一个品种。

为了突出显示用户选择,我在我的onItemClickListener 我的ListView 中这样做

view.setBackgroundColor(Color.GREEN); 

其中viewonItemClickListener 函数中的View view 参数。

但是,当我到达鸟类片段并选择然后滚动/滑动回到狗片段时,绿色突出显示消失了。

我所做的尝试是有一个 int 来标记选择了哪个品种索引,然后在 Fragment 的 onResume 上,获取适当的 int 标记,然后获取我的 listView,获取该行的子项,然后设置背景颜色。但是,我在我的代码块中遇到了 NullPointerException:

这是我在 Fragment 中的 onResume 函数:

@Override
public void onResume() {
    super.onResume();

    //I get my MainActivity/the base one, get the view pager, and get the current item
    int index = ((MainActivity)getActivity()).mViewPager.getCurrentItem();

    Log.d("hi", "index is = " + index);

    switch(index){
        case 0:
            //dogSelected is the index of the selected item in the dogFragment
            if(((MainActivity)getActivity()).dogSelected > -1){

                Log.d("hi","selected dog = " + ((MainActivity)getActivity()).dogSelected);

                //I reinitialize the listView
                listViewBreeds = (ListView) rootView.findViewById(R.id.listViewBreeds);

                adapter = new ArrayAdapter<>(getActivity(),
                        android.R.layout.simple_list_item_1, android.R.id.text1, DogBreeds);
                //DogBreeds is an ArrayList of DogBreeds

                listViewBreeds.setAdapter(adapter);

                //check for null
                if(listViewBreeds == null){
                    Log.d("hi", "hello, lvn NULL");
                }

                int pos = ((MainActivity)getActivity()).dogSelected; // + 1;

                listViewBreeds.getChildAt(pos).setBackgroundColor(Color.GREEN);
            }

            break;


        case 1:
            //same as case 0
    }
}

正如您在我的代码中看到的,在片段的 onResume 期间,我检查了哪个片段索引处于活动状态,因此我知道要在我的 switch-case 语句中查找哪个列表。记录的索引是正确的。然后基于索引,然后我查找我的列表和保存在基本活动中的索引。选择的狗(或猫)是正确的。之后我重新初始化 listView 以确保它不为空(日志语句不显示)。然后我得到listView,让孩子在选定的位置,然后设置背景颜色。

但是,我在该行得到一个 NullPointer:

listViewBreeds.getChildAt(pos).setBackgroundColor(Color.GREEN);

我不知道其中的哪一部分是空的。

谁能帮我解决这个问题?我有一种感觉,我忽略了一些简单的事情,我无法弄清楚。

感谢您的帮助。

【问题讨论】:

  • 如果您正在查看布局或视图,findviewbyid() 通常会返回 null。有可能 rootview 在 onResume() 调用中还没有膨胀,因此无法找到属于它的元素。
  • 那么我如何确保rootView 被夸大了呢? rootView 看起来像rootView = inflater.inflate(R.layout.fragement_nominee_page, container, false);,我不认为我可以通过 onResume 方法对其进行初始化,因为容器来自 onStart 方法。
  • 在我们尝试找到解决方案之前,请确保 这是问题 一个简单的 if(listViewBreeds==null)Log.d("debug", "yep绝对是这个");会的。
  • 我不认为 ListView.getchildAt(); 是正确的方法,为什么你得到 NPE 我仍然不确定但你为什么不把 rootView 替换为 getView() 并且你可能想要检查这个间接的post 你的问题,它会给你正确的方式去
  • listViewBreeds 不为空,它会在listViewBreeds.setAdapter(adapter); 行抛出异常。我会邀请getChildAt(pos) 返回一个空对象。

标签: android listview android-fragments android-listview


【解决方案1】:

实际上我已经让它工作了。解决方案是使用 CustomListView 适配器,然后为行条目创建一个对象。该对象有一个布尔值isSelected,然后在自定义 ListView 适配器中,根据isSelected 变量将背景设置为不同的颜色或设置回白色。

【讨论】:

    猜你喜欢
    • 2013-08-06
    • 1970-01-01
    • 1970-01-01
    • 2011-08-28
    • 1970-01-01
    • 1970-01-01
    • 2013-08-04
    • 1970-01-01
    • 2021-09-27
    相关资源
    最近更新 更多