列表视图崩溃滚动&QUOT时,该应用程序可以做它的主线程的工作太多了" [英] List view crashes when scrolling "The application may be doing too much work on its main thread."

查看:349
本文介绍了列表视图崩溃滚动&QUOT时,该应用程序可以做它的主线程的工作太多了"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个列表视图以下列code顺畅滚动:

I am trying to create a list view that smoothly scrolls with the following code:

 <ListView
        android:id="@+id/list2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:listSelector="@android:color/transparent" />

// TT是和X(对象)数组有100个对象。

// tt is and array of x (Object)that have 100 objects.

x[] tt= response.getX();

ItemsAdapter myListAdapter = new ItemsAdapter(getActivity(),R.layout.mylist, tt);
// setListAdapter(myListAdapter);
list2.setAdapter(myListAdapter);

//适配器列表视图

// the adapter listview

 public class ItemsAdapter extends BaseAdapter {
    Context myContext;
    private int customLayoutId = -1;
    private X[] items;

    public ItemsAdapter(Context context, int resource, X[] x) {

        customLayoutId = resource;
        myContext = context;
        this.items = x;

    }

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

        // View row = convertView;

        try {
            LayoutInflater inflator = ((Activity) myContext)
                    .getLayoutInflater();

            xdata= items[position];

            if (convertView == null) {
                // row = inflater.inflate(customLayoutId, null);
                convertView = inflator.inflate(customLayoutId, null);
                DataViewHolder viewHolder1 = null;

                viewHolder1 = new DataViewHolder();

                viewHolder1.numberTV = (TextView) convertView
                        .findViewById(R.id.numberTV);
                viewHolder1.costTV = (TextView) convertView
                        .findViewById(R.id.costTV);
                viewHolder1.reserveButton = (Button) convertView
                        .findViewById(R.id.reserveButton);
                viewHolder1.reserveButton
                        .setBackgroundResource(R.drawable.reserve_button);

                viewHolder1.position = position;

                convertView.setTag(viewHolder1);
            }


            DataViewHolder viewHolder1 = (DataViewHolder) convertView
                    .getTag();


            viewHolder1.numberTV.setText(xdata.getMsisdn());

            viewHolder1.costTV.setText(etrData.getPrice());

            viewHolder1.reserveButton
                    .setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {



                    });

        } catch (Exception e) {
            e.printStackTrace();
        }

        return convertView;
    }

 }

  @Override
    public int getCount() {

        return this.items.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }
}

static class DataViewHolder {
    public TextView numberTV;
    public TextView costTV;
    public Button reserveButton;
    public int position;
}

当我滚动这个列表,应用程序崩溃这里是在logcat中:

When I scroll this list, the app crashes here is what is in Logcat:

07-27 02:50:26.756: I/Choreographer(24450): Skipped 46 frames!  The application may be       doing too much work on its main thread.
07-27 02:50:27.497: I/Choreographer(24450): Skipped 41 frames!  The application may be doing too much work on its main thread.
07-27 02:50:28.698: I/Choreographer(24450): Skipped 32 frames!  The application may be doing too much work on its main thread.

07-27 02:50:34.724: D/dalvikvm(24450): GC_CONCURRENT freed 1120K, 12% free 13993K/15815K, paused 6ms+6ms, total 35ms

07-27 02:50:35.655: I/Choreographer(24450): Skipped 41 frames!  The application may be doing too much work on its main thread.
07-27 02:50:45.685: I/Choreographer(24450): Skipped 58 frames!  The application may be doing too much work on its main thread.
07-27 02:50:46.936: I/Choreographer(24450): Skipped 69 frames!  The application may be doing too much work on its main thread.
07-27 02:50:57.677: I/Choreographer(24450): Skipped 31 frames!  The application may be doing too much work on its main thread.
07-27 02:50:59.639: I/Choreographer(24450): Skipped 53 frames!  The application may be doing too much work on its main thread.
07-27 02:51:03.953: D/TextLayoutCache(24450): Cache value 0x54a260f0 deleted, size = 280
07-27 02:51:03.963: D/TextLayoutCache(24450): Cache value 0x41002fd0 deleted, size = 400
07-27 02:51:03.963: D/TextLayoutCache(24450): Cache value 0x40f05908 deleted, size = 360
07-27 02:51:04.113: D/TextLayoutCache(24450): Cache value 0x413aa480 deleted, size = 384
07-27 02:51:04.123: D/TextLayoutCache(24450): Cache value 0x413a6200 deleted, size = 408

应该如何这些错误得到处理?我在做什么错了?

How should these errors be dealt with? What am I doing wrong?

推荐答案

首先,

我认为这个问题是在getView当您使用

I think the problem is in the getView when you use

LayoutInflater inflator = ((Activity) myContext).getLayoutInflater();

有没有必要将它转换为(活动),并使用该layoutInflater。

there is no need to cast it to (Activity) and use that layoutInflater.

和我看到你传递你的适配器的情况下custructor你可以只用

and as i see that you pass context in the custructor of your adapter you can just use

LayoutInflater inflater = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

此外,如果你要正确执行ViewHolder百通,你应该做的是这样的:

Also if you want to implement the ViewHolder patern correctly you should do it like this:

       DataViewHolder viewHolder1 = null;
       if (convertView == null) {
            // row = inflater.inflate(customLayoutId, null);
            convertView = inflator.inflate(customLayoutId, null);
            viewHolder1 = new DataViewHolder();
            viewHolder1.numberTV = (TextView) convertView.findViewById(R.id.numberTV);
            viewHolder1.costTV = (TextView) convertView.findViewById(R.id.costTV);
            viewHolder1.reserveButton = (Button)convertView.findViewById(R.id.reserveButton);      
            viewHolder1.reserveButton.setBackgroundResource(R.drawable.reserve_button);
            viewHolder1.position = position;
            convertView.setTag(viewHolder1);
        }
        else{
            viewHolder1 = (DataViewHolder) convertView.getTag();
        }

更新:

一般情况下,为了使一个ListView更快,你应该尽量让getView()简单,快速,并与我的意思是,你应该pretty多用它来将数据打印到$ P用户$ ppopulated值。

Generally, in order to make a listview faster , you should try to make getView() simple and fast, and with that i mean that you should pretty much use it to print your data to the user with prepopulated values.

在为了做到这一点,你应该正确执行viewholder模式,
从未访问数据库或者从互联网值在getView。

In order to do that you should correctly implement the viewholder pattern and never access database or get Values from internet in the getView.

之后,唯一可能的原因您的问题,我会在你的code发现是,如果方法

After that the only possible reason for your problem i may find in your code is if the methods

xdata.getMsisdn() and etrData.getPrice()

做背景比获得像访问互联网或数据库中的值以外的东西?

do something in the background other than getting values like accessing internet or a database?

这篇关于列表视图崩溃滚动&QUOT时,该应用程序可以做它的主线程的工作太多了&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