从方法中将文本映射到按钮

mapping text to buttons from within a method

我正处于开发这个 android 应用程序的早期阶段,我正在尝试从一个方法中将 setText 设置为按钮。不在方法中执行它很好,但我想一遍又一遍地调用该方法。但是,当我尝试创建执行此操作的方法时,出现 "Non-static method findViewById(int) cannot be referenced from a static context" 错误。

例如:按钮button = (Button) findViewById(R.id.button); button.setText(选项[0]);

findViewById 将以红色高亮显示

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

}

删除

static

来自您的函数。 同时从 onCreate 回调中删除函数,它们不属于那里。

调用 onCreate 中的函数时,functions/methods 不能是静态的

如果你想在静态方法中使用 findViewById() 然后让你的 rootview 全局变量