0

I have created a dashboard of my app containing six buttons on it. My Project code compiles perfectly without showing any error. When I click on any button for 1st time it also response perfectly but when i click for 2nd time at another button My App Crashes completely and it shows errors in Run tab.

I have googled many times but nothing to solve my critical statement Seeds class(MainActivity) is here:

public class Seeds extends AppCompatActivity {
RecyclerView mRecyclerView;
List<SeedsData> mySeedsList;
SeedsData mSeedsData;

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

    mRecyclerView = (RecyclerView)findViewById(R.id.recyclerView2);


    GridLayoutManager gridLayoutManager;
    gridLayoutManager = new GridLayoutManager(Seeds.this, 1);
    mRecyclerView.setLayoutManager(gridLayoutManager);

    mySeedsList = new ArrayList<>();
    mSeedsData = new SeedsData("Wheat","Wheat is a grass widely cultivated for its seed, a cereal grain which is a worldwide staple food. The many species of wheat together make up the genus Triticum; the most widely grown is common wheat (T. aestivum). Botanically, the wheat kernel is a type of fruit called a caryopsis.","Rs.50",R.drawable.wheat);
    mySeedsList.add(mSeedsData);

    mSeedsData = new SeedsData("Peanut","Peanut seeds are planted one to two inches deep, one every six inches or so apart, in rows about three feet apart. ... Thirty to forty days after emergence the plants bloom, form and enter the soil. The peanut shells and kernels develop and mature during the next 60 to 70 day period.","Rs.6000",R.drawable.peanut);
    mySeedsList.add(mSeedsData);

    mSeedsData = new SeedsData("Almond","The fruit of the almond is a drupe, consisting of an outer hull and a hard shell with the seed, which is not a true nut, inside. Shelling almonds refers to removing the shell to reveal the seed","Rs.700",R.drawable.almond);
    mySeedsList.add(mSeedsData);

    MyAdapter2   myAdapter2 = new MyAdapter2(Seeds.this,mySeedsList);
    mRecyclerView.setAdapter(myAdapter2);
}
}

SeedsData class code is here:

public class SeedsData {
private String itemName;
private String itemDescription;
private String itemPrice;
private int itemImage;
public SeedsData(String itemName, String itemDescription, String  itemPrice, int itemImage) {
    this.itemName = itemName;
    this.itemDescription = itemDescription;
    this.itemPrice = itemPrice;
    this.itemImage = itemImage;
}

public String getItemName() {
    return itemName;
}

public String getItemDescription() {
    return itemDescription;
}

public String getItemPrice() {
    return itemPrice;
}

public int getItemImage() {
    return itemImage;
}
}

MyAdapter3.java class code is here:

public class MyAdapter3 extends   RecyclerView.Adapter<DiseasesViewHolder>{

private Context mContext;
private List<DiseasesData> myDiseasesList;

public MyAdapter3(Context mContext, List<DiseasesData> mySeedsList) {
    this.mContext = mContext;
    this.myDiseasesList = mySeedsList;
}

@Override
public DiseasesViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View mView = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_row_item3,parent,false);
    return new DiseasesViewHolder(mView);
}


@Override
public void onBindViewHolder(@NonNull final DiseasesViewHolder holder, final int position) {

    holder.imageView.setImageResource(myDiseasesList.get(position).getItemImage());
    holder.mTitle.setText(myDiseasesList.get(position).getItemName());
    holder.mDescription.setText(myDiseasesList.get(position).getItemDescription());
    holder.mPrice.setText(myDiseasesList.get(position).getItemPrice());

   /* SeedsViewHolder.mCardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(mContext,SeedsDetailedActivity.class);
            intent.putExtra("Image",mySeedsList.get(holder.getAdapterPosition()).getItemImage());
            intent.putExtra("Description",mySeedsList.get(holder.getAdapterPosition()).getItemDescription());
            mContext.startActivity(intent);
        }
    });*/
}

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

class DiseasesViewHolder extends RecyclerView.ViewHolder      {

static ImageView imageView;
static TextView mTitle;
static TextView mDescription;
static TextView mPrice;
static CardView mCardView;

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

