1

Having a problem with Android studio with the randomizer application I'm making and it seems unable to resolve setOnClickListener. Below is the code for the application I can't seem to identify the problem any help would be greatly appreciated.

public class MainActivity extends AppCompatActivity {

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

TextView textOne = (TextView) findViewById(R.id.textView);
TextView textTwo = (TextView) findViewById(R.id.textView2);
TextView textThree = (TextView) findViewById(R.id.textView3);
TextView textFour = (TextView) findViewById(R.id.textView4);
TextView textFive = (TextView) findViewById(R.id.textView5);
TextView textSix = (TextView) findViewById(R.id.textView6);
TextView textSeven = (TextView) findViewById(R.id.textView7);
TextView textEight = (TextView) findViewById(R.id.textView8);
Button button = (Button) findViewById(R.id.button);

String[] myStamina = {"Stamina", "300%"};
String[] mySize = {"Off", "Mega", "Mini"};
String[] myHead = {"Off", "Flower", "Bunny"};
String[] myBody = {"Off", "Metal", "Clear", "Tail", "Rocket belt", "Screw", "Back shield"};
String[] myStatus = {"Off", "Curry", "Reflect"};
String[] myGravity = {"Off", "Light", "Heavy"};
String[] mySpeed = {"Off", "Fast", "Slow"};
String[] myCamera = {"Off", "Fixed", "Angled"};

int random1 = (int) ((Math.random() * 1));
int random2 = (int) ((Math.random() * 2));
int random3 = (int) ((Math.random() * 6));

button.setOnClickListener(new View.OnClickListener(){

    public void onClick(View v){
        textOne.setText(myStamina[random1]);
        textTwo.setText(mySize[random2]);
        textThree.setText(myHead[random2]);
        textFour.setText(myBody[random3]);
        textFive.setText(myStatus[random2]);
        textSix.setText(myGravity[random2]);
        textSeven.setText(mySpeed[random2]);
        textEight.setText(myCamera[random2]);
    }



});

}

Coulton
  • 13
  • 1
  • 3

4 Answers4

2

Move your code to the onCreate() method like this:

public class MainActivity extends AppCompatActivity {

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

    TextView textOne = (TextView) findViewById(R.id.textView);
    TextView textTwo = (TextView) findViewById(R.id.textView2);
    TextView textThree = (TextView) findViewById(R.id.textView3);
    TextView textFour = (TextView) findViewById(R.id.textView4);
    TextView textFive = (TextView) findViewById(R.id.textView5);
    TextView textSix = (TextView) findViewById(R.id.textView6);
    TextView textSeven = (TextView) findViewById(R.id.textView7);
    TextView textEight = (TextView) findViewById(R.id.textView8);
    Button button = (Button) findViewById(R.id.button);

    String[] myStamina = {"Stamina", "300%"};
    String[] mySize = {"Off", "Mega", "Mini"};
    String[] myHead = {"Off", "Flower", "Bunny"};
    String[] myBody = {"Off", "Metal", "Clear", "Tail", "Rocket belt", "Screw", "Back shield"};
    String[] myStatus = {"Off", "Curry", "Reflect"};
    String[] myGravity = {"Off", "Light", "Heavy"};
    String[] mySpeed = {"Off", "Fast", "Slow"};
    String[] myCamera = {"Off", "Fixed", "Angled"};

    int random1 = (int) ((Math.random() * 1));
    int random2 = (int) ((Math.random() * 2));
    int random3 = (int) ((Math.random() * 6));

    button.setOnClickListener(new View.OnClickListener(){

        public void onClick(View v){
            textOne.setText(myStamina[random1]);
            textTwo.setText(mySize[random2]);
            textThree.setText(myHead[random2]);
            textFour.setText(myBody[random3]);
            textFive.setText(myStatus[random2]);
            textSix.setText(myGravity[random2]);
            textSeven.setText(mySpeed[random2]);
            textEight.setText(myCamera[random2]);
        }

    });


}
1
  1. Declare your TextView and Button widgets as global.
  2. Set button onClick listener from OnCreate() method.

Try this:

public class MainActivity extends AppCompatActivity {

    TextView textOne, textTwo, textThree, textFour, textFive, textSix, textSeven, textEight;
    Button button;

    String[] myStamina = {"Stamina", "300%"};
    String[] mySize = {"Off", "Mega", "Mini"};
    String[] myHead = {"Off", "Flower", "Bunny"};
    String[] myBody = {"Off", "Metal", "Clear", "Tail", "Rocket belt", "Screw", "Back shield"};
    String[] myStatus = {"Off", "Curry", "Reflect"};
    String[] myGravity = {"Off", "Light", "Heavy"};
    String[] mySpeed = {"Off", "Fast", "Slow"};
    String[] myCamera = {"Off", "Fixed", "Angled"};

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

    textOne = (TextView) findViewById(R.id.textView);
    textTwo = (TextView) findViewById(R.id.textView2);
    textThree = (TextView) findViewById(R.id.textView3);
    textFour = (TextView) findViewById(R.id.textView4);
    textFive = (TextView) findViewById(R.id.textView5);
    textSix = (TextView) findViewById(R.id.textView6);
    textSeven = (TextView) findViewById(R.id.textView7);
    textEight = (TextView) findViewById(R.id.textView8);
    button = (Button) findViewById(R.id.button);

    int random1 = (int) ((Math.random() * 1));
    int random2 = (int) ((Math.random() * 2));
    int random3 = (int) ((Math.random() * 6));

    button.setOnClickListener(new View.OnClickListener(){

        public void onClick(View v){
            textOne.setText(myStamina[random1]);
            textTwo.setText(mySize[random2]);
            textThree.setText(myHead[random2]);
            textFour.setText(myBody[random3]);
            textFive.setText(myStatus[random2]);
            textSix.setText(myGravity[random2]);
            textSeven.setText(mySpeed[random2]);
            textEight.setText(myCamera[random2]);
        }

    });

}
Ferdous Ahamed
  • 21,438
  • 5
  • 52
  • 61
0

Move your code inside OnCreate() Method.

J.A.V.A
  • 3
  • 4
0

The solution was presented to you, now let dig into some theory: although you have tagged your question as android related, I'll suggest you to look a little bit at some object oriented programming principles. If you want to have executable code (i.e. code that calls other methods on object members and the like) in your class, you should put it inside a method. The idea is that an object must be first created(instantiated) in order to use all its fields without worrying if its fields are instantiated

Note that all the code outside methods will be executed either when the class is loaded (static code blocks) or when an object of that class type is created (constructor).

Going back to your original question: the android virtual machine cannot even tell who is "button" at the moment when you try to set its click listener, because at that point in execution the instance of MainActivity created by the system is not completely created (instantiated).

Community
  • 1
  • 1