1
votes


I have RecyclerView and it has Pagination, When I scroll some times it is being crashed. When I scroll slowly it is not being crashed. I have provided Adapter and Activity code. Please let me know any mistake here I did. I have searched for same issues , but I am not able to catch error here.

private class GetTrending extends AsyncTask<Void, Void, Void> {
    BufferedReader bufferedReader = null;
    private StringBuffer stringBuffer = new StringBuffer();
    private JSONArray jArray = new JSONArray();
    private int pageNum;
    private boolean value;

    public GetTrending(int pageNum, boolean value) {
        this.pageNum = pageNum;
        this.value = value;
    }

    @Override
    protected Void doInBackground(Void... params) {
        try {

            HttpClient httpClient = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet();
            URI uri = new URI(getString(R.string.url) +
                    "products?bestsellers=on&items_per_page=24&page=" + pageNum + mWholeSaleUrlTag);

            httpGet.setURI(uri);
            httpGet.addHeader(BasicScheme.authenticate(
                    new UsernamePasswordCredentials(getString(R.string.username), getString(R.string.password)),
                    HTTP.UTF_8, false));
            HttpResponse httpResponse;
            Log.e("Trending-Before", "Date" + new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(new Date()));
            httpResponse = httpClient.execute(httpGet);
            Log.e("Trending-After", "New Date" + new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(new Date()));
            InputStream inputStream = httpResponse.getEntity().getContent();
            bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            //Log.e("Hi", String.valueOf(bufferedReader));
            String readLine = bufferedReader.readLine();
            while (readLine != null) {
                stringBuffer.append(readLine);
                //stringBuffer.append("\n");
                readLine = bufferedReader.readLine();

            }
            //Log.e("BestSellers", String.valueOf(stringBuffer));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        try {
            JSONObject prodObjects = new JSONObject(String.valueOf(stringBuffer));
            jArray = prodObjects.optJSONArray("products");
            if (jArray.length() < 24) {
                shouldFetchTrending = false;
            }

            for (int i = 0; i < jArray.length(); i++) {
                Home home = new Home();
                prodObjects = jArray.getJSONObject(i);
                home.setProduct_id(prodObjects.getString("product_id"));
                // Log.e("Trending id", jobj.getString("product_id"));
                home.setProduct_name(prodObjects.getString("product"));
                NumberFormat nf = NumberFormat.getInstance();
                nf.setMinimumFractionDigits(2);
                nf.setMaximumFractionDigits(2);
                home.setProduct_price(nf.format(Double.parseDouble(prodObjects.getString("price"))));
                home.setProduct_listprice(nf.format(Double.parseDouble(prodObjects.getString("list_price"))));
                String mAmount = prodObjects.getString("amount");
                home.setAmount(mAmount);


                Item item = new Item();

                item.setName(prodObjects.getString("product"));
                item.setProductId(prodObjects.getString("product_id"));
                item.setPrice(nf.format(Double.parseDouble(prodObjects.getString("price"))));
                item.setListPrice(nf.format(Double.parseDouble(prodObjects.getString("list_price"))));
                item.setImage(prodObjects.getJSONObject("main_pair").getJSONObject("detailed").getString("image_path"));
                item.setAmount(mAmount);


                home.setProduct_image(prodObjects.getJSONObject("main_pair").getJSONObject("detailed").getString("image_path"));

                if (prodObjects.getString("status").equalsIgnoreCase("A") && !prodObjects.getString("amount").equalsIgnoreCase("0")) {
                    Sports.add(home);
                    trendingItemList.add(item);
                }


            }
            // Log.e("Length", String.valueOf(Sports.size()));
        } catch (JSONException e) {
            e.printStackTrace();
        }


        return null;
    }

    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        shouldFetchTrending = true;
        if (value) {
            trendingItemAdapter = new ItemAdapter(HomeActivity.this, trendingItemList);
            mTrendingRecyclerView.setAdapter(trendingItemAdapter);
        }
        trendingItemAdapter.notifyDataSetChanged();


        mTrendingRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(HomeActivity.this, new RecyclerItemClickListener.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                ((CardView) view).setCardElevation(100);
                Intent myintent = new Intent(HomeActivity.this, ProductPageActivity.class);
                myintent.putExtra("Prodid", trendingItemList.get(position).getProductId());
                myintent.putExtra("FromHome", "Home");
                startActivity(myintent);
            }
        }));

        try {
            for (int i = 0; i < Sports.size(); ++i) {
                trendinglinear[i].setVisibility(View.VISIBLE);
                tname[i].setText(Sports.get(i).getProduct_name());
                tprice[i].setText(Sports.get(i).getProduct_price());
                ((View) tname[i].getParent()).setTag(Sports.get(i).getProduct_id());
                String[] simg = new String[25];
                simg[i] = Sports.get(i).getProduct_image();
                //Log.e("image", simg[i]);
                Picasso.with(getApplicationContext()).load(simg[i]).error(R.drawable.ic_launcher).resize(400, 400).into(timage[i]);

            }
        } catch (Exception e) {
            //Toast.makeText(HomeActivity.this, "Failed to connect to internet Try again !", Toast.LENGTH_SHORT).show();

        }

    }
}

