OutOfMemoryError - Android

0

Witam, stworzyłem sobie prostą aplikację która oblicza BMI, wszystko jest ok aplikacja dobrze działa na emulatorze, ale po wygenerowaniu APK i wgraniu tego na telefon pojawi mi się taki błąd jak poniżej. Czy ktoś z was miał do czynienia z czymś takim ? Jak można się tego pozbyć ?

java.lang.RuntimeException: Unable to start activity ComponentInfo{marczuk.com.bmi/marczuk.com.bmi.Splashscreen}: android.view.InflateException: Binary XML file line #11: Error inflating class android.widget.ImageView
	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2464)
	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2526)
	at android.app.ActivityThread.access$800(ActivityThread.java:169)
	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
	at android.os.Handler.dispatchMessage(Handler.java:111)
	at android.os.Looper.loop(Looper.java:194)
	at android.app.ActivityThread.main(ActivityThread.java:5549)
	at java.lang.reflect.Method.invoke(Native Method)
	at java.lang.reflect.Method.invoke(Method.java:372)
	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)
Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class android.widget.ImageView
	at android.view.LayoutInflater.createView(LayoutInflater.java:637)
	at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
	at android.view.LayoutInflater.onCreateView(LayoutInflater.java:686)
	at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:745)
	at android.view.LayoutInflater.rInflate(LayoutInflater.java:810)
	at android.view.LayoutInflater.inflate(LayoutInflater.java:508)
	at android.view.LayoutInflater.inflate(LayoutInflater.java:418)
	at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
	at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:400)
	at android.app.Activity.setContentView(Activity.java:2168)
	at marczuk.com.bmi.Splashscreen.onCreate(Splashscreen.java:27)
	at android.app.Activity.performCreate(Activity.java:5975)
	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
	... 10 more
Caused by: java.lang.reflect.InvocationTargetException
	at java.lang.reflect.Constructor.newInstance(Native Method)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
	at android.view.LayoutInflater.createView(LayoutInflater.java:611)
	... 23 more
Caused by: java.lang.OutOfMemoryError: Failed to allocate a 74649612 byte allocation with 16777168 free bytes and 56MB until OOM
	at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
	at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
	at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:655)
	at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:488)
	at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:978)
	at android.content.res.Resources.createFromResourceStream(Resources.java:3026)
	at android.content.res.Resources.loadDrawableForCookie(Resources.java:2721)
	at android.content.res.Resources.loadDrawable(Resources.java:2607)
	at android.content.res.MiuiResources.loadDrawable(MiuiResources.java:393)
	at android.content.res.TypedArray.getDrawable(TypedArray.java:751)
	at android.view.View.<init>(View.java:3777)
	at android.widget.ImageView.<init>(ImageView.java:139)
	at android.widget.ImageView.<init>(ImageView.java:135)
	at android.widget.ImageView.<init>(ImageView.java:131)
	... 26 more
0

A co robisz w tym marczuk.com.bmi.Splashscreen? W szczególności co za obrazki próbujesz tam ładować? Bo błąd mówi że próbujesz zaalokować 75MB pamięci a urządzenie ma tylko 16 dostępnej ;]

0

To jest moja klasa SplashScreen

package marczuk.com.bmi;

import android.app.Activity;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class Splashscreen extends Activity {
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        Window window = getWindow();
        window.setFormat(PixelFormat.RGBA_8888);
    }
   
    Thread splashTread;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splashscreen);
        StartAnimations();
    }
    private void StartAnimations() {
        Animation anim = AnimationUtils.loadAnimation(this, R.anim.alpha);
        anim.reset();
        LinearLayout l=(LinearLayout) findViewById(R.id.lin_lay);
        l.clearAnimation();
        l.startAnimation(anim);

        anim = AnimationUtils.loadAnimation(this, R.anim.translate);
        anim.reset();
        ImageView iv = (ImageView) findViewById(R.id.splash);
        iv.clearAnimation();
        iv.startAnimation(anim);

        splashTread = new Thread() {
            @Override
            public void run() {
                try {
                    int waited = 0;
                    // Splash screen pause time
                    while (waited < 6500) {
                        sleep(100);
                        waited += 100;
                    }
                    Intent intent = new Intent(Splashscreen.this,
                            MainActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                    startActivity(intent);
                    Splashscreen.this.finish();
                } catch (InterruptedException e) {
                    // do nothing
                } finally {
                    Splashscreen.this.finish();
                }

            }
        };
        splashTread.start();

    }

}

