0

「OtherClass」から「somevariable」の値を取得するにはどうすればよいですか? 以下で試していたように、これにアクティビティのコンテキストを使用できますか?

public class ParentActivity extends Activity {
  //This variable is reused by multiple Activities inheriting form this class
  protected static String somevariable = "text";   
  ...
}

public class MyActivity extends ParentActivity {
  @Override
  public void onCreate(Bundle icicle) {
  super.onCreate(icicle);
  myObject = new OtherClass(this);
  myObject.doSomething();
  ...
  }

...
}

public class OtherClass(){
  private Context c;
  private String b;

  OtherClass(Context context) {
    c = context;
  }

    doSomething() {
        // This does NOT work. 
        // How can I get somevariable from the ParentActivity????
        b = c.somevariable;   
    }
}
4

3 に答える 3

2

somevariableクラスで定義された特定のものにアクセスしたい場合は、宣言さParentActivityれているので、アクセスを試みる必要があります。ParentActivity.somevariablestatic

public class OtherClass { // Why were there extra parentheses ()
  private Context c;
  private String b;

  OtherClass(Context context) {
    c = context;
  }

  doSomething(){
  b = ParentActivity.somevariable; // Try this at home ;)
  }

}
于 2012-09-23T12:43:00.510 に答える
0
a = ParentActivity.somevarible;

それはうまくいくはずです。

于 2012-09-23T12:36:34.637 に答える
0

次のように変数を渡すことができます。

public void doSomething(String someVariable){
  b = someVariable;   
}
于 2012-09-23T12:30:27.043 に答える