Android getChildAt始终返回null

Android getChildAt始终返回null,android,android-gridview,Android,Android Gridview,我在onCreateView的一个片段中设置了一个gridview,以显示一周中的几天,如下所示: weekGridView = (GridView)view.findViewById(R.id.weekGrid); // set up days of week grid dayAdapter = new ArrayAdapter<String>(ShowEventsNavFragment.this.getActivity(), R.layout.event_gridview_he

我在onCreateView的一个片段中设置了一个gridview,以显示一周中的几天,如下所示:

weekGridView = (GridView)view.findViewById(R.id.weekGrid);

// set up days of week grid
dayAdapter = new ArrayAdapter<String>(ShowEventsNavFragment.this.getActivity(), R.layout.event_gridview_header_cell, R.id.cellTextView, days);
headerGrid.setAdapter(dayAdapter);
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/cellTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:textStyle="bold"
        android:textSize="18sp" />

</RelativeLayout>
不幸的是,getChildAt方法总是返回null。如果查询gridview,我会发现它是可见的,但它没有子对象。gridview在屏幕上清晰可见,并填充了正确的值


提前感谢您的帮助

您必须覆盖适配器的
getView(…)
方法。尝试这样做:

dayAdapter = new ArrayAdapter<String>(getActivity(), R.layout.event_gridview_header_cell, R.id.cellTextView, days) {
    public View getView(int position, View convertView, android.view.ViewGroup parent) {
        View result = super.getView(position, convertView, parent);
        int highlightDay = cal.get(Calendar.DAY_OF_WEEK)
        // if I am right with indexing ...
        if(position == highlightDay - 1) {
            result.setBackgroundColor(0x448FCC85);
        } else {
            // set another background ... this is the default background, you have to provide this because the views are reused
        }
        return result;
    };
}
dayAdapter=newarrayadapter(getActivity(),R.layout.event\u gridview\u header\u cell,R.id.cellTextView,days){
公共视图getView(int位置,视图转换视图,android.View.ViewGroup父级){
查看结果=super.getView(位置、转换视图、父级);
int highlightDay=cal.get(日历日,每周)
//如果我是正确的索引。。。
如果(位置==highlightDay-1){
结果:setBackgroundColor(0x448FCC85);
}否则{
//设置另一个背景…这是默认背景,您必须提供此背景,因为视图是重复使用的
}
返回结果;
};
}

您应该将该代码放入适配器中。是否在onCreate()方法中调用getChildAt()?请尝试使用其他方法,如onResume()。值得注意的是,每周一天的值为1-7,而不是0-6。感谢您的回复。您建议在适配器中插入哪种代码?getChildAt在片段的onStart方法中被调用,因此视图可见。我验证了gridview的onItemClickListener是否返回了正确的视图(highlightday-1),因为它不是0based@KennyC:谢谢,这就是我发表评论的原因,我一直在等待其他人对此的评论,因为我不确定它是否基于零指数:)+1非常感谢。我的gridview上的getChildAt是否触发此行为?请删除对
getChildAt()
的调用。。。你不需要那个<代码>getView(…)将在首次设置gridview以及用户滚动它时调用。
dayAdapter = new ArrayAdapter<String>(getActivity(), R.layout.event_gridview_header_cell, R.id.cellTextView, days) {
    public View getView(int position, View convertView, android.view.ViewGroup parent) {
        View result = super.getView(position, convertView, parent);
        int highlightDay = cal.get(Calendar.DAY_OF_WEEK)
        // if I am right with indexing ...
        if(position == highlightDay - 1) {
            result.setBackgroundColor(0x448FCC85);
        } else {
            // set another background ... this is the default background, you have to provide this because the views are reused
        }
        return result;
    };
}