2016-06-07 4 views
0

Meine Methode newNum mit dem Parameter resetb wird auf eine Schaltfläche namens onClick, aber ich möchte auch, dass es laufen in onCreate finden. Aus irgendeinem Grund, wenn ich versuche, newNum(resetB); in OnCreate aufrufen, gibt es mir den Fehler: Kann nicht finden Symbol Variable resetB. Ich verstehe nicht, warum das so ist. Jede Hilfe wäre willkommen!Kann nicht Symbolvariable für eine Ansicht (Android Studio)

public void newNum(View resetB){ 
    TextView numOne = (TextView) findViewById(R.id.numOne); 
    TextView numTwo = (TextView) findViewById(R.id.numTwo); 
    TextView ans = (TextView) findViewById(R.id.ans); 
    Button reset = (Button) resetB; 

    Random rand = new Random(); 
    int ranOne = rand.nextInt(50) + 1; 
    int ranTwo = rand.nextInt(50) + 1; 
    int ansNum = (ranOne/2) + ranTwo; 

    numOne.setText("" + ranOne); 
    numTwo.setText("" + ranTwo); 
    ans.setText("" + ansNum); 
} 

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

Antwort

1

Die Variable resetB ist nicht im Gültigkeitsbereich onCreate definiert. Versuchen Sie:

View reset = findViewById(R.id.<>); // Put the id of the button with the onClick attribute 
newNum(reset); 
Verwandte Themen