A to jest mój Main

package marczuk.com.bmi;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class MainActivity extends AppCompatActivity {


    private EditText weight;
    private EditText height;
    private TextView result;

    private GoogleApiClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        weight = (EditText) findViewById(R.id.weight);
        height = (EditText) findViewById(R.id.height);
        result = (TextView) findViewById(R.id.result);



        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }

    public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
        
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            
            while ((halfHeight / inSampleSize) > reqHeight
                    && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }

        return inSampleSize;
    }

    public void calculateBMI(View v) {
        String heightStr = height.getText().toString();
        String weightStr = weight.getText().toString();

        if (heightStr != null && !"".equals(heightStr)) {
            float heightValue = Float.parseFloat(heightStr) / 100;
            float weightValue = Float.parseFloat(weightStr);

            float bmi = weightValue / (heightValue * heightValue);

            displayBmi(bmi);
        }

    }
   
    private void displayBmi(float bmi) {
        String bmiLabel = "";

        if (Float.compare(bmi, 16f) <= 0) {
            bmiLabel = getString(R.string.wyglodzenie);
        } else if (Float.compare(bmi, 15f) > 0 && Float.compare(bmi, 16f) <= 0) {
            bmiLabel = getString(R.string.wychudzenie);
        } else if (Float.compare(bmi, 16f) > 0 && Float.compare(bmi, 18.5f) <= 0) {
            bmiLabel = getString(R.string.niedowaga);
        } else if (Float.compare(bmi, 18.5f) > 0 && Float.compare(bmi, 25f) <= 0) {
            bmiLabel = getString(R.string.norma);
        } else if (Float.compare(bmi, 25f) > 0 && Float.compare(bmi, 30f) <= 0) {
            bmiLabel = getString(R.string.nadwaga);
        } else if (Float.compare(bmi, 30f) > 0 && Float.compare(bmi, 35f) <= 0) {
            bmiLabel = getString(R.string.class_i);
        } else if (Float.compare(bmi, 35f) > 0 && Float.compare(bmi, 40f) <= 0) {
            bmiLabel = getString(R.string.class_ii);
        } else bmiLabel = getString(R.string.class_iii);

        bmiLabel = bmi + "\n\n" + bmiLabel;
        result.setText(bmiLabel);

    }

    @Override
    public void onStart() {
        super.onStart();

        client.connect();
        Action viewAction = Action.newAction(
                Action.TYPE_VIEW, // TODO: choose an action type.
                "Main Page",
               
                Uri.parse("http://host/path"),
                
                Uri.parse("android-app://marczuk.com.bmi/http/host/path")
        );
        AppIndex.AppIndexApi.start(client, viewAction);
    }

    @Override
    public void onStop() {
        super.onStop();

      
        Action viewAction = Action.newAction(
                Action.TYPE_VIEW, // TODO: choose an action type.
                "Main Page",
                Uri.parse("http://host/path"),
                Uri.parse("android-app://marczuk.com.bmi/http/host/path")
        );
        AppIndex.AppIndexApi.end(client, viewAction);
        client.disconnect();
    }
}

0

A to nie jest tak że masz tutaj wątek który non stop spawnuje ci kolejny raz to activity a ono robi ImageView iv = (ImageView) findViewById(R.id.splash); i ładuje sobie ten obrazek?

0

No tak, tak, Ten przykład znalazłem gdzieś na necie, zmieniłem czas na 3000ms ale wraz nic nie dało. Znasz może jakiś inny sposób jak zrobić splash screena ?

0

Zamiast klasy Thread możesz w wygodny sposób użyć klasy Handler z pakietu android.os i opóźnić uruchomienie drugiej aktywności

    public static int SPLASH_SCREEN_TIME_MS = 3000;

    Handler handler = new Handler();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(Splashscreen.this,
                        MainActivity.class);
                startActivity(intent);
                Splashscreen.this.finish();
            }
        }, SPLASH_SCREEN_TIME_MS);
    }
0

Wywal z activity_splashscreen.xml w imageview parametr src.

1 użytkowników online, w tym zalogowanych: 0, gości: 1