RecyclerView error: “The application may be doing too much work on its main thread.”

Chào mọi người, Mìmh sử dụng RecyclerView để chứa 3 item nhưng nó cứ báo “The application may be doing too much work on its main thread.”

Có search trên mạng họ bảo dùng Runnable nhưng mình ko biết fix vào demo lúc này của mình như thế nào, mong giúp đỡ !

Bạn có thể đưa code lên để mọi người xem

1 Like

Bạn phải đưa code lên .
Lỗi : The application may be doing too much work on its main thread." là do bạn thực hiện nhiều task vụ nặng ở luồng UI thread thôi . Ko liên quan gì đến RecyclerView ở đây cả .

Blogs: http://bit.ly/24D139w
Hỏi đáp android : http://bit.ly/24Ckin7

2 Likes
public class Data {

    public static ArrayList<Information> getData(){
        ArrayList<Information> data = new ArrayList<>();
        int[] images = {
                R.drawable.mocchau1,
                R.drawable.mocchau2,
                R.drawable.mocchau3
        };
        String[] Categories = {"Moc Chau 1","Moc Chau 2", "Moc Chau 3"};

        for (int i=0; i<images.length;i++){
            Information current = new Information();
            current.imageId = images[i];
            current.title = Categories[i];
            data.add(current);
        }

        return data;
    }
}
public class Information {

    public int imageId;
    public String title;

}
public class MainActivity extends AppCompatActivity {
    RecyclerView recyclerView;
    MyCustomAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = (RecyclerView) findViewById(R.id.recycleView);
        adapter = new MyCustomAdapter(this, Data.getData());
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
    }

}
public class MyCustomAdapter extends RecyclerView.Adapter<MyCustomAdapter.MyViewHolder> {
    Context context;
    ArrayList<Information> data;
    LayoutInflater inflater;

    public MyCustomAdapter(Context context, ArrayList<Information> data) {
        this.context = context;
        this.data = data;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int position) {
        View view = inflater.inflate(R.layout.list_item_row, parent, false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.textView.setText(data.get(position).title);
        holder.imageView.setImageResource(data.get(position).imageId);
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView textView;
        ImageButton imageView;

        public MyViewHolder(View itemView) {
            super(itemView);

            textView = (TextView) itemView.findViewById(R.id.txv_row);
            imageView = (ImageButton) itemView.findViewById(R.id.img_row);
        }
    }
}

sửa dùm với ạ

còn đây là giao diện file activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycleView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v7.widget.RecyclerView>

</RelativeLayout>

file list_item_row.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:layout_margin="5dp"
    card_view:cardCornerRadius="5dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <ImageButton

            android:id="@+id/img_row"
            android:layout_width="match_parent"
            android:layout_height="200dp" />


        <TextView
            android:id="@+id/txv_row"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginLeft="10dp"
            android:text="Hello How Are you" />

        <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="match_parent"

            android:layout_height="60dp"
            android:layout_below="@id/txv_row"
            android:background="@color/colorPrimary"
            android:orientation="horizontal">


            <LinearLayout
                android:layout_width="150dp"
                android:layout_height="match_parent"
                android:layout_marginTop="15dp"
                android:orientation="horizontal">

                <ImageButton
                    android:layout_width="20dp"
                    android:layout_height="20dp"


                    android:layout_marginLeft="16dp"
                    android:background="@drawable/star" />

                <ImageButton
                    android:layout_width="20dp"
                    android:layout_height="20dp"

                    android:layout_marginLeft="5dp"

                    android:background="@drawable/star" />

                <ImageButton
                    android:layout_width="20dp"
                    android:layout_height="20dp"

                    android:layout_marginLeft="5dp"


                    android:background="@drawable/star" />

                <ImageButton
                    android:layout_width="20dp"
                    android:layout_height="20dp"


                    android:layout_marginLeft="5dp"

                    android:background="@drawable/star" />

                <ImageButton
                    android:layout_width="20dp"
                    android:layout_height="20dp"


                    android:layout_marginLeft="5dp"

                    android:background="@drawable/star" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="220dp"
                android:layout_height="match_parent"
                android:layout_marginTop="5dp"
             >

                <ImageButton
                    android:layout_width="40dp"
                    android:layout_height="40dp"


                    android:background="@drawable/heart" />

                <ImageButton
                    android:layout_width="40dp"
                    android:layout_height="40dp"


                    android:layout_marginLeft="15dp"
                    android:background="@drawable/chat" />

                <ImageButton
                    android:layout_width="40dp"
                    android:layout_height="40dp"

                    android:layout_marginLeft="15dp"
                    android:background="@drawable/share" />
                <ImageButton
                    android:layout_width="40dp"
                    android:layout_height="40dp"


                    android:layout_marginLeft="15dp"
                    android:background="@drawable/placeholder" />


            </LinearLayout>
        </LinearLayout>


    </LinearLayout>


</android.support.v7.widget.CardView>

Mình nghĩ có thể do 3 cái ảnh của bạn nặng , tuy bạn get nó từ drawable ra .
Bạn thử ảnh hơn xem sao.

đúng rôi nó cứ báo dòng đó,thử ảnh hơn là sao bạn

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?