Android Studio - 带图像的活动崩溃模拟器,但在手机中工作

时间:2016-03-28 08:12:38

标签: java android xml android-studio android-logcat

我在Android Studio中添加了新的Activity。这个活动有一张图片。这是我的xml.file

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

<ImageView
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:id="@+id/imageView2"
    android:src="@drawable/logoauxguide"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

这是我的java类代码 包com.example.gonalo.meu;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;

public class AuxGuide1 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.auxguide1);
        int secondsDelayed = 1;
        new Handler().postDelayed(new Runnable() {
            public void run() {
                startActivity(new Intent(AuxGuide1.this, MainActivity.class));

                finish();
            }
        }, secondsDelayed * 4000);
    }

}

它在我的手机中完美运行,但是当我在模拟器上运行时,它说“对不起,应用程序已停止&#34;

这是我的logcat

03-28 08:01:20.513 29031-29031/com.example.gonalo.meu W/art: Throwing OutOfMemoryError "Failed to allocate a 79051908 byte allocation with 1048576 free bytes and 63MB until OOM"
03-28 08:01:20.514 29031-29031/com.example.gonalo.meu D/skia: --- allocation failed for scaled bitmap
03-28 08:01:20.514 29031-29031/com.example.gonalo.meu D/AndroidRuntime: Shutting down VM
03-28 08:01:20.515 29031-29031/com.example.gonalo.meu E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                 Process:   com.example.gonalo.meu, PID: 29031
                                                                     java.lang.OutOfMemoryError: Failed to allocate a 79051908 byte allocation with 1048576 free bytes and 63MB until OOM
                                                                        at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
                                                                        at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
                                                                        at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
                                                                        at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
                                                                        at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1080)
                                                                        at android.content.res.Resources.loadDrawableForCookie(Resources.java:2635)
                                                                        at android.content.res.Resources.loadDrawable(Resources.java:2540)
                                                                        at android.content.res.TypedArray.getDrawable(TypedArray.java:870)
                                                                        at android.widget.ImageView.<init>(ImageView.java:152)
                                                                        at android.widget.ImageView.<init>(ImageView.java:140)
                                                                        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:106)
                                                                        at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:972)
                                                                        at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:1031)
                                                                        at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
                                                                        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:746)
                                                                        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
                                                                        at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
                                                                        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                                                                        at android.view.LayoutInflater.parseInclude(LayoutInflater.java:971)
                                                                        at android.view.LayoutInflater.rInflate(LayoutInflater.java:831)
                                                                        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
                                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
                                                                        at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276)
                                                                        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
                                                                        at com.example.gonalo.meu.AuxGuide1.onCreate(AuxGuide1.java:13)
                                                                        at android.app.Activity.performCreate(Activity.java:6237)
                                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                        at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                        at android.os.Looper.loop(Looper.java:148)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
03-28 08:01:25.402 29031-29031/? I/Process: Sending signal. PID: 29031 SIG: 9

它不会停止我在该项目上的工作,但我知道为什么会发生这种情况对我很有帮助。很感激帮助。 My application has stopped (emulator only)

1 个答案:

答案 0 :(得分:2)

如果Android设备必须显示图像,则必须将整个图像加载到内存中。在您的情况下logoauxguide是高分辨率图像,因此整个图像将不适合设备内存。因此OutOfMemoryError将会发生。

但是具有更大内存的设备,在这种情况下,您的手机将能够将图像加载到内存中。

如果要显示图像,必须在任何图像编辑工具中降低其分辨率(640x480就可以了)。除此之外,您不希望更高的内存图像增加您的apk大小:)