problem when running apk on cell phone

2

You see then I have the following code and I just started on this android, well I have a problem running the app on my cell phone (it says that it has closed unexpectedly), which I can not find the error because in the emulator of my laptop runs well, I have the files added, any ideas?

package mx.edu.itoaxaca.oraciones;

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Iterator;

public class Palabras extends AppCompatActivity {
    private ArrayList<Object> pistas;
    private MediaPlayer reprod;
    private String texto;
    private TextView ora;
    private boolean bandSujeto,bandAd,bandAc,bandLugar;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_palabras);
        reprod=null;
        pistas=new ArrayList<Object>();
        ora=(TextView)findViewById(R.id.oracion);
        bandSujeto=false;
        bandAc=false;
        bandLugar=false;
        bandAd=false;
        texto="";
    }

    public void reproducir(View eve){
        ora.setText("");
        texto="";
        switch (eve.getId()){
            case R.id.dog:
                pistas.add(R.raw.perro);
                bandSujeto=true;
                texto+="El perro ";
                //play(R.raw.perro);
                break;
            case R.id.lion:
                pistas.add(R.raw.leon);
                bandSujeto=true;
                texto+="El león ";
                //play(R.raw.leon);
                break;
            case R.id.elephant:
                pistas.add(R.raw.elefante);
                bandSujeto=true;
                texto+="El elefante ";
                //play(R.raw.elefante);
                break;
            case R.id.parrow:
                pistas.add(R.raw.guacamaya);
                bandSujeto=true;
                texto+="La guacamaya ";
                //play(R.raw.guacamaya);
                break;
        }
    }
    public void adjetivo(View eve){
        if(bandSujeto==false){
            Toast to= Toast.makeText(getApplicationContext(),"Seleccione un Sujeto primero!",Toast.LENGTH_LONG);
            to.show();
        }else{
            switch (eve.getId()){
                case R.id.man:
                    pistas.add(R.raw.manchas);
                    bandAd=true;
                    texto+="con manchas ";
                    //play(R.raw.perro);
                    break;
                case R.id.feroz:
                    pistas.add(R.raw.feroz);
                    bandAd=true;
                    texto+="feroz ";
                    //play(R.raw.perro);
                    break;
                case R.id.afric:
                    pistas.add(R.raw.africano);
                    bandAd=true;
                    texto+="africano ";
                    //play(R.raw.perro);
                    break;
                case R.id.ruidosa:
                    pistas.add(R.raw.ruidosa);
                    bandAd=true;
                    texto+="ruidosa ";
                    //play(R.raw.perro);
                    break;

            }
        }
    }

    public void accion(View eve){
        if(bandAd==false){
            Toast to= Toast.makeText(getApplicationContext(),"Seleccione un adjetivo primero!",Toast.LENGTH_LONG);
            to.show();
        }else{
            switch (eve.getId()){
                case R.id.eat:
                    pistas.add(R.raw.come);
                    bandAc=true;
                    texto+="come ";
                    //play(R.raw.perro);
                    break;

                case R.id.caza:
                    pistas.add(R.raw.caza);
                    bandAc=true;
                    texto+="caza ";
                    //play(R.raw.perro);
                    break;
                case R.id.bania:
                    pistas.add(R.raw.bana);
                    bandAc=true;
                    texto+="se baña ";
                    //play(R.raw.perro);
                    break;
                case R.id.sing:
                    pistas.add(R.raw.canta);
                    bandAc=true;
                    texto+="canta ";
                    //play(R.raw.perro);
                    break;
            }
        }
    }

    public void lugar(View eve){
        if(bandAc==false){
            Toast to= Toast.makeText(getApplicationContext(),"Seleccione una accion primero!",Toast.LENGTH_LONG);
            to.show();
        }else{
            switch (eve.getId()){
                case R.id.house:
                    pistas.add(R.raw.casa);
                    bandAd=true;
                    texto+="en casa ";
                    //play(R.raw.perro);
                    break;
                case R.id.origen:
                    pistas.add(R.raw.sabana);
                    bandAd=true;
                    texto+="en la sabana ";
                    //play(R.raw.perro);
                    break;
                case R.id.lago:
                    pistas.add(R.raw.lago);
                    bandAd=true;
                    texto+="en el lago ";
                    //play(R.raw.perro);
                    break;
                case R.id.three:
                    pistas.add(R.raw.arbol);
                    bandAd=true;
                    texto+="en el arbol ";
                    //play(R.raw.perro);
                    break;


            }
        }
    }


    public void play(View eve){
        ora.setText(texto);
        if(reprod!=null){
            stopPlaying();
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                Iterator<Object> it=pistas.iterator();
                try {
                    while (it.hasNext()) {
                        //Thread.sleep(1000);
                        Object num = it.next();
                        reprod = MediaPlayer.create(getApplicationContext(), (int) num);

                        reprod.start();
                        Thread.sleep(1050);


                    }

                  pistas.clear();

                }catch(Exception e){;}
            }
        }).start();

    }

    public void rest(View eve){
        if(reprod!=null){
        reprod.release();
        reprod = null;}
        texto="";
        ora.setText("");
        pistas.clear();
        bandAc=false;
        bandAd=false;
        bandSujeto=false;
        bandLugar=false;
    }
    private void stopPlaying() {
        if (reprod != null) {
            reprod.stop();
            reprod.release();
            reprod = null;
        }
    }
    public void vacia(){
        if(reprod.isPlaying())
        pistas.clear();
    }
}
    
asked by Andy M. Moreno 25.05.2017 в 06:31
source

2 answers

4

This error is the result of too many images being loaded or the images are very large:

Try adding android: largeHeap="true" in your AndroidManifest , what it does is try to increase the available memory for your application:

  

android: largeHeap="true"

There should be something like this:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:largeHeap="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

But it is important to optimize the images, I suggest you review this answer with tips to optimize your images:

good resolution of an image causes "OutOfMemoryError"

    
answered by 25.05.2017 / 15:26
source
0

The problem that you add is a OutOfMemoryError error, it is a very common problem and it is a problem of the java virtual machine (JVM) when there is not enough space to assign an object or the garbage collector has not released junk space. It may also be because your application contains many files (images, audios) as it is in your case.

Add to your AndroidManifest the parameter android:largeHeap="true" can be a solution, but in the same documentation indicate that it is not advisable:

  

Most applications should not need this   parameter and instead, should focus on releasing or reducing the   Total memory usage to improve performance. If this parameter is enabled, the fixed increase in available memory is not guaranteed, because some devices limit their total memory.

    
answered by 25.05.2017 в 15:07