0

I am new to this android world..so please help... I am writing my own custom adapter using the GridView to populate images..but every time the above error is crashing my app..enclosing my MainActivity File, COdeAdapterFile

This is MainActivityCode com.example.android.internship;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import java.util.ArrayList;

public class Handicrafts extends AppCompatActivity  implements NavigationView.OnNavigationItemSelectedListener {
//THIS IS ARTIFACTS FILE
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mToggle;
    private Toolbar mToolbar;
    NavigationView nv;

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

        nv=(NavigationView)findViewById(R.id.mynavigation);
        nv.setNavigationItemSelectedListener(this);

        mToolbar=(Toolbar)findViewById(R.id.nav_action);
        setSupportActionBar(mToolbar);

        mDrawerLayout=(DrawerLayout) findViewById(R.id.drawerLayout);
        mToggle=new ActionBarDrawerToggle(this,mDrawerLayout,R.string.open,R.string.close);

        mDrawerLayout.addDrawerListener(mToggle);
        mToggle.syncState();

        getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);

        final ArrayList<Items> itemObject = new ArrayList<Items>();

        itemObject.add(new Items("",R.drawable.a1));
        itemObject.add(new Items("",R.drawable.a2));
        itemObject.add(new Items("",R.drawable.a3));
        itemObject.add(new Items("",R.drawable.a4));
        itemObject.add(new Items("",R.drawable.a5));

        itemObject.add(new Items("",R.drawable.a6));
        itemObject.add(new Items("",R.drawable.a7));
        itemObject.add(new Items("",R.drawable.a8));
        itemObject.add(new Items("",R.drawable.a9));
        itemObject.add(new Items("",R.drawable.a10));
        itemObject.add(new Items("",R.drawable.a11));
        itemObject.add(new Items("",R.drawable.a12));
        itemObject.add(new Items("",R.drawable.a13));
        itemObject.add(new Items("",R.drawable.a14));
        itemObject.add(new Items("",R.drawable.a15));

        itemObject.add(new Items("",R.drawable.a16));
        itemObject.add(new Items("",R.drawable.a17));
        itemObject.add(new Items("",R.drawable.a18));
        itemObject.add(new Items("",R.drawable.a19));


        ItemAdapter adapter = new ItemAdapter(this, itemObject);

        // Get a reference to the ListView, and attach the adapter to the listView.
        GridView newGrdidView = (GridView) findViewById(R.id.gridview);
        newGrdidView.setAdapter(adapter);
        final int []images={R.drawable.a1,R.drawable.a2,R.drawable.a3,R.drawable.a4,R.drawable.a5,R.drawable.a6,R.drawable.a7,R.drawable.a8,
                R.drawable.a9,R.drawable.a10,R.drawable.a11,R.drawable.a12,R.drawable.a13,R.drawable.a14,R.drawable.a15,R.drawable.a16,R.drawable.a17,
                R.drawable.a18,R.drawable.a19,};

        newGrdidView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent = new Intent(Handicrafts.this,
                        Main3Activity.class);
                int mImageId =0;
                intent.putExtra("img",images[position]);
                startActivity(intent);

            }
        });


    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if(mToggle.onOptionsItemSelected(item))
        {return true;}
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {

        switch(item.getItemId())
        {
            case R.id.paint:
                Intent paintIntent=new Intent(Handicrafts.this,MainActivity.class);
                startActivity(paintIntent);
                break;
            case R.id.covers:
                Intent collageIntent=new Intent(Handicrafts.this,collages.class);
                startActivity(collageIntent);
                break;
            case R.id.artifacts:
                Intent craftsIntent=new Intent(Handicrafts.this,Handicrafts.class);
                startActivity(craftsIntent);
                break;
        }
        return true;
    }
}

This is customAdapter code

package com.example.android.internship;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

import static android.R.attr.id;
import static android.R.attr.resource;

/**
 * Created by hatim on 6/6/2017.
 */

public class ItemAdapter extends ArrayAdapter<Items> {
    public ItemAdapter(Activity context,ArrayList<Items> itemObject) {
        super(context,0,itemObject);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View gridview=convertView;
        if(gridview==null)
        {
            gridview = LayoutInflater.from(getContext()).inflate(
                    R.layout.item_layout, parent, false);
        }

        Items currentItems = getItem(position);

        TextView nameTextView = null;
        if (gridview != null) {
            nameTextView = (TextView) gridview.findViewById(R.id.product_name);
        }
        // Get the version name from the current AndroidFlavor object and
        // set this text on the name TextView
        nameTextView.setText(currentItems.getItemName());

        ImageView iconView = null;
        if (gridview != null) {
            iconView = (ImageView) gridview.findViewById(R.id.product_images);

        }
        // Get the image resource ID from the current AndroidFlavor object and
        // set the image to iconView
        iconView.setImageResource(currentItems.getImageId());


        return gridview;
    }
}

