所以我有一个主类,另一个类有一个我需要进入主类的变量。我已经尝试过像这样回答的问题上发布的一些方法,但我仍未能做到这一点。
public class Example extends MapActivity
{
public void OnCreate(Bundle savedInstanceState){
final Button bttn = (Button)findViewById(R.id.button1);
bttn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast tulos = Toast.makeText(getBaseContext(),
"Area is: "
+MyItemizedOverlay.alue +""
/*I get the non static variable error here
which I get as it is not yet defined, it will be after the user
inputs values into the program*/
,Toast.LENGTH_LONG);
tulos.show();
}
});
public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {
int alue;
//irreleveant stuff (I assume)
public void Calc(Geopoint gp, Mapview map){
//there's some stuff before the variable I want to get like other variables
//not relevant for my problem I hope
alue = (int)Math.round(*formula: derived from user input data*)
}
}
那么我怎样才能从我的其他课程中获得一个价值,因为它现在似乎无法获得它?或者这可能是一个更大问题的迹象?
答案 0 :(得分:0)
有两种方式
1 - make alue static
但会显示error that satic variable can't be accesses in non static function Calc
,因此make function Calcalso static as well
或转到第2点
2 - 尝试以下new MyItemizedOverlay().alue with making alue public
public class Example extends MapActivity
{
public void OnCreate(Bundle savedInstanceState){
final Button bttn = (Button)findViewById(R.id.button1);
bttn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast tulos = Toast.makeText(getBaseContext(),
"Area is: "
+new MyItemizedOverlay().alue +"" /<--------------------
/*I get the non static variable error here
which I get as it is not yet defined, it will be after the user has input
values into the program*/
,Toast.LENGTH_LONG);
tulos.show();
}
});
public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {
public int alue; /<--------------------
//irreleveant stuff (I assume)
public void Calc(Geopoint gp, Mapview map){
//there's some stuff before the variable I want to get like other variables
//not relevant for my problem I hope
alue = (int)Math.round(*formula: derived from user input data*)
}
答案 1 :(得分:0)
int alue
没有访问修饰符,表示它是默认值。
使其可以从其他类访问public int alue
。
看看Controlling Access to Members of a Class