    imageView = itemView.findViewById(R.id.ivImage);
    mTitle = itemView.findViewById(R.id.tvTitle);
    mDescription = itemView.findViewById(R.id.tvDescription);
    mPrice = itemView.findViewById(R.id.tvPrice);
    mCardView = itemView.findViewById(R.id.myCardView);
}
}

When i click on given link in error statements it brings my cursor to the beginning of these statements, so here is the specific critical part of my code(because cursor blinks at the start of this part when i click on exception)

 holder.imageView.setImageResource(myDiseasesList.get(position).getItemImage());

Here is my Complete Stack trace List(from Run tab)

 W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x94cd8b20)
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.e_agriculture10, PID: 3694
java.lang.OutOfMemoryError
    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
    at  android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587)
    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:422)
    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
    at android.content.res.Resources.loadDrawable(Resources.java:2110)
    at android.content.res.Resources.getDrawable(Resources.java:700)
    at androidx.appcompat.widget.ResourcesWrapper.getDrawable(ResourcesWrapper.java:130)
    at androidx.appcompat.widget.TintResources.getDrawable(TintResources.java:46)
    at androidx.core.content.ContextCompat.getDrawable(ContextCompat.java:465)
    at androidx.appcompat.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:203)
    at androidx.appcompat.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:191)
    at androidx.appcompat.content.res.AppCompatResources.getDrawable(AppCompatResources.java:102)
    at androidx.appcompat.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:86)
    at androidx.appcompat.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:94)
    at com.example.e_agriculture10.MyAdapter3.onBindViewHolder(MyAdapter3.java:37)
    at com.example.e_agriculture10.MyAdapter3.onBindViewHolder(MyAdapter3.java:16)
    at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781)
    at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6823)
    at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5752)
    at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6019)
    at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
    at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
    at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
    at androidx.recyclerview.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:557)
    at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
    at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
    at androidx.recyclerview.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:171)
    at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
    at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3641)
    at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4194)
    at android.view.View.layout(View.java:14817)
    at android.view.ViewGroup.layout(ViewGroup.java:4631)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
    at  android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1660)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1436)
    at android.view.View.layout(View.java:14817)
    at android.view.ViewGroup.layout(ViewGroup.java:4631)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
    at android.view.View.layout(View.java:14817)
    at android.view.ViewGroup.layout(ViewGroup.java:4631)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
    at android.view.View.layout(View.java:14817)
    at android.view.ViewGroup.layout(ViewGroup.java:4631)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
    at android.view.View.layout(View.java:14817)
    at android.view.ViewGroup.layout(ViewGroup.java:4631)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
    at android.widget.LinearLayout.layoutVertica
I/Process: Sending signal. PID: 3694 SIG: 9
 Application terminated.

1 Answers1

0

You need to provide more info on what the method getItemImage() is doing. Basically there's nothing clear.

Basically it's saying that your app is trying to use more memory than it's allowed by the system. When dealing with Bitmaps, memory management should be thought through within implementation. If it is only for displaying an Image on the screen try to use the Glide library In your app/build.gradle add its dependency

 dependencies{
implementation 'com.github.bumptech.glide:glide:4.9.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0' }

And replace holder.imageView.setImageResource(myDiseasesList.get(position).getItemImage());

with

 Glide.with(your_context)
                .asBitmap()
                .load("your/Image/Path") ///feed in path of the image
                .centerCrop()
                .into(holder.imageView);
  • now I have provided all the three classes of my project .Now please check it. I,m stuck on this problem from previous two days – Engr Javed Abrar Aug 25 '19 at 08:25
  • It's the same issue as i mentionned before. You need to leave it to Glide as of now. Glide.with(your_context).asBitmap().load(myDiseasesList.get(position).getItemImage()).centerCrop().into(holder.imageView) – Rachid Rajjys Aug 30 '19 at 07:59
  • Great, it's a pleasure. If possible you should accept the answer so that others can have access to it – Rachid Rajjys Aug 31 '19 at 16:52