滚动到特定位置时,Recyclerview崩溃

时间:2017-07-13 02:57:44

标签: android android-recyclerview


我有RecyclerView并且它有分页,当我滚动一些时候它正在崩溃。当我慢慢滚动时,它没有被撞毁。我提供了适配器和活动代码。请告诉我这里的任何错误。我搜索过同样的问题,但我无法在这里发现错误   

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;
        }
    }
}

显示一些错误(日志):

  

java.lang.InternalError:在运行时关闭07-13期间启动的线程   09:01:29.721 32089-1640 /? W / System.err:at   java.lang.Thread.nativeCreate(Native方法)     ----------和----------   GC_FOR_ALLOC释放1057K,20%免费115415K / 143088K,暂停295ms,总计295ms   07-13 11:57:15.692 11792-12003 / com.dealmaar.customer I / dalvikvm-heap:为4000012字节分配增长堆(frag case)到116.585MB   07-13 11:57:15.936 11792-12001 / com.dealmaar.customer D / dalvikvm:GC_FOR_ALLOC释放<1K,17%自由119321K / 143088K,暂停245ms,总计245ms   07-13 11:57:15.960 11792-12017 / com.dealmaar.customer D / dalvikvm:GC_FOR_ALLOC释放41K,17%免费119389K / 143088K,暂停21ms,总计21ms   07-13 11:57:15.960 11792-12017 / com.dealmaar.customer I / dalvikvm-heap:将堆(frag case)增长到120.466MB,用于4000012字节分配   07-13 11:57:15.992 11792-12003 / com.dealmaar.customer D / dalvikvm:GC_FOR_ALLOC释放8K,14%免费123287K / 143088K,暂停29ms,总计29ms   07-13 11:57:15.992 11792-11792 / com.dealmaar.customer I / Choreographer:跳过34帧!应用程序可能在其主线程上做了太多工作。   07-13 11:57:16.036 11792-12001 / com.dealmaar.customer D / dalvikvm:GC_FOR_ALLOC释放8172K,20%免费115531K / 143088K,暂停23ms,总计23ms   07-13 11:57:16.036 11792-12001 / com.dealmaar.customer I / dalvikvm-heap:为4000012字节分配增长堆(frag case)到116.699MB   07-13 11:57:16.068 11792-12003 / com.dealmaar.customer D / dalvikvm:GC_FOR_ALLOC释放6K,17%免费119431K / 143088K,暂停29ms,总计29ms   07-13 11:57:16.096 11792-12017 / com.dealmaar.customer D / dalvikvm:GC_FOR_ALLOC释放119K,17%免费119613K / 143088K,暂停19ms,总计19ms   07-13 11:57:16.096 11792-12017 / com.dealmaar.customer I / dalvikvm-heap:将堆(frag case)增长到120.685MB,用于4000012字节分配   07-13 11:57:16.124 11792-12001 / com.dealmaar.customer D / dalvikvm:GC_FOR_ALLOC释放&lt; 1K,14%free 123519K / 143088K,暂停28ms,总计28ms   07-13 11:57:16.136 11792-11792 / com.dealmaar.customer V / ......:最后一项哇!   07-13 11:57:16.172 11792-12002 / com.dealmaar.customer D / dalvikvm:GC_FOR_ALLOC释放4252K,14%免费119709K / 139172K,暂停31ms,总计32ms   07-13 11:57:16.180 11792-12002 / com.dealmaar.customer I / dalvikvm-heap:将堆(frag case)增长到120.779MB,用于4000012字节分配   07-13 11:57:16.456 11792-12003 / com.dealmaar.customer D / dalvikvm:GC_FOR_ALLOC释放5K,12%免费123609K / 139172K,暂停263ms,总计263ms   07-13 11:57:16.680 11792-12003 / com.dealmaar.customer D / dalvikvm:GC_FOR_ALLOC释放3730K,14%免费120100K / 139172K,暂停170ms,总计170ms   07-13 11:57:16.680 11792-12003 / com.dealmaar.customer I / dalvikvm-heap:为4000012字节分配增长堆(frag case)到121.161MB   07-13 11:57:16.712 11792-12002 / com.dealmaar.customer D / dalvikvm:GC_FOR_ALLOC释放4K,11%释放124002K / 139172K,暂停32ms,总计32ms   07-13 11:57:16.716 11792-11792 / com.dealmaar.customer I / Choreographer:跳过39帧!应用程序可能在其主线程上做了太多工作。   07-13 11:57:16.724 11792-11792 / com.dealmaar.customer V / ...:最后一项哇!   07-13 11:57:17.032 11792-12001 / com.dealmaar.customer D / dalvikvm:GC_FOR_ALLOC释放269K,11%免费124134K / 139172K,暂停257ms,总计257ms   07-13 11:57:17.068 11792-12001 / com.dealmaar.customer I / dalvikvm-heap:将堆(frag case)增长到125.100MB,用于4000012字节分配   07-13 11:57:17.104 11792-11818 / com.dealmaar.customer D / dalvikvm:GC_FOR_ALLOC释放44K,11%免费127996K / 143080K,暂停36ms,总计36ms   07-13 11:57:17.292 11792-12002 / com.dealmaar.customer D / dalvikvm:GC_FOR_ALLOC释放75K,11%免费128064K / 143080K,暂停179ms,总计179ms   07-13 11:57:17.292 11792-12002 / com.dealmaar.customer I / dalvikvm-heap:将堆(frag case)增长到128.938MB以进行4000012字节分配   07-13 11:57:17.324 11792-12003 / com.dealmaar.customer D / dalvikvm:GC_FOR_ALLOC释放1K,11%免费131969K / 146988K,暂停31ms,总计31ms   07-13 11:57:17.348 11792-12003 / com.dealmaar.customer D / dalvikvm:GC_FOR_ALLOC被释放&lt; 1K,11%free 131969K / 146988K,暂停22ms,总计22ms   07-13 11:57:17.352 11792-12003 / com.dealmaar.customer I / dalvikvm-heap:将堆(frag case)增长到132.752MB,用于4000012字节分配   07-13 11:57:17.384 11792-12001 / com.dealmaar.customer D / dalvikvm:GC_FOR_ALLOC释放0K,10%免费135876K / 150896K,暂停32ms,总计32ms   07-13 11:57:17.384 11792-11792 / com.dealmaar.customer I / Choreographer:跳过33帧!应用程序可能在其主线程上做了太多工作。   07-13 11:57:17.384 11792-11792 / com.dealmaar.customer V / ...:最后一项哇!   07-13 11:57:17.420 11792-12017 / com.dealmaar.customer D / dalvikvm:GC_FOR_ALLOC释放133K,10%免费135855K / 150896K,暂停26ms,总计29ms   07-13 11:57:17.460 11792-12017 / com.dealmaar.customer I / dalvikvm-heap:将堆(frag case)增长到136.546MB,用于4000012字节分配   07-13 11:57:17.736 11792-12003 / com.dealmaar.customer D / dalvikvm:GC_FOR_ALLOC释放5K,10%免费139756K / 154804K,暂停275ms,总计275ms   07-13 11:57:17.980 11792-12002 / com.dealmaar.customer D / dalvikvm:GC_FOR_ALLOC释放16073K,20%免费124155K / 154804K,暂停168ms,总计168ms   07-13 11:57:17.980 11792-12002 / com.dealmaar.customer I / dalvikvm-heap:为4000012字节分配增长堆(frag case)到125.121MB   07-13 11:57:18.012 11792-12001 / com.dealmaar.customer D / dalvikvm:GC_FOR_ALLOC释放48K,18%免费128012K / 154804K,暂停30ms,总计30ms   07-13 11:57:18.032 11792-12001 / com.dealmaar.customer D / dalvikvm:GC_FOR_ALLOC释放32K,18%免费128044K / 154804K,暂停18ms,总计18ms   07-13 11:57:18.032 11792-12001 / com.dealmaar.customer I / dalvikvm-heap:将堆(frag case)增长到128.918MB,用于4000012字节分配   07-13 11:57:18.064 11792-12017 / com.dealmaar.customer D / dalvikvm:GC_FOR_ALLOC释放0K,15%免费131950K / 154804K,暂停30ms,总计30ms   07-13 11:57:18.084 11792-12017 / com.dealmaar.customer D / dalvikvm:GC_FOR_ALLOC释放32K,15%免费131982K / 154804K,暂停19ms,总计19ms   07-13 11:57:18.084 11792-12017 / com.dealmaar.customer I / dalvikvm-heap:将堆(frag case)增长到132.765MB,用于4000012字节分配   07-13 11:57:18.104 11792-11818 / com.dealmaar.customer D / dalvikvm:GC_FOR_ALLOC释放0K,13%免费135889K / 154804K,暂停20ms,总计20ms   07-13 11:57:18.108 11792-11792 / com.dealmaar.customer I / Choreographer:跳过42帧!应用程序可能在其主线程上做了太多工作。   07-13 11:57:18.136 11792-12003 / com.dealmaar.customer D / dalvikvm:GC_FOR_ALLOC释放125K,13%免费135840K / 154804K,暂停26ms,总计27ms   07-13 11:57:18.140 11792-12003 / com.dealmaar.customer I / dalvikvm-heap:为4000012字节分配增长堆(frag case)到136.532MB   07-13 11:57:18.168 11792-11999 / com.dealmaar.customer D / dalvikvm:GC_FOR_ALLOC释放2K,10%免费139744K / 154804K,暂停29ms,总计29ms   07-13 11:57:19.216 11792-11792 / com.dealmaar.customer I / Choreographer:跳过32帧!应用程序可能在其主线程上做了太多工作。   07-13 11:57:19.800 11792-11792 / com.dealmaar.customer I / Choreographer:跳过34帧!应用程序可能在其主线程上做了太多工作。

3 个答案:

答案 0 :(得分:0)

你在主线程中做了太多工作。系统是通过它的限制并执行您的过程它需要大量的资源,它冻结主UI线程导致ANR错误。我建议您使用AsyncTask在后​​台线程中执行所有API调用和JSON解析。

答案 1 :(得分:0)

你有超出内存的例外。 在某些设备中,当您传递最大堆时,它不会显示任何日志,JVM只会终止您的应用程序。 当您缓慢滚动时,您让JVM完成其垃圾收集过程,但是当您快速滚动时,它没有足够的时间来完成其任务,您将通过最大堆。 我看到你持有对视图列表和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();

    }

这可能是您的堆达到最大大小的原因之一。

答案 2 :(得分:0)

应用程序因内存管理失败而崩溃,此处每个项目都分配了动画,并且应用的动画未被清除。我已经解决了您花费1天的问题,这对我有用。

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

在我的ViewHolder中添加了方法:

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