![](/img/trans.png)
[英]Android Java | InflateException error inflating class when I call setContentView()
[英]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>
任何見解都將非常感激。 我查看了我在StackOverflow上可以找到的所有其他OOM問題,但找不到我的實際問題。 它們主要是關於嘗試創建一個圖像,而我的SplashScreen.java文件中調用.setContentView調用正好發生了。
setContentView(R.layout.splash);
所以我猜這是造成這個問題的背景? 但為什么它不會導致非gradle項目出現問題? 無論如何,感謝提前輸入。
這是title_image的規格:
您可以將此行添加到清單文件中的應用程序類聲明,以請求更大的堆大小。
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問題的更好選擇。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.