我正在爲OS 5上的Blackberry開發應用程序。我試圖在調用成功時訪問一些以前初始化的變量。但是,這些變量不具有它們的價值,我無法找到原因。如何從PhoneListener事件訪問全局變量?
當應用程序啓動時,testVariable設置爲5,一切都按預期工作。但是,當我嘗試從callConnected事件訪問變量時,它返回0.即使我將該變量放在PhoneEvents類中,它仍然會返回0.它具有一個PhoneEvents值,另一個值用於屏幕。不應該這些變量只有一個值,因爲它不是一個實例變量?我究竟做錯了什麼?有沒有其他方法可以從PhoneEvents類和TestScreen類訪問變量,並保持其值?提前致謝。
public class TestApp extends UiApplication {
public TestApp() {
TestScreen screen = new TestScreen();
UiApplication.getUiApplication().pushScreen(screen);
}
public static void main(String[] args) {
Phone.addPhoneListener(new PhoneEvents());
TestApp app = new TestApp();
app.enterEventDispatcher();
}
}
// The main screen.
public class TestScreen extends MainScreen {
public TestScreen() {
this.setTitle("Test");
GlobalClass.testVariable = 5;
}
}
// A public static holding a variable I want to have access to.
public class GlobalClass {
public static int testVariable;
}
// The method that gets called when a call is made.
public class PhoneEvents implements PhoneListener {
public void callConnected(int callId) {
int x = GlobalClass.testVariable;
}
}
)謝謝!這正是我最終做的事情,但它不斷讓我知道是否還有別的東西我錯過了,很高興能得到確認。 – 2011-04-18 19:33:01