由于图像太多(仅20张图像),无法在Note 2上运行App

时间:2016-06-22 21:06:37

标签: java android android-studio bitmap

从“主活动”页面转到“新活动”页面时出现以下错误。新活动页面崩溃,除非它有< 10张图片左右,这很可怜(但是我的计算机上运行的应用程序运行得很好):

06-22 16:53:04.841 15058-15058/? D/AndroidRuntime: Shutting down VM
06-22 16:53:04.841 15058-15058/? W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41e35c08)
06-22 16:53:04.851 15058-15058/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                   Process: com.matthewscompany.matthew.test1, PID: 15058
                                                   java.lang.OutOfMemoryError
                                                       at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
                                                       at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:683)
                                                       at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:513)
                                                       at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:889)
                                                       at android.content.res.Resources.loadDrawable(Resources.java:3436)
                                                       at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
                                                       at android.widget.ImageView.<init>(ImageView.java:133)
                                                       at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:57)
                                                       at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:53)
                                                       at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:102)
                                                       at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:972)
                                                       at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:1030)
                                                       at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
                                                       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:690)
                                                       at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
                                                       at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
                                                       at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
                                                       at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
                                                       at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
                                                       at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276)
                                                       at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:136)
                                                       at com.example.matthew.test1.NewActivity.onCreate(NewActivity.java:23)
                                                       at android.app.Activity.performCreate(Activity.java:5451)
                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
                                                       at android.app.ActivityThread.access$900(ActivityThread.java:175)
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                       at android.os.Looper.loop(Looper.java:146)
                                                       at android.app.ActivityThread.main(ActivityThread.java:5602)
                                                       at java.lang.reflect.Method.invokeNative(Native Method)
                                                       at java.lang.reflect.Method.invoke(Method.java:515)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
                                                       at dalvik.system.NativeStart.main(Native Method)
06-22 16:53:04.861 2430-2769/? W/ActivityManager:   Force finishing activity com.matthewscompany.matthew.test1/com.example.matthew.test1.NewActivity
06-22 16:53:04.866 2430-2769/? W/ActivityManager:   Force finishing activity com.matthewscompany.matthew.test1/com.example.matthew.test1.MainActivity

据我所知,当处理过多位图时,手机的内存耗尽时会发生内存不足错误。

是的,我已阅读Android开发人员网页上有关处理位图的提示。但是,我仍然不明白我应该插入哪些代码组,以及我应该插入的代码。我已经尝试将他们的一些东西插入我的.java文件,但我最终得到错误。我认为这是我作为Java的初学者而不了解如何应用示例代码。

同样,我是Java和Android Studio的初学者,所以请尽可能简单地向我解释并尽可能简单地解释一下。

这是我的代码:

MainActivity.java:

package com.example.matthew.test1;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;

public class MainActivity extends AppCompatActivity {
//Main Menu

    //Main Activity Mario Icon:

    ImageButton Mario;
    ImageButton Luigi;

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

        Mario= (ImageButton) findViewById(R.id.Mario);

        Mario.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intentLoadNewActivity = new Intent(MainActivity.this, NewActivity.class);
                startActivity(intentLoadNewActivity);

            }
        });

       Luigi= (ImageButton) findViewById(R.id.Luigi);

        Luigi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intentLoadNewActivity = new Intent(MainActivity.this, Main2Activity.class);
                startActivity(intentLoadNewActivity);


            }
        });
    }



}

NewActivity.java:

public class NewActivity extends AppCompatActivity implements View.OnClickListener {

    ViewFlipper viewFlipper;
    Button next;
    Button previous;
    TextView pageCounter;
    int count = 1;
    private static final int TOTAL_IMG_COUNT = 23;

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

        viewFlipper = (ViewFlipper)findViewById(R.id.viewFlipper);
        next = (Button) findViewById(R.id.next);
        previous = (Button) findViewById(R.id.previous);
        pageCounter = (TextView)findViewById(R.id.textView2);
        pageCounter.setText(String.valueOf(count));
        next.setOnClickListener(this);
        previous.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        if (v == next) {
            viewFlipper.showNext();
            if (next.getId() == (R.id.ImageView23)) {
                count = 1;
            } else {
                count++;
            }
            pageCounter.setText(String.valueOf(count) + "/" + TOTAL_IMG_COUNT);
        }
        else if (v == previous) {
            viewFlipper.showPrevious();
            if(previous.getId() == (R.id.ImageView1)) {
                count = TOTAL_IMG_COUNT;
            } else {
                count--;
            }
            pageCounter.setText(String.valueOf(count) + "/" + TOTAL_IMG_COUNT);
        }
    }
}

