안드로이드 개발 질문/답변
(글 수 45,052)
안녕하세요.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout layout = (LinearLayout)findViewById(R.layout.main);
layout.setBackgroundColor(Color.GREEN);
}
이렇게, xml 로 표시되는 레이아웃을 소스로 접근 하려고 하는데 nullporint 에러가 뜨네요..
어떻게 접근하면 좋은 건가요?
2012.01.31 15:18:12
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
.....
위와 같이 해주시고
findViewById(R.id.main_layout);
이런식으로요




findViewById 라는 메소드 명을 봐도 알 수 있듯이
ID를 통해서 View를 찾는 메소드입니다.
그러므로 파라메터는 id명이 들어가야 하는것이구요.
저렇게 하시려면 main.xml 라는 레이아웃의 최상위 Layout에 android:id 를 통해 id 를 설정해주시고
그 아이디를 통해 findViewById 메소드를 사용해주시면 됩니다.