我想将一个变量从一个类调用到另一个类(在我的示例中,从 WifiActivity 到 WifiBroadcastReceiver)。我对此使用了一种方法,这是我的代码:
WifiActivity.java:
public class WifiActivity extends Activity implements OnClickListener {
[...]
public boolean getLocalizationState() {
return localizationPhase; //localizationPhase is defined as boolean
}
[...]
}
WifiBroadcastReceiver.java :
public class WifiBroadcastReceiver extends BroadcastReceiver {
private WifiActivity wifiActivity; //
[...]
private boolean localizationState;
@Override
public void onReceive(Context context, Intent intent) {
[...]
while(position<=numberdisplay-1){ // While we have not read all the AP RSSI average
localizationState=wifiActivity.getLocalizationState(); //The problem is here!!!
if(sampleposition[position]!=0 && localizationState==true){
[...]
}
[...]
}
[...]
}
}
当我使用wifiActivity.getLocalizationState()
它时返回'false',但实际上当我在调试模式下查看时它是'true':variables->context->localizationPhase='true' 我印象深刻的是wifiActivity.getLocalizationState()
刚刚读取了变量的定义,但没有变化。我调用的其他方法也有同样的问题,它不会返回与调试窗口相同的东西。
你知道可能是什么问题吗?
对不起我的英语不好...
编辑:这是 WifiActivity 类的开始:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wifi);
@Jason C:当您谈论初始化时,您会考虑这个吗?见识少见见谅……