[英]getLocationOnScreen crashes
我需要在屏幕上獲取我的應用程序的坐標,以將它們用作彈出窗口的偏移量。
View tempView = (View) findViewById(R.layout.main);
int[] loc = {0,0};
tempView.getLocationOnScreen(loc); /// crashes here!
嘗試了這段代碼,但它的最后一行使應用程序崩潰。 也許我從主要布局得到的tempView不知何故與屏幕上的布局不一致?
有什么建議......謝謝! :)
補充:已解決
int[] loc = new int[2];
View tempView = (View) findViewById(R.id.LL_whole);
tempView.getLocationOnScreen(loc);
post( String.valueOf(loc[1]) );
作品! :)
嘗試檢查tempView是否為null。 我懷疑findViewById()無法找到你給它的id。
int[] loc = {0,0};
View tempView = (View) findViewById(R.layout.main);
if (tempView != null) {
tempView.getLocationOnScreen(loc);
} else {
Log.d("YourComponent", "tempView was null.");
}
在你的XML中你有這樣的東西:
<SomeLayout android:id="@+id/myLayout" ... >
<SomeView android:id="@+id/myView" ... />
<SomeOtherView android:id="@+id/myOtherView" .../>
</SomeLayout>
所以你想說:
SomeView v = (SomeView)findViewById(R.id.myView);
也許:
SomeLayout l = (SomeLayout)findViewById(R.id.myLayout);
findViewById(R.layout.main)
你應該放在R.id.something而不是R.layout.something。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.