0

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);
        }
    }
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
okunato
  • 63
  • 1
  • 8

1 Answers1

-1

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

Community
  • 1
  • 1
Serg Chernata
  • 12,280
  • 6
  • 32
  • 50
  • *You haven't assigned the onClick listener* And that's why he has OOM... Interesting :-) ... Also it could be done via layout XML... – Selvin Dec 30 '16 at 21:12
  • @Selvin At least part of the problem he described is "Based on the IF-STATEMENT the program should switch to its appropriate view." And he went out of his way to say that the XML files have nothing in them but the image. – Serg Chernata Dec 30 '16 at 21:15