ViewHolder模式问题

时间:2013-04-20 00:24:36

标签: android performance

所以我在我的应用程序中使用了一个众所周知的ViewHolder模式。我的ViewHolder:

protected static class ViewHolder{
    final ImageView itemImage;
    final TextView txtName;
    final TextView txtTitle;
    final WebView webView;

    public ViewHolder (final ImageView itemImage,  final TextView txtName, final TextView txtTitle, final WebView webView){
        this.itemImage = itemImage;
        this.txtName = txtName;
        this.txtTitle = txtTitle;
        this.webView = webView;
    }

    public ViewHolder (View convertView){
        itemImage = (ImageView) convertView.findViewById(R.id.fav_item_icon);
        txtName = (TextView) convertView.findViewById(R.id.fav_item_name);
        txtTitle = (TextView) convertView.findViewById(R.id.fav_item_title);
        webView = (WebView) convertView.findViewById(R.id.fav_webView);
    }
}

所以如果我将它与第一个构造函数一起使用,一切正常:

...
                public View getView(final int position, View convertView, final ViewGroup parent) {

                    final Searchable searchResultsItem = searchResults.get(position);

                    ViewHolder viewHolder = null;
                    if (convertView==null){
                        convertView = inflater.inflate(R.layout.search_list_item, null);

                        final ImageView itemImage = (ImageView) convertView.findViewById(R.id.search_item_icon);
                        final TextView txtName = (TextView) convertView.findViewById(R.id.search_item_name);
                        final TextView txtTitle = (TextView) convertView.findViewById(R.id.search_item_title);
                        final WebView webView = (WebView) convertView.findViewById(R.id.search_webView);
                        viewHolder = new ViewHolder(itemImage, txtName, txtTitle, webView);

                        convertView.setTag(viewHolder);
                    }else
                        viewHolder = (ViewHolder)convertView.getTag();

                    viewHolder.webView.setFocusable(false);
    ...

但如果我尝试使用第二个构造函数则失败:

...
                public View getView(final int position, View convertView, final ViewGroup parent) {

                    final Searchable searchResultsItem = searchResults.get(position);

                    ViewHolder viewHolder = null;
                    if (convertView==null){
                        convertView = inflater.inflate(R.layout.search_list_item, null);

                        viewHolder = new ViewHolder(convertView);
                        convertView.setTag(viewHolder);
                    }else
                        viewHolder = (ViewHolder)convertView.getTag();

                    viewHolder.webView.setFocusable(false);
    ...

它在行viewHolder.webView.setFocusable(false)处与NPE崩溃。当我尝试调试时,说创建的ViewHolder实例的所有字段都为null。为什么会发生这种情况?

1 个答案:

答案 0 :(得分:1)

ids似乎有所不同,是故意的吗? final WebView webView = (WebView)convertView.findViewById(R.id.search_webView); R.id.search_webView(这个有效)

webView = (WebView) convertView.findViewById(R.id.fav_webView);
R.id.fav_webView
(这个不起作用)