This my Android Manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.internship">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:hardwareAccelerated="false"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity" />
        <activity android:name=".Main2Activity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".Main3Activity" />

        <activity
            android:name=".collages"
            android:label="Cushion covers" />

        <activity
            android:name=".Handicrafts"
            android:label="Artefacts" />
    </application>

</manifest>

PLEASE HELP ME!!

5
  • Possible duplicate of Out of Memory Error while loading bitmaps Commented Jun 13, 2017 at 18:57
  • didn't get you will you please elaborate...with solution Commented Jun 13, 2017 at 19:02
  • can you post stack trace of exception from Android monitor Commented Jun 13, 2017 at 19:08
  • Probable duplicate of this post. Try to recycle bitmap of your ImageView before you update your iconView's drawable. But first of all consider using view holder approach as a best practice for simple ListView and GridView controls Commented Jun 13, 2017 at 19:30
  • 15451212 byte allocation -- this is equivalent to a 1965 x 1965 pixel image. This is much too large. If this is coming from your resources, either they are too large or are in the wrong directory. Commented Jun 13, 2017 at 19:45

1 Answer 1

0

Try to use RecyclerView and ViewHolder approach and you can also use Picasso or Glide library to smooth loading of image into ImageView. You are tying to show images in a grid manner, so I think you can use RecyclerView and make the RecyclerView LayoutManager in stack format.

 compile 'com.github.bumptech.glide:glide:3.7.0'
 compile 'com.squareup.picasso:picasso:2.5.2'
 compile 'com.android.support:recyclerview-v7:25.3.1'

Have a look at this - https://guides.codepath.com/android/using-the-recyclerview Let me know if any problem occur!

your_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="and.com.bakingtime.activity.MainActivity">

   <android.support.v7.widget.RecyclerView
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:id="@+id/recycler_view" />
</LinearLayout>

list_item.xml

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/card_view"
    android:layout_width="80dp"
    android:layout_height="wrap_content"
    card_view:cardUseCompatPadding="true"
    card_view:cardCornerRadius="8dp"
    android:layout_marginBottom="16dp">
        <ImageView
            android:id="@+id/photo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="centerCrop" />
 </android.support.v7.widget.CardView>

your_adapter_class.java

public class your_adapter_class extends RecyclerView.Adapter<RecyclerViewHolders> {

    private List<ItemObject> itemList;
    private Context context;

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

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

        View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, null);
        RecyclerViewHolders rcv = new RecyclerViewHolders(layoutView);
        return rcv;
    }

    @Override
    public void onBindViewHolder(RecyclerViewHolders holder, int position) {
        Picasso.with(context).load((itemList.get(position).getPhoto()).into(holder.thumbnail);
    }

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

your_ViewHolder.java

public class your_ViewHolder extends RecyclerView.ViewHolder{

    public ImageView thumbnail;

    public RecyclerViewHolders(View itemView) {
        super(itemView);
        thumbnail = (ImageView)itemView.findViewById(R.id.photo);
    }
}

MainActivity.java
In your MainActivity onCreate making a GridLayoutManager and attaching it to your recyclerView

      private GridLayoutManager lLayout;

      // inside onCreate
       List<ItemObject> rowListItem = getAllItemList();
        lLayout = new GridLayoutManager(MainActivity.this, 2);

        RecyclerView rView = (RecyclerView)findViewById(R.id.recycler_view);
        rView.setLayoutManager(lLayout);

        RecyclerViewAdapter rcAdapter = new RecyclerViewAdapter(MainActivity.this, rowListItem);
        rView.setAdapter(rcAdapter);

    //getAllItemList method
    private List<ItemObject> getAllItemList(){

    List<ItemObject> itemObject = new ArrayList<ItemObject>();

    itemObject.add(new ItemObject("",R.drawable.a1));
    itemObject.add(new ItemObject("",R.drawable.a2));
    itemObject.add(new ItemObject("",R.drawable.a3));
    itemObject.add(new ItemObject("",R.drawable.a4));
    itemObject.add(new ItemObject("",R.drawable.a5));

    itemObject.add(new ItemObject("",R.drawable.a6));
    itemObject.add(new ItemObject("",R.drawable.a7));
    itemObject.add(new ItemObject("",R.drawable.a8));
    itemObject.add(new ItemObject("",R.drawable.a9));
    itemObject.add(new ItemObject("",R.drawable.a10));
    itemObject.add(new ItemObject("",R.drawable.a11));
    itemObject.add(new ItemObject("",R.drawable.a12));
    itemObject.add(new ItemObject("",R.drawable.a13));
    itemObject.add(new ItemObject("",R.drawable.a14));
    itemObject.add(new ItemObject("",R.drawable.a15));

    itemObject.add(new ItemObject("",R.drawable.a16));
    itemObject.add(new ItemObject("",R.drawable.a17));
    itemObject.add(new ItemObject("",R.drawable.a18));
    itemObject.add(new ItemObject("",R.drawable.a19));

    return ItemObject;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.