我需要獲取屏幕尺寸才能使我的應用程序正常工作。要做到這一點,我在我的主要活動中創建了2個函數,並且我通過非Activity類中的對象調用它們。Android從非活動類別獲取圖像大小
出於某種原因,它不斷崩潰。爲什麼我不能調用這些函數?我如何解決它?
代碼主要活動:
public int getWidth(){
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
return width;
}
public int getHeight(){
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int height = display.getHeight();
return height;
}
非活動的代碼(主叫方):
main Main = new main();
private int height = Main.getHeight();
private int width = Main.getWidth();
請注意我所知這些方法現在已經過時,但我不能使用的getSize (點);方法,因爲我正在開發最低API級別7.
將您的堆棧跟蹤從y我們的崩潰。 – kabuko
沒有顯示,它在getHeight()的第一行崩潰;因爲這是第一個被調用的函數。 – arielschon12
我們不能在非Activity類的構造函數中傳遞上下文嗎? –