在java gradle android项目中获取setContentView调用的OOM问题
我使用Intellij idea,我让这个项目在没有gradle的情况下运行,运行良好,功能正常。将它迁移到gradle项目(只需创建一个新的gradle android项目并复制代码),现在我得到了这个堆栈跟踪:(抱歉,它太小了…) 以下是我的布局xml:在java gradle android项目中获取setContentView调用的OOM问题,java,android,intellij-idea,out-of-memory,android-gradle-plugin,Java,Android,Intellij Idea,Out Of Memory,Android Gradle Plugin,我使用Intellij idea,我让这个项目在没有gradle的情况下运行,运行良好,功能正常。将它迁移到gradle项目(只需创建一个新的gradle android项目并复制代码),现在我得到了这个堆栈跟踪:(抱歉,它太小了…) 以下是我的布局xml: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/andro
<?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>
舱单:
<?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>
所以我猜是背景造成了这个问题?但为什么它不会在非gradle项目上引起问题呢?不管怎样,谢谢你提前提供的信息
以下是标题图片的规格:
您可以将此行添加到清单文件中的应用程序类声明中,以请求更大的堆大小
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问题。title\u image有多大(MB)?“我的问题是在调用setContentView时发生的”-是的,但最终会膨胀一个布局,以代表您创建视图-其中一个恰巧有一个名为title\u image的可绘制资源。由于这是您的启动屏幕,因此可以安全地假设您已经很长时间没有运行应用程序了。可能是title_image位图太大了。title_image.png是100 kb,所以garima的答案目前还有效,但我想弄清楚,如果我的图像只有100 kb,为什么它会这么大…你的修复程序对临时garima有效。。。非常感谢!不过,我现在在这个应用程序上遇到了各种各样的问题。背景图像未显示在启动屏幕上,按钮未按应有的方式运行…:(但这就是我能弄明白的所有事情。再次感谢你的帮助!嗨@Garima Mathur你认为当我的应用程序部署到手机上时,增加堆大小会减慢我的应用程序的速度吗?我现在无法在手机上说出来,但我只完成了启动屏幕和一些菜单,没有图形密集型或处理器密集型的功能。没有ot负责您的应用程序速度减慢。这可能是另一个原因,如服务器响应延迟可能是原因,或者可能是应用程序中使用了更高分辨率的drawable。
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>