画廊活动崩溃?

时间:2014-06-30 02:21:30

标签: android xml android-activity

我的应用程序中的其他活动正在打开和更改,但由于某种原因,这个特别是崩溃。知道我的问题是什么吗?

这是类文件

    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;

    }

}


}

这是相应的.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错误:

06-29 21:16:47.914:E / AndroidRuntime(22401):致命异常:主要 06-29 21:16:47.914:E / AndroidRuntime(22401):进程: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):在android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 06-29 21:16:47.914:E / AndroidRuntime(22401):在android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:677) 06-29 21:16:47.914:E / AndroidRuntime(22401):在android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:507) 06-29 21:16:47.914:E / AndroidRuntime(22401):在android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:872) 06-29 21:16:47.914:E / AndroidRuntime(22401):在android.content.res.Resources.loadDrawable(Resources.java:3024) 06-29 21:16:47.914:E / AndroidRuntime(22401):在android.content.res.Resources.getDrawable(Resources.java:1586) 06-29 21:16:47.914:E / AndroidRuntime(22401):在android.widget.ImageView.resolveUri(ImageView.java:648) 06-29 21:16:47.914:E / AndroidRuntime(22401):在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):在android.widget.Gallery.makeAndAddView(Gallery.java:865) 06-29 21:16:47.914:E / AndroidRuntime(22401):在android.widget.Gallery.fillToGalleryRightLtr(Gallery.java:819) 06-29 21:16:47.914:E / AndroidRuntime(22401):在android.widget.Gallery.fillToGalleryRight(Gallery.java:763) 06-29 21:16:47.914:E / AndroidRuntime(22401):在android.widget.Gallery.layout(Gallery.java:672) 06-29 21:16:47.914:E / AndroidRuntime(22401):在android.widget.Gallery.onLayout(Gallery.java:357) 06-29 21:16:47.914:E / AndroidRuntime(22401):在android.view.View.layout(View.java:15655) 06-29 21:16:47.914:E / AndroidRuntime(22401):在android.view.ViewGroup.layout(ViewGroup.java:4856) 06-29 21:16:47.914:E / AndroidRuntime(22401):在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677) 06-29 21:16:47.914:E / AndroidRuntime(22401):在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531) 06-29 21:16:47.914:E / AndroidRuntime(22401):在android.widget.LinearLayout.onLayout(LinearLayout.java:1440) 06-29 21:16:47.914:E / AndroidRuntime(22401):在android.view.View.layout(View.java:15655) 06-29 21:16:47.914:E / AndroidRuntime(22401):在android.view.ViewGroup.layout(ViewGroup.java:4856) 06-29 21:16:47.914:E / AndroidRuntime(22401):在android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) 06-29 21:16:47.914:E / AndroidRuntime(22401):在android.widget.FrameLayout.onLayout(FrameLayout.java:388) 06-29 21:16:47.914:E / AndroidRuntime(22401):在android.view.View.layout(View.java:15655) 06-29 21:16:47.914:E / AndroidRuntime(22401):在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):在android.view.View.layout(View.java:15655) 06-29 21:16:47.914:E / AndroidRuntime(22401):在android.view.ViewGroup.layout(ViewGroup.java:4856) 06-29 21:16:47.914:E / AndroidRuntime(22401):在android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) 06-29 21:16:47.914:E / AndroidRuntime(22401):在android.widget.FrameLayout.onLayout(FrameLayout.java:388) 06-29 21:16:47.914:E / AndroidRuntime(22401):在android.view.View.layout(View.java:15655) 06-29 21:16:47.914:E / AndroidRuntime(22401):在android.view.ViewGroup.layout(ViewGroup.java:4856) 06-29 21:16:47.914:E / AndroidRuntime(22401):在android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2282) 06-29 21:16:47.914:E / AndroidRuntime(22401):在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2002) 06-29 21:16:47.914:E / AndroidRuntime(22401):在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1234) 06-29 21:16:47.914:E / AndroidRuntime(22401):在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:6465) 06-29 21:16:47.914:E / AndroidRuntime(22401):在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:803) 06-29 21:16:47.914:E / AndroidRuntime(22401):在android.view.Choreographer.doCallbacks(Choreographer.java:603) 06-29 21:16:47.914:E / AndroidRuntime(22401):在android.view.Choreographer.doFrame(Choreographer.java:573) 06-29 21:16:47.914:E / AndroidRuntime(22401):在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:789) 06-29 21:16:47.914:E / AndroidRuntime(22401):在android.os.Handler.handleCallback(Handler.java:733) 06-29 21:16:47.914:E / AndroidRuntime(22401):在android.os.Handler.dispatchMessage(Handler.java:95) 06-29 21:16:47.914:E / AndroidRuntime(22401):在android.os.Looper.loop(Looper.java:157) 06-29 21:16:47.914:E / AndroidRuntime(22401):在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)

2 个答案:

答案 0 :(得分:1)

知道安卓图片的麻烦(功能)。谷歌将向您展示关于此主题的大量文章(Android图像内存不足)。你可以找到一篇关于android如何推荐处理这个问题的文章here

答案 1 :(得分:0)

尝试在应用程序标记内的清单文件中使用它。

android:largeHeap="true"