public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ViewHolder> {
    private List<Item> itemList;
    private Context context;
    private int lastPosition = -1;

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.home_recycler_item, parent, false);

        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Item item = itemList.get(position);
        holder.tvName.setText(item.getName());
        if(Integer.valueOf(item.getAmount()) == 0)
        holder.tvOutofstack.setVisibility(View.VISIBLE);
        else holder.tvOutofstack.setVisibility(View.INVISIBLE);

        holder.tvPrice.setText(item.getPrice());
        Picasso.with(context).load(item.getImage()).into(holder.ivImage);
        setAnimation(holder.cardView, position);

    }

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

    public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView tvName,tvOutofstack;
        public TextView tvPrice;
        public ImageView ivImage;

        public CardView cardView;



        public ViewHolder(View itemView) {
            super(itemView);
            tvName = (TextView) itemView.findViewById(R.id.tv_name);
            tvOutofstack = (TextView) itemView.findViewById(R.id.item_adapter_tv);
            tvPrice = (TextView) itemView.findViewById(R.id.tv_price);
            ivImage = (ImageView) itemView.findViewById(R.id.iv_image);
            cardView = (CardView) itemView.findViewById(R.id.card_view);
        }
    }

    public ItemAdapter(Context context, List<Item> itemList) {
        this.itemList = itemList;
        this.context = context;
    }

    private void setAnimation(View viewToAnimate, int position)
    {
        // If the bound view wasn't previously displayed on screen, it's animated
        if (position > lastPosition)
        {
            Animation animation = AnimationUtils.loadAnimation(context,  android.R.anim.slide_in_left);
            viewToAnimate.startAnimation(animation);
            lastPosition = position;
        }
    }
}

Some error shown(Log):

