Android应用程序崩溃当我添加ImageView时

时间:2016-03-27 16:45:29

标签: android android-studio android-5.0-lollipop

我正在学习Android,我制作了一个简单的代码并在我的SONY Xperia Z Ultra上使用Android 5.0进行了测试 由于未知原因应用程序在我启动后立即停止,我不知道为什么!

    <?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.husseinalbehary.alpha.MainActivity">

    <TextView
        android:id="@+id/first"
        android:text="League"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/second"
        android:layout_toRightOf="@id/first"
        android:text=" Songs"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ImageView
        android:src="@drawable/warsong"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

如果这是一个愚蠢的问题,我真的很抱歉,但我认为我的代码很好。

注意:

  • Android Studio中的内置预览功能可显示该应用。 是的,我在手机上测试了其他代码,但是他们工作了。
  • 当我删除I​​mageView代码时,我的应用程序似乎工作正常。

如果它有帮助,这是Logcat:

    03-27 18:46:28.627 13022-13022/? I/art: Late-enabling -Xcheck:jni
03-27 18:46:28.797 13022-13022/com.example.husseinalbehary.alpha W/ResourceType: Found multiple library tables, ignoring...
03-27 18:46:29.801 13022-13022/com.example.husseinalbehary.alpha I/art: Alloc sticky concurrent mark sweep GC freed 3218(289KB) AllocSpace objects, 0(0B) LOS objects, 33% free, 7MB/10MB, paused 463us total 7.347ms
03-27 18:46:29.811 13022-13032/com.example.husseinalbehary.alpha W/art: Suspending all threads took: 8.480ms
03-27 18:46:29.812 13022-13022/com.example.husseinalbehary.alpha I/art: Alloc partial concurrent mark sweep GC freed 64(15KB) AllocSpace objects, 0(0B) LOS objects, 39% free, 7MB/12MB, paused 757us total 9.856ms
03-27 18:46:29.824 13022-13022/com.example.husseinalbehary.alpha I/art: Alloc concurrent mark sweep GC freed 157(18KB) AllocSpace objects, 0(0B) LOS objects, 39% free, 7MB/12MB, paused 444us total 11.895ms
03-27 18:46:29.825 13022-13022/com.example.husseinalbehary.alpha I/art: Forcing collection of SoftReferences for 197MB allocation
03-27 18:46:29.844 13022-13022/com.example.husseinalbehary.alpha I/art: Alloc concurrent mark sweep GC freed 11(344B) AllocSpace objects, 0(0B) LOS objects, 39% free, 7MB/12MB, paused 2.548ms total 19.180ms
03-27 18:46:29.844 13022-13022/com.example.husseinalbehary.alpha E/art: Throwing OutOfMemoryError "Failed to allocate a 206720012 byte allocation with 5040122 free bytes and 184MB until OOM"
03-27 18:46:29.845 13022-13022/com.example.husseinalbehary.alpha D/skia: --- allocation failed for scaled bitmap
03-27 18:46:29.854 13022-13022/com.example.husseinalbehary.alpha D/AndroidRuntime: Shutting down VM
03-27 18:46:29.855 13022-13022/com.example.husseinalbehary.alpha E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                   Process: com.example.husseinalbehary.alpha, PID: 13022
                                                                                   java.lang.OutOfMemoryError: Failed to allocate a 206720012 byte allocation with 5040122 free bytes and 184MB 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:989)
                                                                                       at android.content.res.Resources.loadDrawableForCookie(Resources.java:2643)
                                                                                       at android.content.res.Resources.loadDrawable(Resources.java:2525)
                                                                                       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: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:725)
                                                                                       at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
                                                                                       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:276)
                                                                                       at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
                                                                                       at com.example.husseinalbehary.alpha.MainActivity.onCreate(MainActivity.java:11)
                                                                                       at android.app.Activity.performCreate(Activity.java:5933)
                                                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
                                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
                                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2405)
                                                                                       at android.app.ActivityThread.access$800(ActivityThread.java:149)
                                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)
                                                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                       at android.os.Looper.loop(Looper.java:211)
                                                                                       at android.app.ActivityThread.main(ActivityThread.java:5317)
                                                                                       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:1016)
                                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)

感谢。

1 个答案:

答案 0 :(得分:1)

之所以会发生这种情况,是因为您的图片太大而且出现了错误。你必须使用较小的图像。 见link

您可以从xml中删除src并按代码执行。也许这段代码可以帮助你:

private void setScaledImage(ImageView imageView, final int resId) {
    final ImageView iv = imageView;
    ViewTreeObserver viewTreeObserver = iv.getViewTreeObserver();
    viewTreeObserver.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        public boolean onPreDraw() {
            iv.getViewTreeObserver().removeOnPreDrawListener(this);
            int imageViewHeight = iv.getMeasuredHeight();
            int imageViewWidth = iv.getMeasuredWidth();
            iv.setImageBitmap(
                    decodeSampledBitmapFromResource(getResources(),
                            resId, imageViewWidth, imageViewHeight));
            return true;
        }
    });
}

private static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
                                                     int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds = true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}

private static int calculateInSampleSize(
        BitmapFactory.Options options, int reqWidth, int reqHeight) {

    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}

另见link