GridView错误“ClassCastException:java.lang.Integer”

时间:2012-08-16 20:38:08

标签: android android-layout gridview

我正在关注google 2009并注意到我的代码效率低下,可能会导致内存问题。

这是我的实施

public View getView(final int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            ViewHolder viewHolder;
            if(li == null){
            li = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        if(convertView == null){
            convertView = li.inflate(R.layout.book_case_view, parent, false);

            viewHolder = new ViewHolder();
            viewHolder.image = (ImageView)convertView.findViewById(R.id.bookCover);
            viewHolder.textView = (TextView)convertView.findViewById(R.id.bookLabel);

            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
        bi = dbh.getData(FILES.get(position));

        convertView.setTag(position);
        convertView.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                BookCaseOnClick(position);
            }
        });

        viewHolder.image.setImageResource(R.drawable.book);//Create a drawable backup cover


        viewHolder.textView.setText( bi.getBook_Title());//Object refers to Book_information


viewHolder.textView.setBackgroundResource(R.drawable.alphabet_bar_bg_dark);
//          new LoadCover(image, bi).run();
            LoadCover(viewHolder.image, bi, position);
            return convertView;
        }

static class ViewHolder{
    ImageView image;
    TextView textView;
}

但是,在加载第一个循环后,我在此行上遇到错误。 (第二个)

viewHolder = (ViewHolder) convertView.getTag();

这些是错误。需要注意的主要问题是在调试期间发现了一个异常并在gridview类中传递了“java.lang.integer”错误

08-16 20:13:02.073: E/AndroidRuntime(6520): FATAL EXCEPTION: main
08-16 20:13:02.073: E/AndroidRuntime(6520): java.lang.ClassCastException: java.lang.Integer
08-16 20:13:02.073: E/AndroidRuntime(6520):     at epub.activity.View_Book_Selection_Screen$BookCaseAdapter.getView(View_Book_Selection_Screen.java:835)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.widget.AbsListView.obtainView(AbsListView.java:1409)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.widget.GridView.makeAndAddView(GridView.java:1222)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.widget.GridView.makeRow(GridView.java:268)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.widget.GridView.fillDown(GridView.java:221)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.widget.GridView.fillFromTop(GridView.java:340)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.widget.GridView.layoutChildren(GridView.java:1136)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.widget.AbsListView.onLayout(AbsListView.java:1260)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.view.View.layout(View.java:7175)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.view.View.layout(View.java:7175)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1243)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1049)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.view.View.layout(View.java:7175)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.view.View.layout(View.java:7175)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.view.View.layout(View.java:7175)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.view.View.layout(View.java:7175)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.view.View.layout(View.java:7175)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1140)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.os.Looper.loop(Looper.java:123)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.app.ActivityThread.main(ActivityThread.java:3683)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at java.lang.reflect.Method.invokeNative(Native Method)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at java.lang.reflect.Method.invoke(Method.java:507)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

您要将整数设置为标记

convertView.setTag(position);

但是你正在做

viewHolder = (ViewHolder) convertView.getTag();

这就是你得到这个例外的原因。将viewHolder设置为标记或将其转换为整数

答案 1 :(得分:1)

Akyl,这样的事情可能对你有帮助。

ViewHolder holder = null;
if (convertView == null || !(convertView.getTag() instanceof ViewHolder)) {
    convertView = mInflater.inflate(R.layout.your_view, null);
    holder = new ViewHolder(your params);
    convertView.setTag(holder);
} else {
    holder = (ViewHolder) convertView.getTag();
}