Android Studio + Genymotion:加载图片时应用崩溃

时间:2016-05-29 14:34:35

标签: android android-studio genymotion

我一直在尝试创建一个非常简单的应用,为此,我只使用了activity_main.xml(目前)。每当我放置ImageView,应用程序崩溃。没有ImageView,它运行完美。这是我使用ImageView设置的代码:

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000"
    android:orientation="vertical"
    android:weightSum="32">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="10">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Rock Paper Scissor"
            android:textSize="30dp"
            android:textColor="#FFF"
            android:gravity="center"
            android:layout_marginTop="20dp"
            android:id="@+id/title"
            android:layout_marginBottom="20dp"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:layout_marginBottom="30dp"
        android:id="@+id/iconLayout"
        android:layout_weight="10"
        >
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/rock"
            android:layout_gravity="center"
            android:adjustViewBounds="true"
            android:layout_weight="1"
            />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/paper"
            android:layout_gravity="center"
            android:adjustViewBounds="true"
            android:layout_weight="1"/>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/scissor"
            android:layout_gravity="center"
            android:adjustViewBounds="true"
            android:layout_weight="1"/>
    </LinearLayout>
    <LinearLayout
        android:layout_weight="10"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:textColor="#FFF"
            android:textSize="20sp"
            android:layout_margin="20dp"
            android:gravity="center"
            android:id="@+id/scoreID"
            android:text="Your Score: 0"
            android:layout_weight="1"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="Computer Score: 0"
            android:textColor="#FFF"
            android:gravity="center"
            android:textSize="20sp"
            android:layout_margin="20dp"
            android:layout_weight="1"
            />
    </LinearLayout>

</LinearLayout>

如果我从所有3中删除android:src然后运行,app运行正常。但随之而来的是,它崩溃了。这里也是我的logcat:

5.853 2171-2171/com.codedamn.m.firstapp I/Process: Sending signal. PID: 2171 SIG: 9
05-29 10:23:44.459 3861-3861/com.codedamn.m.firstapp I/art: Forcing collection of SoftReferences for 95MB allocation
05-29 10:23:44.463 3861-3861/com.codedamn.m.firstapp E/art: Throwing OutOfMemoryError "Failed to allocate a 100000012 byte allocation with 16777120 free bytes and 62MB until OOM"
05-29 10:23:44.463 3861-3861/com.codedamn.m.firstapp D/skia: --- allocation failed for scaled bitmap
05-29 10:23:44.464 3861-3861/com.codedamn.m.firstapp D/AndroidRuntime: Shutting down VM
05-29 10:23:44.464 3861-3861/com.codedamn.m.firstapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       Process: com.codedamn.m.firstapp, PID: 3861
                                                                       java.lang.OutOfMemoryError: Failed to allocate a 100000012 byte allocation with 16777120 free bytes and 62MB 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:973)
                                                                           at android.content.res.Resources.loadDrawableForCookie(Resources.java:2423)
                                                                           at android.content.res.Resources.loadDrawable(Resources.java:2330)
                                                                           at android.content.res.TypedArray.getDrawable(TypedArray.java:749)
                                                                           at android.widget.ImageView.<init>(ImageView.java:146)
                                                                           at android.widget.ImageView.<init>(ImageView.java:135)
                                                                           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:980)
                                                                           at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:1039)
                                                                           at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
                                                                           at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:725)
                                                                           at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
                                                                           at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
                                                                           at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
                                                                           at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
                                                                           at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
                                                                           at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
                                                                           at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                                                                           at com.codedamn.m.firstapp.MainActivity.onCreate(MainActivity.java:12)
                                                                           at android.app.Activity.performCreate(Activity.java:5933)
                                                                           at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
                                                                           at android.app.ActivityThread.access$800(ActivityThread.java:144)
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                           at android.os.Looper.loop(Looper.java:135)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5221)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at java.lang.reflect.Method.invoke(Method.java:372)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
05-29 10:24:44.974 3861-3861/com.codedamn.m.firstapp I/Process: Sending signal. PID: 3861 SIG: 9
05-29 10:27:41.400 5851-5851/com.codedamn.m.firstapp W/art: Verification of void android.support.v4.app.FragmentHostCallback.reportLoaderStart() took 176.439ms
05-29 10:27:41.451 5851-5851/com.codedamn.m.firstapp W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
05-29 10:27:42.040 5851-5851/com.codedamn.m.firstapp I/art: Forcing collection of SoftReferences for 95MB allocation
05-29 10:27:42.044 5851-5851/com.codedamn.m.firstapp E/art: Throwing OutOfMemoryError "Failed to allocate a 100000012 byte allocation with 16777216 free bytes and 62MB until OOM"
05-29 10:27:42.044 5851-5851/com.codedamn.m.firstapp D/skia: --- allocation failed for scaled bitmap
05-29 10:27:42.045 5851-5851/com.codedamn.m.firstapp D/AndroidRuntime: Shutting down VM
05-29 10:27:42.045 5851-5851/com.codedamn.m.firstapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       Process: com.codedamn.m.firstapp, PID: 5851
                                                                       java.lang.OutOfMemoryError: Failed to allocate a 100000012 byte allocation with 16777216 free bytes and 62MB 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:973)
                                                                           at android.content.res.Resources.loadDrawableForCookie(Resources.java:2423)
                                                                           at android.content.res.Resources.loadDrawable(Resources.java:2330)
                                                                           at android.content.res.TypedArray.getDrawable(TypedArray.java:749)
                                                                           at android.widget.ImageView.<init>(ImageView.java:146)
                                                                           at android.widget.ImageView.<init>(ImageView.java:135)
                                                                           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:980)
                                                                           at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:1039)
                                                                           at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
                                                                           at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:725)
                                                                           at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
                                                                           at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
                                                                           at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
                                                                           at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
                                                                           at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
                                                                           at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
                                                                           at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                                                                           at com.codedamn.m.firstapp.MainActivity.onCreate(MainActivity.java:12)
                                                                           at android.app.Activity.performCreate(Activity.java:5933)
                                                                           at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
                                                                           at android.app.ActivityThread.access$800(ActivityThread.java:144)
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                           at android.os.Looper.loop(Looper.java:135)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5221)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at java.lang.reflect.Method.invoke(Method.java:372)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
05-29 10:31:26.106 5851-5851/com.codedamn.m.firstapp I/Process: Sending signal. PID: 5851 SIG: 9

1 个答案:

答案 0 :(得分:1)

我猜你正在加载的图片非常庞大,因为你遇到了内存不足的错误。

尝试使用较小的图像,看看是否有帮助。阅读android开发者网站来处理这些问题。