Corey Jacobb
Corey Jacobb

Reputation: 29

Gallery Activity Crashing?

Other Activities in my application are opening and changing just fine, but for some reason this one specifically is crashing. Any idea what my issue is?

This is the class file

    package com.gcomcode.oakgang;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

@SuppressWarnings("deprecation")
public class GalleryActivity extends Activity {
private Gallery gallery;

private ImageView imgView;


private Integer[] Imgid = {

        R.drawable.photo1, R.drawable.photo2, R.drawable.photo3, R.drawable.photo4, R.drawable.photo5, R.drawable.photo6, R.drawable.photo7, R.drawable.photo9,

};


@Override

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.gallery_activity);


    imgView = (ImageView)findViewById(R.id.ImageView01);

    imgView.setImageResource(Imgid[0]);


     gallery = (Gallery) findViewById(R.id.examplegallery);

     gallery.setAdapter(new AddImgAdp(this));


     gallery.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

            imgView.setImageResource(Imgid[position]);

        }

    });


}


public class AddImgAdp extends BaseAdapter {

    int GalItemBg;

    private Context cont;


    public AddImgAdp(Context c) {

        cont = c;

        TypedArray typArray = obtainStyledAttributes(R.styleable.Gallery1);

        GalItemBg = typArray.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);

        typArray.recycle();

    }


    public int getCount() {

        return Imgid.length;

    }


    public Object getItem(int position) {

        return position;

    }


    public long getItemId(int position) {

        return position;

    }


    public View getView(int position, View convertView, ViewGroup parent) {

        ImageView imgView = new ImageView(cont);


        imgView.setImageResource(Imgid[position]);

        imgView.setLayoutParams(new Gallery.LayoutParams(80, 70));

        imgView.setScaleType(ImageView.ScaleType.FIT_XY);

        imgView.setBackgroundResource(GalItemBg);


        return imgView;

    }

}


}

and this is the corresponding .xml

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

<LinearLayout android:id="@+id/LinearLayout01"

android:layout_width="fill_parent" android:layout_height="fill_parent"

xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical">

<Gallery 

    android:id="@+id/examplegallery" android:layout_width="fill_parent"

    android:layout_height="wrap_content" />

<ImageView android:id="@+id/ImageView01"

    android:layout_width="wrap_content" android:layout_height="wrap_content"/>

</LinearLayout>

LogCat Errors:

06-29 21:16:47.914: E/AndroidRuntime(22401): FATAL EXCEPTION: main 06-29 21:16:47.914: E/AndroidRuntime(22401): Process: com.gcomcode.oakgang, PID: 22401 06-29 21:16:47.914: E/AndroidRuntime(22401): java.lang.OutOfMemoryError 06-29 21:16:47.914: E/AndroidRuntime(22401): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 06-29 21:16:47.914: E/AndroidRuntime(22401): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:677) 06-29 21:16:47.914: E/AndroidRuntime(22401): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:507) 06-29 21:16:47.914: E/AndroidRuntime(22401): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:872) 06-29 21:16:47.914: E/AndroidRuntime(22401): at android.content.res.Resources.loadDrawable(Resources.java:3024) 06-29 21:16:47.914: E/AndroidRuntime(22401): at android.content.res.Resources.getDrawable(Resources.java:1586) 06-29 21:16:47.914: E/AndroidRuntime(22401): at android.widget.ImageView.resolveUri(ImageView.java:648) 06-29 21:16:47.914: E/AndroidRuntime(22401): at android.widget.ImageView.setImageResource(ImageView.java:377) 06-29 21:16:47.914: E/AndroidRuntime(22401): at com.gcomcode.oakgang.GalleryActivity$AddImgAdp.getView(GalleryActivity.java:108) 06-29 21:16:47.914: E/AndroidRuntime(22401): at android.widget.Gallery.makeAndAddView(Gallery.java:865) 06-29 21:16:47.914: E/AndroidRuntime(22401): at android.widget.Gallery.fillToGalleryRightLtr(Gallery.java:819) 06-29 21:16:47.914: E/AndroidRuntime(22401): at android.widget.Gallery.fillToGalleryRight(Gallery.java:763) 06-29 21:16:47.914: E/AndroidRuntime(22401): at android.widget.Gallery.layout(Gallery.java:672) 06-29 21:16:47.914: E/AndroidRuntime(22401): at android.widget.Gallery.onLayout(Gallery.java:357) 06-29 21:16:47.914: E/AndroidRuntime(22401): at android.view.View.layout(View.java:15655) 06-29 21:16:47.914: E/AndroidRuntime(22401): at android.view.ViewGroup.layout(ViewGroup.java:4856) 06-29 21:16:47.914: E/AndroidRuntime(22401): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677) 06-29 21:16:47.914: E/AndroidRuntime(22401): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531) 06-29 21:16:47.914: E/AndroidRuntime(22401): at android.widget.LinearLayout.onLayout(LinearLayout.java:1440) 06-29 21:16:47.914: E/AndroidRuntime(22401): at android.view.View.layout(View.java:15655) 06-29 21:16:47.914: E/AndroidRuntime(22401): at android.view.ViewGroup.layout(ViewGroup.java:4856) 06-29 21:16:47.914: E/AndroidRuntime(22401): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) 06-29 21:16:47.914: E/AndroidRuntime(22401): at android.widget.FrameLayout.onLayout(FrameLayout.java:388) 06-29 21:16:47.914: E/AndroidRuntime(22401): at android.view.View.layout(View.java:15655) 06-29 21:16:47.914: E/AndroidRuntime(22401): at android.view.ViewGroup.layout(ViewGroup.java:4856) 06-29 21:16:47.914: E/AndroidRuntime(22401): at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:438) 06-29 21:16:47.914: E/AndroidRuntime(22401): at android.view.View.layout(View.java:15655) 06-29 21:16:47.914: E/AndroidRuntime(22401): at android.view.ViewGroup.layout(ViewGroup.java:4856) 06-29 21:16:47.914: E/AndroidRuntime(22401): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) 06-29 21:16:47.914: E/AndroidRuntime(22401): at android.widget.FrameLayout.onLayout(FrameLayout.java:388) 06-29 21:16:47.914: E/AndroidRuntime(22401): at android.view.View.layout(View.java:15655) 06-29 21:16:47.914: E/AndroidRuntime(22401): at android.view.ViewGroup.layout(ViewGroup.java:4856) 06-29 21:16:47.914: E/AndroidRuntime(22401): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2282) 06-29 21:16:47.914: E/AndroidRuntime(22401): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2002) 06-29 21:16:47.914: E/AndroidRuntime(22401): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1234) 06-29 21:16:47.914: E/AndroidRuntime(22401): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6465) 06-29 21:16:47.914: E/AndroidRuntime(22401): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803) 06-29 21:16:47.914: E/AndroidRuntime(22401): at android.view.Choreographer.doCallbacks(Choreographer.java:603) 06-29 21:16:47.914: E/AndroidRuntime(22401): at android.view.Choreographer.doFrame(Choreographer.java:573) 06-29 21:16:47.914: E/AndroidRuntime(22401): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789) 06-29 21:16:47.914: E/AndroidRuntime(22401): at android.os.Handler.handleCallback(Handler.java:733) 06-29 21:16:47.914: E/AndroidRuntime(22401): at android.os.Handler.dispatchMessage(Handler.java:95) 06-29 21:16:47.914: E/AndroidRuntime(22401): at android.os.Looper.loop(Looper.java:157) 06-29 21:16:47.914: E/AndroidRuntime(22401): at android.app.ActivityThread.main(ActivityThread.java:5356) 06-29 21:16:47.914: E/AndroidRuntime(22401): at java.lang.reflect.Method.invokeNative(Native Method) 06-29 21:16:47.914: E/AndroidRuntime(22401): at java.lang.reflect.Method.invoke(Method.java:515) 06-29 21:16:47.914: E/AndroidRuntime(22401): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265) 06-29 21:16:47.914: E/AndroidRuntime(22401): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081) 06-29 21:16:47.914: E/AndroidRuntime(22401): at dalvik.system.NativeStart.main(Native Method)

Upvotes: 1

Views: 122

Answers (2)

Gevork Safaryan
Gevork Safaryan

Reputation: 237

Try to use this in manifest file inside application tag.

android:largeHeap="true"

Upvotes: 0

Access Denied
Access Denied

Reputation: 9461

It's known trouble (feature) with android images. Google will show you tons of articles on this topic (Android images out of memory). You can find an article how android recommend to deal with this issue here.

Upvotes: 1

Related Questions