2016-09-07 1 views
0

Je suis dans les toutes premières étapes du développement de cette application android, et j'essaie de définir des boutons à partir d'une méthode. Ne pas le faire dans une méthode est bien, mais je veux appeler la méthode encore et encore. Cependant, lorsque j'essaie de créer la méthode pour ce faire, j'obtiens une erreur "La méthode non statique findViewById (int) ne peut pas être référencée à partir d'un contexte statique". Par exemple: Bouton bouton = (Bouton) findViewById (R.id.button); button.setText (options [0]);mappage de texte à des boutons à partir d'une méthode

Le findViewById sera rouge mis en évidence

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; 
} 

}

+0

pourquoi vous avez écrit statique int getQuestion() {?? Supprime static et appelle la méthode dans oncreate – kgandroid

Répondre

0

Retirez le

statique

de vos fonctions. Supprimez également les fonctions du rappel onCreate, elles n'appartiennent pas à cet emplacement.

Lorsque vous invoquer les fonctions onCreate, les fonctions/méthodes ne peuvent pas être statiques

+0

Nice one. Merci beaucoup. – david

+0

@david De rien. Veuillez signer la question comme correcte :) – Firecat

0

Si vous souhaitez utiliser findViewById() dans la méthode statique puis faites votre rootview variable globale