java gradle android 项目中的 setContentView 调用出现 OOM 问题

Getting an OOM issue on the setContentView call in a java gradle android project

我正在使用 Intellij-idea,并且我在没有 gradle、运行 的情况下运行了这个项目,而且运行良好。将它迁移到一个 gradle 项目(通过创建一个新的 gradle android 项目并复制代码)现在我得到了这个堆栈跟踪:(抱歉它太小了.. .)

这是我的布局xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
    >
<ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/blue_background"
        android:contentDescription="@string/backgroundDesc"
        android:src="@drawable/title_image"
        android:paddingTop="5dp"
        android:paddingStart="7dp"
        android:paddingEnd="5dp"
        android:paddingBottom="5dp"
        />
<TextView
        android:id="@+id/writtenBy"
        android:text="@string/gameBy"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_gravity="end"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:gravity="end"
        android:paddingStart="0dp"
        android:paddingEnd="10dp"
        android:paddingBottom="10dp"
        />
</RelativeLayout>

SplashScreen.java:

package com.thenewjonathan.gloryblades;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

public class SplashScreen extends Activity
{
    TextView writtenBy;
    private static int TIME_OUT = 3000;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.splash);

        writtenBy = (TextView) findViewById(R.id.writtenBy);

        new Handler().postDelayed(new Runnable()
        {
            @Override
            public void run()
            {
                Intent i = new Intent(SplashScreen.this, MainMenu.class);
                startActivity(i);

                finish();
            }
        }, TIME_OUT);
    }
}

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.thenewjonathan.gloryblades" >

    <uses-permission
            android:name="android.permission.internet"/>
    <uses-permission
            android:name="android.permission.access_network_state"/>
    <uses-permission
            android:name="android.permission.write_external_storage"/>

    <uses-sdk
            android:minSdkVersion="17"/>
    <application
            android:label="@string/app_name"
            android:allowBackup="true"
            android:icon="@drawable/title_image"
            >
        <!--<meta-data
                android:name="com.google.android.gms.version"
                android:value="@integer/google_play_services_version"
                />-->
        <activity
                android:name="com.thenewjonathan.gloryblades.SplashScreen"
                android:label="@string/app_name"
                android:screenOrientation="portrait"
                >
            <intent-filter>
                <action
                        android:name="android.intent.action.MAIN"/>
                <category
                        android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

        <activity
                android:name="com.thenewjonathan.gloryblades.MainMenu"
                android:label="Main Menu"
                android:screenOrientation="portrait"
                >
        </activity>
        <activity
                android:name="com.thenewjonathan.gloryblades.SetUpNewGame"
                android:screenOrientation="portrait"
                >
        </activity>
        <activity
                android:name="com.thenewjonathan.gloryblades.StartExistingGame"
                android:screenOrientation="portrait"
                >
        </activity>
        <activity
                android:name="com.thenewjonathan.gloryblades.StoryBoard"/>
        <!--<activity
                android:name="com.android.gms.ads.AdActivity"
                android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
                android:theme="@android:style/Theme.Translucent" />-->
    </application>

</manifest>

如有任何见解,我们将不胜感激。我查看了我可以在 Whosebug 此处找到的所有其他 OOM 问题,但找不到我的实际问题。他们主要是关于尝试创建图像,而我的只是在我的 SplashScreen.java 文件中调用 .setContentView 时发生。

setContentView(R.layout.splash);

所以我猜是背景导致了这个问题?但为什么它不会在非 gradle 项目上引起问题?无论如何,感谢您提前输入。

title_image 的规格如下:

您可以将此行添加到应用程序 class 声明到清单文件中以请求更大的堆大小。

android:largeHeap="true" 

更改清单文件代码。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.thenewjonathan.gloryblades" >

    <uses-permission
            android:name="android.permission.internet"/>
    <uses-permission
            android:name="android.permission.access_network_state"/>
    <uses-permission
            android:name="android.permission.write_external_storage"/>

    <uses-sdk
            android:minSdkVersion="17"/>
    <application
            android:label="@string/app_name"
            android:allowBackup="true"
            android:icon="@drawable/title_image"
            android:largeHeap="true">
        <!--<meta-data
                android:name="com.google.android.gms.version"
                android:value="@integer/google_play_services_version"
                />-->
        <activity
                android:name="com.thenewjonathan.gloryblades.SplashScreen"
                android:label="@string/app_name"
                android:screenOrientation="portrait"
                >
            <intent-filter>
                <action
                        android:name="android.intent.action.MAIN"/>
                <category
                        android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

        <activity
                android:name="com.thenewjonathan.gloryblades.MainMenu"
                android:label="Main Menu"
                android:screenOrientation="portrait"
                >
        </activity>
        <activity
                android:name="com.thenewjonathan.gloryblades.SetUpNewGame"
                android:screenOrientation="portrait"
                >
        </activity>
        <activity
                android:name="com.thenewjonathan.gloryblades.StartExistingGame"
                android:screenOrientation="portrait"
                >
        </activity>
        <activity
                android:name="com.thenewjonathan.gloryblades.StoryBoard"/>
        <!--<activity
                android:name="com.android.gms.ads.AdActivity"
                android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
                android:theme="@android:style/Theme.Translucent" />-->
    </application>

</manifest>

解决您的 OOM 问题会是更好的选择。