0

あるボイドから別のボイドに物事を「共有」する方法を考えています。2つのボイドがあります。1つは「Setup」と呼ばれ、もう1つは「NextQuestionAnswer」と呼ばれます。「セットアップ」に変数があり、「NextQuestionAnswer」ボイドからアクセスする必要があります。私はこのような他の質問を見ましたが、どれも役に立たないようです。これが私が達成しようとしている私のコードです。

私の問題は、TextViewがパブリック変数または静的変数を受け入れないため、グローバル変数として宣言できないことです。

    public void setup() {
      ArrayList<String> questions = new ArrayList<String>();
      ArrayList<String> answers = new ArrayList<String>(); 
      ArrayList<String> correctanswers = new ArrayList<String>();

    TextView question = (TextView)(findViewById(R.id.textboxquestion));
      RadioButton ac1 = (RadioButton)(findViewById(R.id.radiobuttona));
      RadioButton ac2 = (RadioButton)(findViewById(R.id.radiobuttonb));
      RadioButton ac3 = (RadioButton)(findViewById(R.id.radiobuttonc));
      RadioButton ac4 = (RadioButton)(findViewById(R.id.radiobuttond));
    }

    public void NextQuestionAnswer() {

    question.setText("something here"); ---THIS IS THE LINE THAT I WANT TO BE ABLE TO DO

}
4

7 に答える 7

2

ある関数の別の関数の変数declareにアクセスできないため、クラスレベルで宣言する必要があります。onCreate()アクセスできるように、メソッドの前に宣言してください。

例。

public class Test extends Activity 
{
    private TextView textView = null;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }   
}
于 2013-03-26T06:15:22.260 に答える
1

クラスに1つのグローバル変数を配置し、それを2つのメソッドで使用できます。

于 2013-03-26T06:13:59.303 に答える
0

グローバル変数として変数を作成する

ローカル変数へのアクセスは、メソッド自体でのみ行われます

于 2013-03-26T06:16:02.687 に答える
0

これは単なる考えです:

両方のメソッドが同じクラスにある場合、なぜグローバル変数を使用しないのですか?1つのメソッドが異なるクラスにある場合は、getter / setterでグローバル変数を使用します(変数がパブリックでない場合)

于 2013-03-26T06:16:11.730 に答える
0
 private String textValue = null;   
 public void setup() {
     ...
     setTextValue("value");
     ...
 }

 public void NextQuestionAnswer() {

    question.setText(getTextValue());

 }
于 2013-03-26T06:17:22.943 に答える
0
class MainActivity extends Activity 
{
TextView tv;//global declaration

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
            tv= (TextView) findViewById(R.id.textView)
}  

  public void setup() {
         //tv can be accessed here also
   }

  public void NextQuestionAnswer() {
   tv.setText("my text"); //set yout text value

  }

}

クラス変数として宣言すると、クラス全体からアクセスできます。

メソッド内で変数を宣言すると、そのメソッドでのみローカルで使用できます。

ローカル変数はスタックに格納されます。インスタンス変数と静的変数はヒープに格納されます。

于 2013-03-26T06:18:04.370 に答える
0

プライベートと宣言することも、パブリックにすることもできます

private TextView question
    onCreate
    {
    question = (TextView)(findViewById(R.id.textboxquestion));
    }      
    public void setup() {
          ArrayList<String> questions = new ArrayList<String>();
          ArrayList<String> answers = new ArrayList<String>(); 
          ArrayList<String> correctanswers = new ArrayList<String>();


          RadioButton ac1 = (RadioButton)(findViewById(R.id.radiobuttona));
          RadioButton ac2 = (RadioButton)(findViewById(R.id.radiobuttonb));
          RadioButton ac3 = (RadioButton)(findViewById(R.id.radiobuttonc));
          RadioButton ac4 = (RadioButton)(findViewById(R.id.radiobuttond));
        }

        public void NextQuestionAnswer() {

        question.setText("something here"); ---THIS IS THE LINE THAT I WANT TO BE ABLE TO DO

    }
于 2013-03-26T06:19:47.840 に答える