java.lang.InternalError: Thread starting during runtime shutdown 07-13 09:01:29.721 32089-1640/? W/System.err: at java.lang.Thread.nativeCreate(Native Method) ----------and ---------- GC_FOR_ALLOC freed 1057K, 20% free 115415K/143088K, paused 295ms, total 295ms 07-13 11:57:15.692 11792-12003/com.dealmaar.customer I/dalvikvm-heap: Grow heap (frag case) to 116.585MB for 4000012-byte allocation 07-13 11:57:15.936 11792-12001/com.dealmaar.customer D/dalvikvm: GC_FOR_ALLOC freed <1K, 17% free 119321K/143088K, paused 245ms, total 245ms 07-13 11:57:15.960 11792-12017/com.dealmaar.customer D/dalvikvm: GC_FOR_ALLOC freed 41K, 17% free 119389K/143088K, paused 21ms, total 21ms 07-13 11:57:15.960 11792-12017/com.dealmaar.customer I/dalvikvm-heap: Grow heap (frag case) to 120.466MB for 4000012-byte allocation 07-13 11:57:15.992 11792-12003/com.dealmaar.customer D/dalvikvm: GC_FOR_ALLOC freed 8K, 14% free 123287K/143088K, paused 29ms, total 29ms 07-13 11:57:15.992 11792-11792/com.dealmaar.customer I/Choreographer: Skipped 34 frames! The application may be doing too much work on its main thread. 07-13 11:57:16.036 11792-12001/com.dealmaar.customer D/dalvikvm: GC_FOR_ALLOC freed 8172K, 20% free 115531K/143088K, paused 23ms, total 23ms 07-13 11:57:16.036 11792-12001/com.dealmaar.customer I/dalvikvm-heap: Grow heap (frag case) to 116.699MB for 4000012-byte allocation 07-13 11:57:16.068 11792-12003/com.dealmaar.customer D/dalvikvm: GC_FOR_ALLOC freed 6K, 17% free 119431K/143088K, paused 29ms, total 29ms 07-13 11:57:16.096 11792-12017/com.dealmaar.customer D/dalvikvm: GC_FOR_ALLOC freed 119K, 17% free 119613K/143088K, paused 19ms, total 19ms 07-13 11:57:16.096 11792-12017/com.dealmaar.customer I/dalvikvm-heap: Grow heap (frag case) to 120.685MB for 4000012-byte allocation 07-13 11:57:16.124 11792-12001/com.dealmaar.customer D/dalvikvm: GC_FOR_ALLOC freed <1K, 14% free 123519K/143088K, paused 28ms, total 28ms 07-13 11:57:16.136 11792-11792/com.dealmaar.customer V/...: Last Item Wow ! 07-13 11:57:16.172 11792-12002/com.dealmaar.customer D/dalvikvm: GC_FOR_ALLOC freed 4252K, 14% free 119709K/139172K, paused 31ms, total 32ms 07-13 11:57:16.180 11792-12002/com.dealmaar.customer I/dalvikvm-heap: Grow heap (frag case) to 120.779MB for 4000012-byte allocation 07-13 11:57:16.456 11792-12003/com.dealmaar.customer D/dalvikvm: GC_FOR_ALLOC freed 5K, 12% free 123609K/139172K, paused 263ms, total 263ms 07-13 11:57:16.680 11792-12003/com.dealmaar.customer D/dalvikvm: GC_FOR_ALLOC freed 3730K, 14% free 120100K/139172K, paused 170ms, total 170ms 07-13 11:57:16.680 11792-12003/com.dealmaar.customer I/dalvikvm-heap: Grow heap (frag case) to 121.161MB for 4000012-byte allocation 07-13 11:57:16.712 11792-12002/com.dealmaar.customer D/dalvikvm: GC_FOR_ALLOC freed 4K, 11% free 124002K/139172K, paused 32ms, total 32ms 07-13 11:57:16.716 11792-11792/com.dealmaar.customer I/Choreographer: Skipped 39 frames! The application may be doing too much work on its main thread. 07-13 11:57:16.724 11792-11792/com.dealmaar.customer V/...: Last Item Wow ! 07-13 11:57:17.032 11792-12001/com.dealmaar.customer D/dalvikvm: GC_FOR_ALLOC freed 269K, 11% free 124134K/139172K, paused 257ms, total 257ms 07-13 11:57:17.068 11792-12001/com.dealmaar.customer I/dalvikvm-heap: Grow heap (frag case) to 125.100MB for 4000012-byte allocation 07-13 11:57:17.104 11792-11818/com.dealmaar.customer D/dalvikvm: GC_FOR_ALLOC freed 44K, 11% free 127996K/143080K, paused 36ms, total 36ms 07-13 11:57:17.292 11792-12002/com.dealmaar.customer D/dalvikvm: GC_FOR_ALLOC freed 75K, 11% free 128064K/143080K, paused 179ms, total 179ms 07-13 11:57:17.292 11792-12002/com.dealmaar.customer I/dalvikvm-heap: Grow heap (frag case) to 128.938MB for 4000012-byte allocation 07-13 11:57:17.324 11792-12003/com.dealmaar.customer D/dalvikvm: GC_FOR_ALLOC freed 1K, 11% free 131969K/146988K, paused 31ms, total 31ms 07-13 11:57:17.348 11792-12003/com.dealmaar.customer D/dalvikvm: GC_FOR_ALLOC freed <1K, 11% free 131969K/146988K, paused 22ms, total 22ms 07-13 11:57:17.352 11792-12003/com.dealmaar.customer I/dalvikvm-heap: Grow heap (frag case) to 132.752MB for 4000012-byte allocation 07-13 11:57:17.384 11792-12001/com.dealmaar.customer D/dalvikvm: GC_FOR_ALLOC freed 0K, 10% free 135876K/150896K, paused 32ms, total 32ms 07-13 11:57:17.384 11792-11792/com.dealmaar.customer I/Choreographer: Skipped 33 frames! The application may be doing too much work on its main thread. 07-13 11:57:17.384 11792-11792/com.dealmaar.customer V/...: Last Item Wow ! 07-13 11:57:17.420 11792-12017/com.dealmaar.customer D/dalvikvm: GC_FOR_ALLOC freed 133K, 10% free 135855K/150896K, paused 26ms, total 29ms 07-13 11:57:17.460 11792-12017/com.dealmaar.customer I/dalvikvm-heap: Grow heap (frag case) to 136.546MB for 4000012-byte allocation 07-13 11:57:17.736 11792-12003/com.dealmaar.customer D/dalvikvm: GC_FOR_ALLOC freed 5K, 10% free 139756K/154804K, paused 275ms, total 275ms 07-13 11:57:17.980 11792-12002/com.dealmaar.customer D/dalvikvm: GC_FOR_ALLOC freed 16073K, 20% free 124155K/154804K, paused 168ms, total 168ms 07-13 11:57:17.980 11792-12002/com.dealmaar.customer I/dalvikvm-heap: Grow heap (frag case) to 125.121MB for 4000012-byte allocation 07-13 11:57:18.012 11792-12001/com.dealmaar.customer D/dalvikvm: GC_FOR_ALLOC freed 48K, 18% free 128012K/154804K, paused 30ms, total 30ms 07-13 11:57:18.032 11792-12001/com.dealmaar.customer D/dalvikvm: GC_FOR_ALLOC freed 32K, 18% free 128044K/154804K, paused 18ms, total 18ms 07-13 11:57:18.032 11792-12001/com.dealmaar.customer I/dalvikvm-heap: Grow heap (frag case) to 128.918MB for 4000012-byte allocation 07-13 11:57:18.064 11792-12017/com.dealmaar.customer D/dalvikvm: GC_FOR_ALLOC freed 0K, 15% free 131950K/154804K, paused 30ms, total 30ms 07-13 11:57:18.084 11792-12017/com.dealmaar.customer D/dalvikvm: GC_FOR_ALLOC freed 32K, 15% free 131982K/154804K, paused 19ms, total 19ms 07-13 11:57:18.084 11792-12017/com.dealmaar.customer I/dalvikvm-heap: Grow heap (frag case) to 132.765MB for 4000012-byte allocation 07-13 11:57:18.104 11792-11818/com.dealmaar.customer D/dalvikvm: GC_FOR_ALLOC freed 0K, 13% free 135889K/154804K, paused 20ms, total 20ms 07-13 11:57:18.108 11792-11792/com.dealmaar.customer I/Choreographer: Skipped 42 frames! The application may be doing too much work on its main thread. 07-13 11:57:18.136 11792-12003/com.dealmaar.customer D/dalvikvm: GC_FOR_ALLOC freed 125K, 13% free 135840K/154804K, paused 26ms, total 27ms 07-13 11:57:18.140 11792-12003/com.dealmaar.customer I/dalvikvm-heap: Grow heap (frag case) to 136.532MB for 4000012-byte allocation 07-13 11:57:18.168 11792-11999/com.dealmaar.customer D/dalvikvm: GC_FOR_ALLOC freed 2K, 10% free 139744K/154804K, paused 29ms, total 29ms 07-13 11:57:19.216 11792-11792/com.dealmaar.customer I/Choreographer: Skipped 32 frames! The application may be doing too much work on its main thread. 07-13 11:57:19.800 11792-11792/com.dealmaar.customer I/Choreographer: Skipped 34 frames! The application may be doing too much work on its main thread.

