0

所以我在我的应用程序中使用了一个众所周知的 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);
    ...

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

4

1 回答 1

1

id似乎不同,这是故意的吗?
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
(这个不行)

于 2013-04-20T00:50:14.987 回答