一旦我在线性布局中添加2个相对布局,应用程序就会崩溃 - 为什么?

时间:2016-12-17 16:55:14

标签: android android-layout

所以,这是重点:

我正在训练并在Android开发中迈出第一步,在我自己缩小了一些错误之后,我终于找到了一个我无法找到解决方案的方法。

我想创建一个应用程序,显示两个水平分割屏幕的图片。因此,我创建了一个垂直方向的LinearLayout,并在其中添加了两个RelativeLayout。我编译它,构建显然完成但是一旦应用程序尝试启动我的手机(三星S5 Neo),它说它停止工作,我再次登陆我的桌面。 >。<此外,在提示出现之前 - 是的,我已经启动了一个用于测试目的的新项目,并且发生了同样的错误。此外,当我删除这些代码行时,该应用程序在我的手机上工作得很好。那就是我的代码:

<?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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="removedbecausenotyourbusiness:P">

    <RelativeLayout
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_width="match_parent">

        <ImageView
            android:id="@+id/background2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/background2"
            android:scaleType="centerCrop"/>
    </RelativeLayout>

    <RelativeLayout
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_width="match_parent">

        <ImageView
            android:id="@+id/background1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/background1"
            android:scaleType="centerCrop"/>
    </RelativeLayout>

</LinearLayout>

另外,我想知道我是否能以某种方式从我的手机上获取日志,以确定应用程序是否停止工作。我在哪里可以得到这个?

编辑:我相信我找到了你要求我的东西。

  

12-17 19:27:18.751 11830-11830 /? E / Zygote:v2 12-17 19:27:18.751   11830-11830 /? E / Zygote:accessInfo:0 12-17 19:27:20.211   11830-11830 / com.example.marco E / AndroidRuntime:FATAL EXCEPTION:main                                                                                           过程:com.example.marco,PID:11830                                                                                           java.lang.OutOfMemoryError:无法分配231444012字节   分配4182352个空闲字节和125MB直到OOM                                                                                               at dalvik.system.VMRuntime.newNonMovableArray(Native Method)                                                                                               在android.graphics.BitmapFactory.nativeDecodeAsset(本机方法)                                                                                               在android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:856)                                                                                               在   android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:675)                                                                                               在   android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:2228)                                                                                               在   android.content.res.Resources.loadDrawableForCookie(Resources.java:4211)                                                                                               在android.content.res.Resources.loadDrawable(Resources.java:4085)                                                                                               在android.content.res.Resources.loadDrawable(Resources.java:3935)                                                                                               在android.content.res.TypedArray.getDrawable(TypedArray.java:886)                                                                                               在android.widget.ImageView。(ImageView.java:157)                                                                                               在android.widget.ImageView。(ImageView.java:145)                                                                                               在   android.support.v7.widget.AppCompatImageView。(AppCompatImageView.java:60)                                                                                               在   android.support.v7.widget.AppCompatImageView。(AppCompatImageView.java:56)                                                                                               在   android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)                                                                                               在   android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1021)                                                                                               在   android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1080)                                                                                               在   android.support.v4.view.LayoutInflaterCompatHC $ FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:47)                                                                                               在   android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:758)                                                                                               在   android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716)                                                                                               在android.view.LayoutInflater.rInflate(LayoutInflater.java:847)                                                                                               在   android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)                                                                                               在android.view.LayoutInflater.rInflate(LayoutInflater.java:855)                                                                                               在   android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)                                                                                               在android.view.LayoutInflater.inflate(LayoutInflater.java:527)                                                                                               在android.view.LayoutInflater.inflate(LayoutInflater.java:429)                                                                                               在android.view.LayoutInflater.inflate(LayoutInflater.java:380)                                                                                               在   android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:288)                                                                                               在   android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:143)                                                                                               在com.example.marco.MainActivity.onCreate(MainActivity.java:11)                                                                                               在android.app.Activity.performCreate(Activity.java:6877)                                                                                               在   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)                                                                                               在   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3208)                                                                                               在   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3351)                                                                                               在android.app.ActivityThread.access $ 1100(ActivityThread.java:222)                                                                                               在   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1796)                                                                                               在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                               在android.os.Looper.loop(Looper.java:158)                                                                                               在android.app.ActivityThread.main(ActivityThread.java:7230)                                                                                               at java.lang.reflect.Method.invoke(Native Method)                                                                                               在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1230)                                                                                               在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作来修复图像中的OutOfMemoryError:

在AndroidManifest文件的顶部application下添加这些行:

android:hardwareAccelerated="false"

android:largeHeap="true"

所以AndroidManifest的顶部看起来会像这样添加两行:

    <application
        android:hardwareAccelerated="false"  //added
        android:largeHeap="true"             //added
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">

如果没有解决问题,那么您需要压缩图像文件。您可以在https://tinyjpg.com/等网站上执行此操作。这可能只是你的图像太大的问题!

下次遇到这样的问题时,您需要使用崩溃日志/堆栈跟踪中收到的错误并搜索解决方案。在您的情况下,您可以找到:Android:java.lang.OutOfMemoryError: Failed to allocate a 23970828 byte allocation with 2097152 free bytes and 2MB until OOM