3
post your error log plz배준모
Post you r stacktrace. And why are you using async task and http client as you can easly call network using Retrofit and parse json using the jackson parser and that will eliminate a lot of codeSony
there is no error log.Lokesh

3 Answers

0
votes

App is being crashed for memory management failure , here every item is assigned animation , and applied animation is not cleared.I have solved your problem spending 1 day, it's worked for me.

@Override public void onViewDetachedFromWindow(ViewHolder holder) { 
super.onViewDetachedFromWindow(holder);
 ((ViewHolder)holder).clearAnimation();
 } 

In my ViewHolder added method:

public void clearAnimation() { 
itemView.clearAnimation(); 
}
0
votes

You have Out Of Memory Exception. In some devices, when you pass the max heap, it won't show any log and the JVM just terminate your application. When you scroll slowly, you let the JVM to complete its garbage collection process, But when you scroll fast, it doesn't have enough time to complete its task and you will pass the max heap. I see you hold a reference to a list of views and ImageViews.

try {
        for (int i = 0; i < Sports.size(); ++i) {
            trendinglinear[i].setVisibility(View.VISIBLE);
            tname[i].setText(Sports.get(i).getProduct_name());
            tprice[i].setText(Sports.get(i).getProduct_price());
            ((View) tname[i].getParent()).setTag(Sports.get(i).getProduct_id());
            String[] simg = new String[25];
            simg[i] = Sports.get(i).getProduct_image();
            //Log.e("image", simg[i]);
            Picasso.with(getApplicationContext()).load(simg[i]).error(R.drawable.ic_launcher).resize(400, 400).into(timage[i]);

        }
    } catch (Exception e) {
        //Toast.makeText(HomeActivity.this, "Failed to connect to internet Try again !", Toast.LENGTH_SHORT).show();

    }

It may be one of the reasons that why your heap is reaching the max size.

0
votes

You are doing too much work in the main thread. System is way passed it's limit and to execute your process it takes a significant amount of resources and it freezes the main UI thread leads to ANR error. I recommend you to do all the API calls and JSON parsing in the in the background thread by using AsyncTask.