这个我的游戏一切正常,除非将图像作为背景添加到我的按钮,我已经尝试了stackoverflow中的所有答案,但没有工作,错误消除了未设置背景图像。 以下是我的代码
Logcat
10-30 19:38:42.473 17302-17302/com.danaraddi.quickmath W/art: Throwing OutOfMemoryError "Failed to allocate a 16777228 byte allocation with 4194304 free bytes and 5MB until OOM"
10-30 19:38:42.473 17302-17302/com.danaraddi.quickmath D/skia: --- allocation failed for scaled bitmap
10-30 19:38:42.476 17302-17302/com.danaraddi.quickmath W/System.err: android.view.InflateException: Binary XML file line #91: Binary XML file line #91: Error inflating class <unknown>
10-30 19:38:42.476 17302-17302/com.danaraddi.quickmath W/System.err: at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
10-30 19:38:42.478 17302-17302/com.danaraddi.quickmath W/System.err: at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
10-30 19:38:42.478 17302-17302/com.danaraddi.quickmath W/System.err: at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
10-30 19:38:42.478 17302-17302/com.danaraddi.quickmath W/System.err: at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393)
10-30 19:38:42.478 17302-17302/com.danaraddi.quickmath W/System.err: at android.app.Activity.setContentView(Activity.java:2166)
10-30 19:38:42.478 17302-17302/com.danaraddi.quickmath W/System.err: at com.danaraddi.quickmath.activities.ScoreActivity.onCreate(ScoreActivity.java:56)
10-30 19:38:42.478 17302-17302/com.danaraddi.quickmath W/System.err: at android.app.Activity.performCreate(Activity.java:6237)
10-30 19:38:42.478 17302-17302/com.danaraddi.quickmath W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
10-30 19:38:42.478 17302-17302/com.danaraddi.quickmath W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
10-30 19:38:42.478 17302-17302/com.danaraddi.quickmath W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
10-30 19:38:42.478 17302-17302/com.danaraddi.quickmath W/System.err: at android.app.ActivityThread.-wrap11(ActivityThread.java)
10-30 19:38:42.478 17302-17302/com.danaraddi.quickmath W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
10-30 19:38:42.478 17302-17302/com.danaraddi.quickmath W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
10-30 19:38:42.478 17302-17302/com.danaraddi.quickmath W/System.err: at android.os.Looper.loop(Looper.java:148)
10-30 19:38:42.478 17302-17302/com.danaraddi.quickmath W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5417)
10-30 19:38:42.478 17302-17302/com.danaraddi.quickmath W/System.err: at java.lang.reflect.Method.invoke(Native Method)
10-30 19:38:42.478 17302-17302/com.danaraddi.quickmath W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
10-30 19:38:42.478 17302-17302/com.danaraddi.quickmath W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-30 19:38:42.478 17302-17302/com.danaraddi.quickmath W/System.err: Caused by: android.view.InflateException: Binary XML file line #91: Error inflating class <unknown>
10-30 19:38:42.478 17302-17302/com.danaraddi.quickmath W/System.err: at android.view.LayoutInflater.createView(LayoutInflater.java:645)
10-30 19:38:42.478 17302-17302/com.danaraddi.quickmath W/System.err: at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
10-30 19:38:42.478 17302-17302/com.danaraddi.quickmath W/System.err: at android.view.LayoutInflater.onCreateView(LayoutInflater.java:694)
10-30 19:38:42.478 17302-17302/com.danaraddi.quickmath W/System.err: at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:762)
10-30 19:38:42.478 17302-17302/com.danaraddi.quickmath W/System.err: at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
10-30 19:38:42.478 17302-17302/com.danaraddi.quickmath W/System.err: at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
10-30 19:38:42.478 17302-17302/com.danaraddi.quickmath W/System.err: at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
10-30 19:38:42.478 17302-17302/com.danaraddi.quickmath W/System.err: at android.view.LayoutInflater.rInflate(LayoutInflater.java:838)
10-30 19:38:42.478 17302-17302/com.danaraddi.quickmath W/System.err: at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
10-30 19:38:42.483 17302-17302/com.danaraddi.quickmath W/System.err: at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
10-30 19:38:42.483 17302-17302/com.danaraddi.quickmath W/System.err: ... 17 more
10-30 19:38:42.483 17302-17302/com.danaraddi.quickmath W/System.err: Caused by: java.lang.reflect.InvocationTargetException
10-30 19:38:42.483 17302-17302/com.danaraddi.quickmath W/System.err: at java.lang.reflect.Constructor.newInstance(Native Method)
10-30 19:38:42.483 17302-17302/com.danaraddi.quickmath W/System.err: at android.view.LayoutInflater.createView(LayoutInflater.java:619)
10-30 19:38:42.483 17302-17302/com.danaraddi.quickmath W/System.err: ... 26 more
10-30 19:38:42.483 17302-17302/com.danaraddi.quickmath W/System.err: Caused by: java.lang.OutOfMemoryError: Failed to allocate a 16777228 byte allocation with 4194304 free bytes and 5MB until OOM
10-30 19:38:42.484 17302-17302/com.danaraddi.quickmath W/System.err: at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
10-30 19:38:42.484 17302-17302/com.danaraddi.quickmath W/System.err: at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
10-30 19:38:42.484 17302-17302/com.danaraddi.quickmath W/System.err: at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
10-30 19:38:42.484 17302-17302/com.danaraddi.quickmath W/System.err: at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
10-30 19:38:42.484 17302-17302/com.danaraddi.quickmath W/System.err: at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1080)
10-30 19:38:42.484 17302-17302/com.danaraddi.quickmath W/System.err: at android.content.res.Resources.loadDrawableForCookie(Resources.java:2635)
10-30 19:38:42.484 17302-17302/com.danaraddi.quickmath W/System.err: at android.content.res.Resources.loadDrawable(Resources.java:2540)
10-30 19:38:42.484 17302-17302/com.danaraddi.quickmath W/System.err: at android.content.res.TypedArray.getDrawable(TypedArray.java:870)
10-30 19:38:42.484 17302-17302/com.danaraddi.quickmath W/System.err: at android.graphics.drawable.StateListDrawable.inflateChildElements(StateListDrawable.java:170)
10-30 19:38:42.484 17302-17302/com.danaraddi.quickmath W/System.err: at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:115)
10-30 19:38:42.484 17302-17302/com.danaraddi.quickmath W/System.err: at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1215)
10-30 19:38:42.484 17302-17302/com.danaraddi.quickmath W/System.err: at android.graphics.drawable.Drawable.createFromXml(Drawable.java:1124)
10-30 19:38:42.484 17302-17302/com.danaraddi.quickmath W/System.err: at android.content.res.Resources.loadDrawableForCookie(Resources.java:2630)
10-30 19:38:42.484 17302-17302/com.danaraddi.quickmath W/System.err: at android.content.res.Resources.loadDrawable(Resources.java:2540)
10-30 19:38:42.484 17302-17302/com.danaraddi.quickmath W/System.err: at android.content.res.TypedArray.getDrawable(TypedArray.java:870)
10-30 19:38:42.484 17302-17302/com.danaraddi.quickmath W/System.err: at android.view.View.<init>(View.java:3948)
10-30 19:38:42.484 17302-17302/com.danaraddi.quickmath W/System.err: at android.widget.TextView.<init>(TextView.java:677)
10-30 19:38:42.484 17302-17302/com.danaraddi.quickmath W/System.err: at android.widget.Button.<init>(Button.java:109)
10-30 19:38:42.484 17302-17302/com.danaraddi.quickmath W/System.err: at android.widget.Button.<init>(Button.java:105)
10-30 19:38:42.485 17302-17302/com.danaraddi.quickmath W/System.err: at android.widget.Button.<init>(Button.java:101)
10-30 19:38:42.485 17302-17302/com.danaraddi.quickmath W/System.err: ... 28 more
10-30 19:38:42.531 17302-17317/com.danaraddi.quickmath W/EGL_emulation: eglSurfaceAttrib not implemented
10-30 19:38:42.531 17302-17317/com.danaraddi.quickmath W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x9fbbf0e0, error=EGL_SUCCESS
10-30 19:38:42.728 17302-17317/com.danaraddi.quickmath W/EGL_emulation: eglSurfaceAttrib not implemented
10-30 19:38:42.728 17302-17317/com.danaraddi.quickmath W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x9fb7b280, error=EGL_SUCCESS
10-30 19:38:43.227 17302-17317/com.danaraddi.quickmath E/Surface: getSlotFromBufferLocked: unknown buffer: 0x9fb75650
10-30 19:38:46.073 17302-17317/com.danaraddi.quickmath E/Surface: getSlotFromBufferLocked: unknown buffer: 0x9fb766f0
布局XML
<LinearLayout 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"
android:background="@drawable/bg_green"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:weightSum="4"
tools:context="com.danaraddi.quickmath.activities.ScoreActivity">
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="GAME OVER"
android:textSize="40sp" />
</LinearLayout>
<RelativeLayout
android:layout_weight="1.5"
android:layout_width="match_parent"
android:background="@drawable/button_normal"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_score_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SCORE"
android:layout_marginTop="75dp"
android:gravity="center"
android:textSize="25sp"
/>
<TextView
android:id="@+id/tv_score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SCORE"
android:gravity="center"
android:layout_below="@+id/tv_score_title"
android:textSize="20sp"
/>
<TextView
android:layout_below="@+id/tv_score"
android:id="@+id/tv_bestscore_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="HIGH SCORE"
android:layout_marginTop="35dp"
android:gravity="center"
android:textSize="25sp"
/>
<TextView
android:id="@+id/tv_bestscore"
android:layout_below="@+id/tv_bestscore_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SCORE"
android:gravity="center"
android:textSize="20sp"
/>
</RelativeLayout>
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="wrap_content">
<Button
android:textSize="20sp"
android:text="SHARE"
android:background="@drawable/button"
android:layout_width="100dp"
android:layout_height="50dp" />
</LinearLayout>
<LinearLayout
android:layout_weight=".5"
android:layout_width="match_parent"
android:orientation="horizontal"
android:gravity="center"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn_return_home"
android:layout_margin="5dp"
android:background="@drawable/btn_home"
android:layout_width="75dp"
android:layout_height="75dp" />
<Button
android:id="@+id/btn_leaderboards"
android:layout_margin="5dp"
android:background="@drawable/btn_game"
android:layout_width="75dp"
android:layout_height="75dp" />
<Button
android:id="@+id/btn_reload"
android:layout_margin="5dp"
android:background="@drawable/btn_reload"
android:layout_width="75dp"
android:layout_height="75dp"
/>
</LinearLayout>
</LinearLayout>
Java文件
package com.danaraddi.quickmath.activities;
//import com.google.android.gms.games.Games;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import com.danaraddi.quickmath.GameServices.LeaderBoardHelper;
import com.danaraddi.quickmath.R;
import com.danaraddi.quickmath.Utility.PreferenceManager;
import com.google.android.gms.games.Games;
import com.google.example.games.basegameutils.BaseGameActivity;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
public class ScoreActivity extends BaseGameActivity {
int BACK_KEY_PRESS_COUNT = 0;
PreferenceManager preferenceManager;
// Textview Objects
@InjectView(R.id.tv_score)
TextView tvScore;
@InjectView(R.id.tv_bestscore)
TextView tvBestScore;
@OnClick(R.id.btn_reload)
void btn_reload_click() {
reloadGame();
}
@OnClick(R.id.btn_leaderboards)
void btn_gameservices_click() {
googleGameServices();
}
@OnClick(R.id.btn_return_home)
void btn_home_click() {
returnHome();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// error occuring
try {
setContentView(R.layout.activity_score);
ButterKnife.inject(this);
preferenceManager = new PreferenceManager(this);
setScores();
}catch (Exception e){
Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
@Override
public void onBackPressed() {
returnHome();
}
// kill activity
private void returnHome(){
this.finish();
}
//reload game
private void reloadGame(){
startActivity(new Intent(this, GameActivity.class));
this.finish();
}
// Game Services
private void googleGameServices(){
startActivityForResult(Games.Leaderboards.getLeaderboardIntent(
getApiClient(), getString(R.string.quick_math_leaderboard)),
2);
}
private void setScores(){
tvScore.setText(String.valueOf( preferenceManager.returnFromPreferenceLong("SCORE")));
tvBestScore.setText(String.valueOf( preferenceManager.returnFromPreferenceLong("HIGHSCORE")));
}
@Override
public void onSignInFailed() {
}
@Override
public void onSignInSucceeded() {
LeaderBoardHelper.submitScore(getApiClient(), preferenceManager.returnFromPreferenceLong("HIGHSCORE"));
}
}
*我已存储,可绘制文件夹中的背景图像,以及用于按钮的其他图像甚至存储在可绘制文件夹中,按钮图像为512 * 512px分辨率*
我已经解决了这个问题,我已经缩小了图像分辨率表格 512 * 512到96 * 96来自此处建议的指南
答案 0 :(得分:0)
您正在尝试分配一个16,777,228字节的内存块。在许多设备上你不会有一块很大的内存块,而且有时候你没有任何设备上的内存块。
您要设置的背景图片太大。基于内存分配,它相当于一个2048像素×2048像素的方形图像,比我所知道的任何Android设备屏幕都要大。
您需要使此背景图像的分辨率较低,或者为背景使用其他解决方案,以避免使用位图作为背景图像。
答案 1 :(得分:0)
除了commonsWare的答案:
如果您认为图像尺寸合适,则可能将其存储在错误的文件夹中。如果您不希望缩放它,请尝试drawable-nodpi
。
或者,对于较小的设备,您可能希望在相关的drawable-**dpi
文件夹中提供尺寸合适的图像。