content_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.matthew.test1.MainActivity"
    tools:showIn="@layout/activity_main">


    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/Mario"
        android:src="@drawable/mario_select"
        android:layout_above="@+id/samus_select"
        android:layout_alignLeft="@+id/samus_select"
        android:layout_alignStart="@+id/samus_select" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/Luigi"
        android:src="@drawable/luigi_select"
        android:layout_above="@+id/samus_select"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/dk_select"
        android:src="@drawable/dk_select"
        android:layout_above="@+id/falcon_select"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/link_select"
        android:src="@drawable/link_select"
        android:layout_below="@+id/fox_select"
        android:layout_alignParentLeft="true" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/samus_select"
        android:src="@drawable/samus_select"
        android:layout_above="@+id/kirby_select"
        android:layout_centerHorizontal="true" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/falcon_select"
        android:src="@drawable/falcon_select"
        android:layout_alignTop="@+id/samus_select"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/ness_select"
        android:src="@drawable/ness_select"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/yoshi_select"
        android:src="@drawable/yoshi_select"
        android:layout_below="@+id/falcon_select"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/kirby_select"
        android:src="@drawable/kirby_select"
        android:layout_above="@+id/pika_select"
        android:layout_alignLeft="@+id/samus_select"
        android:layout_alignStart="@+id/samus_select" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/fox_select"
        android:src="@drawable/fox_select"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/Luigi"
        android:layout_alignParentLeft="true" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/pika_select"
        android:src="@drawable/pika_select"
        android:layout_alignParentBottom="true"
        android:layout_alignLeft="@+id/kirby_select"
        android:layout_alignStart="@+id/kirby_select" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/jiggly_select"
        android:src="@drawable/jiggly_select"
        android:layout_below="@+id/yoshi_select"
        android:layout_alignLeft="@+id/yoshi_select"
        android:layout_alignStart="@+id/yoshi_select" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Hitbox64"
        android:id="@+id/textView"
        android:textSize="48dp"
        android:layout_above="@+id/Mario"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

activity_new.xml:

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

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.matthew.test1.NewActivity">

    <ViewFlipper
        android:layout_width="480dp"
        android:layout_height="480dp"
        android:id="@+id/viewFlipper">

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/ImageView1"
            android:scaleType="centerCrop"
            android:src="@drawable/b1"/>

        <ImageView
             android:layout_width="fill_parent"
             android:layout_height="fill_parent"
             android:id="@+id/ImageView2"
             android:scaleType="centerCrop"
             android:src="@drawable/b2"/>

         <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/ImageView3"
            android:scaleType="centerCrop"
            android:src="@drawable/b3"/>

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/ImageView4"
            android:scaleType="centerCrop"
            android:src="@drawable/b4"/>

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/ImageView5"
            android:scaleType="centerCrop"
            android:src="@drawable/b5"/>

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/ImageView6"
            android:scaleType="centerCrop"
            android:src="@drawable/b6"/>

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/ImageView7"
            android:scaleType="centerCrop"
            android:src="@drawable/b7"/>

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/ImageView8"
            android:scaleType="centerCrop"
            android:src="@drawable/b8"/>

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/ImageView9"
            android:scaleType="centerCrop"
            android:src="@drawable/b9"/>

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/ImageView10"
            android:scaleType="centerCrop"
            android:src="@drawable/b10"/>

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/ImageView11"
            android:scaleType="centerCrop"
            android:src="@drawable/b11"/>

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/ImageView12"
            android:scaleType="centerCrop"
            android:src="@drawable/b12"/>

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/ImageView13"
            android:scaleType="centerCrop"
            android:src="@drawable/b13"/>

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/ImageView14"
            android:scaleType="centerCrop"
            android:src="@drawable/b14"/>

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/ImageView16"
            android:scaleType="centerCrop"
            android:src="@drawable/b17"/>

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/ImageView18"
            android:scaleType="centerCrop"
            android:src="@drawable/b18"/>

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/ImageView19"
            android:scaleType="centerCrop"
            android:src="@drawable/b19"/>

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/ImageView20"
            android:scaleType="centerCrop"
            android:src="@drawable/b20"/>

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/ImageView21"
            android:scaleType="centerCrop"
            android:src="@drawable/b21"/>

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/ImageView22"
            android:scaleType="centerCrop"
            android:src="@drawable/b22"/>

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/ImageView23"
            android:scaleType="centerCrop"
            android:src="@drawable/b23"/>

    </ViewFlipper>

    <Button
        android:id="@+id/next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Next"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"/>

    <Button
        android:id="@+id/previous"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Prev"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true"
        android:layout_alignTop="@+id/next"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Frame Counter"
        android:id="@+id/textView2"
        android:layout_below="@+id/textView3"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Bair"
        android:id="@+id/textView3"
        android:layout_alignTop="@+id/previous"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

如果你的图像分辨率太高,他们可能会耗尽很多的记忆力,特别是如你所说的那样有20个。请考虑使用Picasso或其他库来加载图片。