반응형

아주 간단한 Button 한개가 추가된 Simple Project를 작성하고 

Button Click Handler를 구성하기 위해 다음과 같이 code를 작성했다.

 

=========================================================================== 

  1  public void onCreate(Bundle savedInstanceState) {
  2      super.onCreate(savedInstanceState);
  3       
  4      try { 
  5          setContentView(R.layout.main); 
  6          btnObject = (Button)this.findViewById(R.id.button1);  <=== exception 
  7          btnObject.setOnClickListener(this);
  8      } 
  9      catch (Exception e) { 
 10       e.printStackTrace(); 
 11      } 
 12  } 
 13 
===========================================================================

 


그런데 이러허게 간단한 code가 왠걸...컴파일 후 실행하면
exception(id=830007829856)이 발생되면서 프로그램이 죽어버린다.

흠.. 몇시간의 구글링을 통해 알아낸 결과로는

button이 소속된 layout의 생성이 되지 않았는데 button id를 호출해서 발생된다는 것

 

그러나 구글링을 통해 알아낼 해결방법은 고작

Layout생성을 위해 setContentView(R.layout.main); 코드를 최상위로 올려라!!


그렇지만 위의 code에서와 같이 벌써 coding되어 있음에도 exception이 발생된다는것은
여전히 main layout이 생성되지 않았다는 것을 의미한다.

몇시간의 삽질 끝에.. main.xml에 다음과 같이 어이가 없는 상황이 발생되어 있음을 알게되었다.

===========================================================================

  1 <?xml version="1.0" encoding="utf-8"?> 
  2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3     android:id="@+id/linearLayout1"  <== 이 라인이 추가. 
  4     android:orientation="vertical" 
  5     android:layout_width="fill_parent" 
  6     android:layout_height="fill_parent" 
  7     > 
  8  
  9     <Button 
 10         android:id="@+id/button1" 
 11         android:layout_width="fill_parent" 
 12         android:layout_height="wrap_content" 
 13         android:text="Button" /> 
 14  
 15 </LinearLayout>
===========================================================================


반응형
,