Failure To allocate (blank) byte allocation with (blank) bytes free of 57mb until OOM

58 Views Asked by At

I'm trying to switch actvities. Each activity only has one image view so I didn't find it necessary to show xml. It's a relatively simple program. Based on the IF-STATEMENT the program should switch to its appropriate view. All the images are 72dpi jpegs.

package app.com.example.android.chancetheoracle;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

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

        Toast.makeText(this, "Think Of Your Question, Then Tap Anywhere.",Toast.LENGTH_LONG).show();
    }

    public void onClick(View view){
        Random answer = new Random();

        int ask = answer.nextInt(6) + 1;

        if (ask == 1){
            Intent intent = new Intent(this, redThree.class);
            startActivity(intent);
            Toast.makeText(this, "2/5! Click Anywhere To Ask Again.",Toast.LENGTH_SHORT).show();
        }
        else if (ask == 2){
            Intent intent = new Intent(this, greenThree.class);
            startActivity(intent);
            Toast.makeText(this, "3/5! Click Anywhere To Ask Again.",Toast.LENGTH_SHORT).show();
        }
        else if (ask == 3){
            Intent intent = new Intent(this, greenFour.class);
            startActivity(intent);
            Toast.makeText(this, "4/5! Click Anywhere To Ask Again.",Toast.LENGTH_SHORT).show();
        }
        else if (ask == 4){
            Intent intent = new Intent(this, redFour.class);
            startActivity(intent);
            Toast.makeText(this, "1/5! Click Anywhere To Ask Again.",Toast.LENGTH_SHORT).show();
        }
        else if (ask == 5){
            Intent intent = new Intent(this, redFive.class);
            startActivity(intent);
            Toast.makeText(this, "0/5! Click Anywhere To Ask Again.",Toast.LENGTH_SHORT).show();
        }
        else if (ask == 6){
            Intent intent = new Intent(this, greenFive.class);
            startActivity(intent);
            Toast.makeText(this, "5/5! Click Anywhere To Ask Again.",Toast.LENGTH_SHORT).show();
        }
        else {
            Toast.makeText(this, "Think Of Your Question, Then Tap Anywhere.",Toast.LENGTH_LONG).show();
        }
    }

    public static class greenThree extends AppCompatActivity {

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

    public static class redThree extends AppCompatActivity {

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

    public static class greenFour extends AppCompatActivity {

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

    public static class redFour extends AppCompatActivity {

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

    public static class greenFive extends AppCompatActivity {

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

    public static class redFive extends AppCompatActivity {

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

1

There are 1 best solutions below

2
On

You haven't assigned the onClick listener to anything. It's not going to fire on it's own.

final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // Perform action on click
    }
});

For the memory issue, refer to this thread: Android:java.lang.OutOfMemoryError: Failed to allocate a 23970828 byte allocation with 2097152 free bytes and 2MB until OOM

https://developer.android.com/reference/android/widget/Button.html