Error Running App on Mobile

Asked

Viewed 351 times

0

I’m in trouble because I’m creating 1 app and running a good one on the emulator. And when I run on cellular of this error below...

Note: in the cell it opens and closes.

Obs2: in the emulator runs perfectly.

FATAL EXCEPTION: main
Process: com.example.dutra_pc.navigation_teste, PID: 21972
java.lang.OutOfMemoryError: Failed to allocate a 4981212 byte allocation with 3804820 free bytes and 3MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:988)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:2477)
at android.content.res.Resources.loadDrawable(Resources.java:2384)
at android.content.res.Resources.getDrawable(Resources.java:787)
at android.content.Context.getDrawable(Context.java:403)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:351)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:200)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:188)
at android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:100)
at android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:85)
at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:94)
at com.example.dutra_pc.navigation_teste.ButaoAdapter.getView(ButaoAdapter.java:35)
at android.widget.AbsListView.obtainView(AbsListView.java:2347)
at android.widget.ListView.makeAndAddView(ListView.java:1864)
at android.widget.ListView.fillDown(ListView.java:698)
at android.widget.ListView.fillFromTop(ListView.java:759)
at android.widget.ListView.layoutChildren(ListView.java:1673)
at android.widget.AbsListView.onLayout(AbsListView.java:2151)
at android.view.View.layout(View.java:15689)
at android.view.ViewGroup.layout(ViewGroup.java:5040)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:15689)
at android.view.ViewGroup.layout(ViewGroup.java:5040)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15689)
at android.view.ViewGroup.layout(ViewGroup.java:5040)
at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1196)
at android.view.View.layout(View.java:15689)
at android.view.ViewGroup.layout(ViewGroup.java:5040)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15689)
at android.view.ViewGroup.layout(ViewGroup.java:5040)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:15689)
at android.view.ViewGroup.layout(ViewGroup.java:5040)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15689)
at android.view.ViewGroup.layout(ViewGroup.java:5040)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:15689)
at android.view.ViewGroup.layout(ViewGroup.java:5040)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15689)
at android.view.ViewGroup.layout(ViewGroup.java:5040)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2116)
at android.

On the emulator >>>Emulador utilizado android studio

package com.example.dutra_pc.navigation_teste;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class Main2Activity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

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

        if(savedInstanceState == null){
            getSupportFragmentManager()
                    .beginTransaction()
                    .add(R.id.content_ini, new Content_Ini())
                    .commit();

        }

        ListView lista = (ListView) findViewById(R.id.lista);
        final ArrayList<Butao>butaos = adicionar();
        final ArrayAdapter adapter = new ButaoAdapter(this,butaos);
        lista.setAdapter(adapter);

        lista.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

                Butao butao = (Butao) adapter.getItem(position);
                Toast.makeText(Main2Activity.this,butao.getNome(), Toast.LENGTH_SHORT).show();
                if(position ==0){
                    //LayoutInflater inflater = LayoutInflater.from(view.getContext());
                    /*if(savedInstanceState == null){
                        getSupportFragmentManager()
                                .beginTransaction()
                                .add(R.id.cosProg, new Frag_Ini())
                                .commit();
                                Comando para iniciar fragments
                    }*/

                    //Intent myIntent = new Intent(view.getContext(), Main3Activity.class);
                    //startActivityForResult(myIntent, 0);

                    getSupportFragmentManager()
                            .beginTransaction()
                            .replace(R.id.content_ini, new Comunicacao()).commit();
                    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
                    drawer.closeDrawer(GravityCompat.START);

                }else if (position ==1 ){
                    getSupportFragmentManager()
                            .beginTransaction()
                            .replace(R.id.content_ini, new Programacao()).commit();
                    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
                    drawer.closeDrawer(GravityCompat.START);
                }
            }
        });

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }


    private ArrayList<Butao>adicionar(){
        ArrayList<Butao>butaos = new ArrayList<>();
        Butao a = new Butao("Comunicação", R.drawable.comunicacao);
        butaos.add(a);
        Butao b = new Butao("Programação", R.drawable.programacao);
        butaos.add(b);
        Butao c = new Butao("Eventos", R.drawable.eventos);
        butaos.add(c);
        Butao d = new Butao("Caravanas", R.drawable.caravanas);
        butaos.add(d);
        Butao e = new Butao("Experiência de fé", R.drawable.experiencia);
        butaos.add(e);
        Butao f = new Butao("Cultos", R.drawable.cultos);
        butaos.add(f);
        Butao g = new Butao("Aniversários", R.drawable.aniversarios);
        butaos.add(g);
        Butao h = new Butao("Secretaria", R.drawable.secretaria);
        butaos.add(h);
        Butao i = new Butao("Formação Jovem", R.drawable.formacao_jovens);
        butaos.add(i);
        Butao j = new Butao("Contato", R.drawable.contato);
        butaos.add(j);
        Butao k = new Butao("Compartilhar", R.drawable.ic_menu_share);
        butaos.add(k);

        return butaos;
    }



    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main2, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        //ArrayList<Butao>butaos = new ArrayList<>();
        ArrayList<Butao>butaos = adicionar();
        ArrayAdapter adapter = new ButaoAdapter(this,butaos);



        int id = item.getGroupId();

        if (id == R.id.textView1) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }


}
  • The app needs 4981212 memory, but only has 3804820 available-space.

  • @wmsouza cellular has 8Gb of memory if it helps this information for something...

  • It would be interesting to share the code with us! When I speak from memory, I speak from the library OOM who tried to allocate more memory than available at that time. Put in your file manifest on the tag application that: android:hardwareAccelerated="false", android:largeHeap="true"

  • @wmsouza q part of the code ?

  • Thank you very much! you are the Guy! hahaha worked! has how to explain what was done ? @wmsouza ?

  • Dude, is there a heavy image in your app (on his screens)? Because if so, it’s usually this... Images of 2MB, 1.5MB usually cause this kind of problem.

  • @itscorey no image comes to that all n they are in KB

Show 2 more comments

1 answer

0

This error is very common on Android, when an object cannot be allocated due to lack of memory space and also, the garbage collector cannot free up space. To resolve, you must add the properties below in the file manifest.

<application
    ...
    android:hardwareAccelerated="false"
    android:largeHeap="true">
  • hardware

    Enable or disable hardware acceleration, default value is true

  • largeHeap

    Enable or disable fixed increase in available memory. According to documentation no increase is guaranteed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.