java .lang.OutOfMemoryError: Не удалось выделить 253513236 байт с 16774816 свободными байтами и 163 МБ до OOM
Я новичок в разработке игры на android в котором я использую images
для кнопки пуска, для фона, для того, как играть кнопку и некоторые images
на activity
. Тем не менее, я уменьшил размер изображения с 800 КБ до 35 КБ 85 КБ до сих пор , но столкнулся с этой ошибкой. До сих пор я искал об этой ошибке на inte rnet Я получил много решений, но НЕ ПОЛУЧИЛ ТОЧНО ДЛЯ ЭТОГО , Вот мой журнал
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.vikaskonaparthi.game, PID: 23126
java.lang.OutOfMemoryError: Failed to allocate a 253513236 byte allocation with 16774816 free bytes
and 163MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:613)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:446)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1080)
at android.content.res.Resources.createFromResourceStream(Resources.java:2952)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:2684)
at android.content.res.Resources.loadDrawable(Resources.java:2580)
at android.content.res.MiuiResources.loadDrawable(MiuiResources.java:398)
at android.content.res.TypedArray.getDrawable(TypedArray.java:872)
at android.widget.ImageView.<init>(ImageView.java:152)
at android.widget.ImageView.<init>(ImageView.java:140)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:72)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:68)
at android.support.v7.app.AppCompatViewInflater.createImageView(AppCompatViewInflater.java:182)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
at android.support.v7.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1266)
at android.support.v7.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1316)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:708)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:839)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:802)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:842)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:802)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:842)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:802)
at android.view.LayoutInflater.inflate(LayoutInflater.java:519)
at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.vikaskonaparthi.game.Start.onCreate(Start.java:37)
at android.app.Activity.performCreate(Activity.java:6355)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2440)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2547)
at android.app.ActivityThread.access$1100(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1398)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5603)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
Вот моя активность
public class Start extends AppCompatActivity {
private SoundPlayer sound;
AdView mAdview;
private InterstitialAd interstitialAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
MobileAds.initialize (this, getString (R.string.admob_app_id));
mAdview = (AdView)findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdview.loadAd(adRequest);
interstitialAd=new InterstitialAd(this);
interstitialAd.setAdUnitId("");
interstitialAd.loadAd(new AdRequest.Builder().build());
interstitialAd.setAdListener(new AdListener(){
@Override
public void onAdClosed() {
startActivity(new Intent(Start.this,Character.class));
interstitialAd.loadAd(new AdRequest.Builder().build());
}
});
AdView adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId("");
AppRate.with(this)
.setInstallDays(1)
.setLaunchTimes(3)
.setRemindInterval(2)
.monitor();
AppRate.showRateDialogIfMeetsConditions(this);
if (ContextCompat.checkSelfPermission(Start.this,
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(Start.this,
Manifest.permission.ACCESS_FINE_LOCATION)) {
ActivityCompat.requestPermissions(Start.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
} else {
ActivityCompat.requestPermissions(Start.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults){
switch (requestCode){
case 1: {
if (grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
if (ContextCompat.checkSelfPermission(Start.this,
Manifest.permission.ACCESS_FINE_LOCATION)==PackageManager.PERMISSION_GRANTED){
Toast.makeText(this, "Permission Granted", Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show();
}
return;
}
}
}
public void startGame(View view)
{
MediaPlayer mp = MediaPlayer.create(this,R.raw.click);//To play music in onCreate
mp.start();
if(interstitialAd.isLoaded())
{
interstitialAd.show();
}
else
{
startActivity(new Intent(getApplicationContext(),Character.class));
}
}
public void aboutGame(View view)
{
MediaPlayer mp = MediaPlayer.create(this,R.raw.click);//To play music in onCreate
mp.start();
startActivity(new Intent(getApplicationContext(),About.class));
}
@Override
public void onBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setMessage("Do you want to Exit?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//if user pressed "yes", then he is allowed to exit from application
finish();
moveTaskToBack(true);
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//if user select "No", just cancel this dialog and continue with app
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
Вот XML
файл макета
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Start"
android:gravity="bottom|center"
android:background="@drawable/b1"
android:weightSum="12">
<ImageView
android:id="@+id/pac"
android:layout_width="200dp"
android:layout_height="60dp"
android:src="@drawable/logo"
android:layout_gravity="center"/>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
<TableRow
android:gravity="center_vertical">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@drawable/strawberry"/>
</TableRow>
</TableLayout>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
<TableRow
android:gravity="center_vertical">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@drawable/cherries"/>
</TableRow>
</TableLayout>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
<TableRow
android:gravity="center_vertical">
<pl.droidsonroids.gif.GifImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@drawable/ene"/>
</TableRow>
</TableLayout>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
<TableRow
android:gravity="center_vertical">
<pl.droidsonroids.gif.GifImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@drawable/ene2"/>
</TableRow>
</TableLayout>
<Button
android:id="@+id/start"
android:layout_width="120dp"
android:layout_height="80dp"
android:layout_marginTop="100dp"
android:onClick="startGame"
android:background="@drawable/custom_start"
android:layout_marginBottom="100dp"/>
<Button
android:id="@+id/About"
android:layout_width="80dp"
android:layout_height="60dp"
android:layout_marginTop="-100dp"
android:onClick="aboutGame"
android:background="@drawable/custom_about"
android:layout_marginBottom="100dp"/>
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="bottom|center"
app:adSize="BANNER"
app:adUnitId="" />
</LinearLayout>
Буду признателен ЛЮБОЙ помощи, я пытаюсь решить ее больше недели.
Я не знаю, что мне теперь делать, я застрял с этой вещью , Но эта ошибка не отображается на устройствах Samsung и Nokia, работающих на 9 и 10 версиях, а только на Mi и Vivo . Я думаю, что они работают на версиях ниже android 9. Любая помощь будет оценена. Поскольку это game
Если я использую large_heap=true
в manifest.xml
, замедляя движение.