3
votes

I am creating an application using a recyclerview.While I try to scroll it on the mobile device I get the following error.

10-27 11:26:50.635 30536-30536/com.example.sabudaniel61.ktdc E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.OutOfMemoryError at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596) at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444) at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:817) at android.content.res.Resources.loadDrawable(Resources.java:2854) at android.content.res.Resources.getDrawable(Resources.java:1521) at android.widget.ImageView.resolveUri(ImageView.java:628) at android.widget.ImageView.setImageResource(ImageView.java:361) at com.example.sabudaniel61.ktdc.RecyclerAdapter.onBindViewHolder(RecyclerAdapter.java:41) at com.example.sabudaniel61.ktdc.RecyclerAdapter.onBindViewHolder(RecyclerAdapter.java:15) at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:4402) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3717) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3609) at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1859) at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1311) at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1274) at android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1102) at android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:959) at android.support.v7.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:3062) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791) at android.view.Choreographer.doCallbacks(Choreographer.java:591) at android.view.Choreographer.doFrame(Choreographer.java:560) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777) at android.os.Handler.handleCallback(Handler.java:725) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:176) at android.app.ActivityThread.main(ActivityThread.java:5302) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) at dalvik.system.NativeStart.main(Native Method)

Below is the layout for recyclerview:

<?xml version="1.0" encoding="utf-8"?>

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/relative1">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="480dp"
          android:scaleType="fitXY"
            android:id="@+id/imageView1"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:id="@+id/textView1"

                android:layout_centerHorizontal="true"
                android:layout_below="@+id/imageView1"
                android:textSize="35sp"
                android:textColor="#263238"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:id="@+id/textView2"

                android:layout_centerHorizontal="true"
                android:layout_below="@+id/textView1"
                android:textSize="20sp"
                android:textColor="#263238"/>
    </RelativeLayout>

The RecyclerView's class:

package com.example.sabudaniel61.ktdc;

import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by Sabudaniel61 on 2015-10-15.
 */
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder>{
    HotelData[] hoteldata;public int count;View v1;MainActivity main;

    View view;
    RecyclerAdapter(HotelData[] hoteldata,MainActivity main) {

this.main=main;
        this.hoteldata = hoteldata;

    }

    @Override
    public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recyclerlayout, null);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public long getItemId(int position) {

        return super.getItemId(position);
    }

    @Override
    public void onBindViewHolder(RecyclerAdapter.ViewHolder viewHolder, int i) {
        viewHolder.imageView.setImageResource(hoteldata[i].getImageUrl());
        viewHolder.textView1.setText(hoteldata[i].getHotel());
        viewHolder.textView2.setText(hoteldata[i].getPlace());



    }

    @Override
    public int getItemCount() {
        return hoteldata.length;

    }

    public class ViewHolder extends RecyclerView.ViewHolder  {
        TextView textView1, textView2; RecyclerView recyclerView;
        ImageView imageView;

        public ViewHolder(final View itemLayoutView) {
            super(itemLayoutView);
            v1=itemLayoutView;
            textView1 = (TextView) itemLayoutView.findViewById(R.id.textView1);
            textView2 = (TextView) itemLayoutView.findViewById(R.id.textView2);

            imageView = (ImageView) itemLayoutView.findViewById(R.id.imageView1);
            count=getAdapterPosition();
            itemLayoutView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    MascotFragment mascotFragment = new MascotFragment();

                    FrameLayout layout = (FrameLayout) main.findViewById(R.id.frame);
                    layout.removeAllViews();
                    FragmentTransaction transaction = main.getSupportFragmentManager().beginTransaction();
                    transaction.replace(R.id.frame, mascotFragment).commit();
                }
            });

        }
//        public void clicks(){
//
//        v1.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                 count = getAdapterPosition();
//                if (count == 1) {
//                    Toast.makeText(v1.getContext(),"Clicked Bolgatty",Toast.LENGTH_LONG).show();
//
//                }
//
//            }});
//
//
//
//}

    }
    }

Edited Code:

package com.example.sabudaniel61.ktdc;

import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

/**
 * Created by Sabudaniel61 on 2015-10-15.
 */
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder>{
    HotelData[] hoteldata;public int count;View v1;MainActivity main;

    View view;
    RecyclerAdapter(HotelData[] hoteldata,MainActivity main) {

this.main=main;
        this.hoteldata = hoteldata;

    }

    @Override
    public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recyclerlayout, null);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public long getItemId(int position) {

        return super.getItemId(position);
    }

    @Override
    public void onBindViewHolder(RecyclerAdapter.ViewHolder viewHolder, int i) {
        viewHolder.imageView.setImageResource(hoteldata[i].getImageUrl());
        viewHolder.textView1.setText(hoteldata[i].getHotel());
        viewHolder.textView2.setText(hoteldata[i].getPlace());



    }

    @Override
    public int getItemCount() {
        return hoteldata.length;

    }

    public class ViewHolder extends RecyclerView.ViewHolder  {
        TextView textView1, textView2; RecyclerView recyclerView;
        ImageView imageView;

        public ViewHolder(final View itemLayoutView) {
            super(itemLayoutView);
            v1=itemLayoutView;
            textView1 = (TextView) itemLayoutView.findViewById(R.id.textView1);
            textView2 = (TextView) itemLayoutView.findViewById(R.id.textView2);

            imageView = (ImageView) itemLayoutView.findViewById(R.id.imageView1);
            Picasso.with(main).load(R.drawable.aryahomepage).into(imageView);
            Picasso.with(main).load(R.drawable.bolgattyhomepage).into(imageView);
            Picasso.with(main).load(R.drawable.mascothomepage).into(imageView);
            Picasso.with(main).load(R.drawable.waterhomepage).into(imageView);
            Picasso.with(main).load(R.drawable.samudrahomepage).into(imageView);
            Picasso.with(main).load(R.drawable.goldenhomepage).into(imageView);
            Picasso.with(main).load(R.drawable.rainhomepage).into(imageView);
            Picasso.with(main).load(R.drawable.periyarhomepage).into(imageView);
            Picasso.with(main).load(R.drawable.pepperhomepage).into(imageView);
            Picasso.with(main).load(R.drawable.nandanamhomepage).into(imageView);
            Picasso.with(main).load(R.drawable.tamarindhomepage).into(imageView);












            count=getAdapterPosition();
            itemLayoutView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    MascotFragment mascotFragment = new MascotFragment();

                    FrameLayout layout = (FrameLayout) main.findViewById(R.id.frame);
                    layout.removeAllViews();
                    FragmentTransaction transaction = main.getSupportFragmentManager().beginTransaction();
                    transaction.replace(R.id.frame, mascotFragment).commit();
                }
            });

        }
//        public void clicks(){
//
//        v1.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                 count = getAdapterPosition();
//                if (count == 1) {
//                    Toast.makeText(v1.getContext(),"Clicked Bolgatty",Toast.LENGTH_LONG).show();
//
//                }
//
//            }});
//
//
//
//}

    }
    }
1
try lower your image's actual resolution or use third party image loader such as picasso, glide and universal image loader - cw fei
how can i apply glide to this code - jobin
can you show me how your image url looks like - cw fei
Yes, I will give.Please wait... - jobin
Hello, I have loaded it from res/drawable folder - jobin

1 Answers

1
votes

Try load your image from resource drawable folder using Picasso

This can be done by using the following code:

Picasso.with(context).load(R.drawable.drawableName).into(imageView);