I'm in the very early stages of developing this android app, and I'm trying to setText to buttons from within a method. Doing it not in a method is fine, but I want to call the method over and over. However when I try to create the method to do this, I get a "Non-static method findViewById(int) cannot be referenced from a static context" error.
eg: Button button = (Button) findViewById(R.id.button); button.setText(options[0]);
The findViewById will be red highlighted
package com.example.lp1;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private Button button;
private Button button2;
private Button button3;
private Button button4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int score = 0;
int count = 3;
static int getQuestion(){
ArrayList set = addingThree();
final int a = (int) set.get(0);
final int b = (int) set.get(1);
final int c = (int) set.get(2);
final int d = (int) set.get(3);
final int ans = (int) set.get(4);
String qText = (String) set.get(5);
String[] options = new String[] {Integer.toString(a),
Integer.toString(b),Integer.toString(c),Integer.toString(d)};
Button button = (Button) findViewById(R.id.button);
button.setText(options[0]);
Button button2 = (Button) findViewById(R.id.button2);
button2.setText(options[1]);
Button button3 = (Button) findViewById(R.id.button3);
button3.setText(options[2]);
Button button4 = (Button) findViewById(R.id.button4);
button4.setText(options[3]);
TextView textView = (TextView)findViewById(R.id.textView);
textView.setText(qText);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
static ArrayList addingThree(){
Random rand = new Random();
int numa = rand.nextInt(99);
int numb = rand.nextInt(99);
int numc = rand.nextInt(99);
int ans = numa + numb + numc;
int fake1 = rand.nextInt(200) + 50;
int fake2 = rand.nextInt(200) +50;
int fake3 = rand.nextInt(200) +50;
int[ ] options = {fake1, fake2, fake3, ans};
String stringQuestion = ("What is the sum of " + numa + ", " + numb + ",
and " + numc + "? ");
Arrays.sort(options);
ArrayList parcel = new ArrayList();
parcel.add(options[0]);
parcel.add(options[1]);
parcel.add(options[2]);
parcel.add(options[3]);
parcel.add(ans);
parcel.add(stringQuestion);
return parcel;
}
}
Remove the
static
from your functions. Also remove the functions from the onCreate callback, they don't belong there.
When you're invoking the functions in onCreate, the functions/methods cannot be static