为什么我在自定义适配器中出现空对象错误?

时间:2015-04-23 15:43:57

标签: android listview custom-adapter

我有自定义适配器套件。当Listview有超过4-5个视图时,应用程序停止工作并在设置的textview中给出null对象。虽然这个代码在Listview中有2-3个视图时工作正常。 我评论了得到错误的textview。请好好享受!谢谢

更新:似乎如果我快速滚动,则会抛出错误。 ! 更新2:正如@Piotr所建议的,它工作正常,直到我向下滚动到最后并向上滚动,它将视图list_item_header转换为list_item。所以所有视图都变成了list_item。 错误:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(isEnteringText) {
        textView.append(event.getDisplayLabel());
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

1 个答案:

答案 0 :(得分:0)

试试这个:

public View getView(int position, View convertView, ViewGroup Parent)
{

        int getListPos = newsList.getFirstVisiblePosition();
        int count = Integer.parseInt(objects.get(position).get("ListCount"));
        my_url = objects.get(position).get(GUID);
        if (getListPos == count)
        {
            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.list_item_header, null);
            TextView HeaderText = (TextView) convertView.findViewById(R.id.headertext);
            TextView HeaderContent = (TextView) convertView.findViewById(R.id.headercontent);
            HeaderText.setText(objects.get(position).get(POST_TITLE).toUpperCase());
            HeaderContent.setText(objects.get(position).get(POST_CONTENT));
            if (objects.get(position).get(GUID).equals("NULL"))
            {
                postImage.setImageResource(R.drawable.default_bg);
            }
            else
            {
                new DownloadImageTask((ImageView) convertView.findViewById(R.id.img)).execute(my_url);
            }
        }
        else
        {
            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.list_item, null);

            TextView thisview = (TextView) convertView.findViewById(R.id.email);
            Log.d("Title", String.valueOf(thisview.getText()));
            TextView postContent = (TextView) convertView.findViewById(R.id.mobile);
            thisview.setText(objects.get(position).get(POST_TITLE).toUpperCase());
            postContent.setText(objects.get(position).get(POST_CONTENT));
            if (objects.get(position).get(GUID).equals("NULL"))
            {
                postImage.setImageResource(R.drawable.default_bg);
            }
            else
            {
                new DownloadImageTask((ImageView) convertView.findViewById(R.id.img)).execute(my_url);
            }
        }

        return convertView;
    }