2011-08-29 7 views
0

화면에서 내 앱의 좌표를 가져와 팝업으로 오프셋으로 사용해야합니다.getLocationOnScreen이 충돌 함

    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])); 

작품을 해결! :)

답변

1

tempView가 null인지 확인하십시오. findViewById()가 제공 한 ID를 찾지 못하는 것 같습니다.

<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); 
+0

예, null을 제공합니다. :) findViewById 뭔가 다른, 다음 메인? – Roger

+0

예, 찾으려는보기의 ID입니다. –

+0

감사합니다. 위의 수정 사항을 참조하십시오. :) – Roger

1

findViewById를 (R 당신의 XML에서

int[] loc = {0,0}; 
View tempView = (View) findViewById(R.layout.main); 
if (tempView != null) { 
    tempView.getLocationOnScreen(loc); 
} else { 
    Log.d("YourComponent", "tempView was null."); 
} 

는이 같은 있습니다. layout.main)

여기에 R.layout.something이 아닌 R.id를 두어야합니다.

+0

감사합니다. 위의 수정 사항을 참조하십시오. :) – Roger