2017-07-13 43 views
1


我有RecyclerView并且它有分页,当我滚动一些时候,它正在崩溃。当我慢慢地滚动时,它不会被撞坏。我提供了Adapter和Activity代码。请让我知道我犯的任何错误。我已经搜索了相同的问题,但我无法在这里发现错误。
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: 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.

+0

发表您的错误日志PLZ –

+0

后你[R堆栈跟踪。为什么你使用异步任务和http客户端,因为你可以轻松地使用Retrofit调用网络并使用jackson解析器解析json,这将消除很多代码 – Sony

+0

没有错误日志。 – Lokesh

回答

0

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

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

在我ViewHolder添加方法:

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

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

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(); 

    } 

这可能是为什么你的堆达到最大尺寸的原因之一。