【问题标题】:getView() is not finding views when scrolling?getView() 滚动时找不到视图?
【发布时间】:2015-01-22 00:33:56
【问题描述】:

我的一个片段中有一个列表视图,直到今天我将其高度设置为固定 400dp。现在我将其更改为 0dp 并使用布局中的其他视图和按钮对其进行加权。但是,在进行该更改后,我开始获得空指针。显然,当为列表中的项目调用 getView() 时,尝试使用 view.findViewById() 获取诸如 textview 之类的资源会突然返回 null。仅当滚动条可见并且我尝试向下滚动时才会发生这种情况。

在做了一些研究之后,我注意到每当从被移动的滚动条触发 getView() 时,视图都会设置为 convertView。出于某种原因,convertView 实际上是一个 textView?为什么这会作为 convertView 传递?为什么这在固定高度下工作,我该如何解决这个问题?

    @Override
    public View getView(LayoutInflater inflater, View convertView) {
        View view;
        if (convertView == null) {
            view = (View) inflater.inflate(R.layout.interview_item, null);
            // Do some initialization
        } else {
            view = convertView;
        }

//Crash on the next line with an error that text1 is a null pointer
            TextView text1 = (TextView) view.findViewById(R.id.name);
            TextView text2 = (TextView) view.findViewById(R.id.job);
            TextView text3 = (TextView) view.findViewById(R.id.time);
            ImageButton image = (ImageButton) view.findViewById(R.id.interview_type);

            text1.setText(this.getCandidate().getName());
            text2.setText(this.getJob().getTitle());

           //Parse the time of day from the date and format it
            SimpleDateFormat timeFmtOut = new SimpleDateFormat("h:mm a");
            text3.setText(timeFmtOut.format(this.getDate() ));

            if(this.type == interviewType.PHONE_CALL)
                image.setImageResource(R.drawable.phone_interview);
            else
                image.setImageResource(R.drawable.person_interview);



            return view;
        }

thanks

【问题讨论】:

  • 你能给你的R.layout.interview_item吗?

标签: java android


【解决方案1】:

它以固定高度工作,因为视图已经在 if 部分初始化,它没有进入 else 部分。 但是当滚动出现时,一旦创建了视图,它就会重用视图,所以它会转到 else 部分,因为你已经初始化了 view = convertView,但是那时 convertView 为 null,所以 view 为 null。 因此 findViewById() 崩溃了。 当 convertView 对象已经存在时,无需再使用一个视图 因此,将您的代码更改为以下。

@Override
    public View getView(LayoutInflater inflater, View convertView) {

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.interview_item, null);
            //initialization of views goes here
            TextView text1 = (TextView) convertView .findViewById(R.id.name);
            TextView text2 = (TextView) convertView .findViewById(R.id.job);
            TextView text3 = (TextView) convertView .findViewById(R.id.time);
            ImageButton image = (ImageButton) convertView .findViewById(R.id.interview_type);
        } else {
            // do something here
        }
            //set data here
            text1.setText(this.getCandidate().getName());
            text2.setText(this.getJob().getTitle());

           //Parse the time of day from the date and format it
            SimpleDateFormat timeFmtOut = new SimpleDateFormat("h:mm a");
            text3.setText(timeFmtOut.format(this.getDate() ));

            if(this.type == interviewType.PHONE_CALL)
                image.setImageResource(R.drawable.phone_interview);
            else
                image.setImageResource(R.drawable.person_interview);

  return convertView;
}

最佳实践是使用 ViewHolder 模式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-08
    • 2017-01-11
    • 2023-04-11
    • 1970-01-01
    相关资源
    最近更新 更多