TableRow안에 TextView와 EditText를 넣었는데 id값을 입력하였을 때 해당 xml을 불러들이는 Button을 findViewById로 호출할 시에

null포인터가 발생합니다.

 

다이얼로그를 이용하여 텍스트 입력 후 다이얼로그를 닫았을 때 그 텍스트를 부모 화면에 대입하려는 테스트중 발생한

에러입니다.

 

부모 xml입니다.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/linear"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    >
<Button
    android:id="@+id/btnshow" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="기간 선택"
    />
</LinearLayout>

 

btnshow 버튼에서 popup시키는 xml입니다.

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent" android:background="#000066">
 <TableLayout android:layout_width="fill_parent"
  android:layout_height="fill_parent" android:visibility="visible">
  <TableRow>
   <TextView android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:text="시작일자 : " />
   <EditText android:layout_width="130px"  - popup시키는 xml에 id값을 입력하였을때 위 btnshow 버튼을 findViewById하는 부분에서 에러
    android:layout_height="wrap_content" />
  </TableRow>
  <TableRow>
   <TextView android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:text="종료일자 : " />
   <EditText android:layout_width="130px"
    android:layout_height="wrap_content" />
  </TableRow>
  <Button android:id="@+id/btnclose" android:layout_width="wrap_content"
   android:layout_height="wrap_content" android:text="Close Popup" />
 </TableLayout>
</LinearLayout>

 

자바코드입니다.

package exam.dialog;

import exam.AndroidExam.R;
import android.app.*;
import android.os.*;
import android.view.*;
import android.view.View.OnClickListener;
import android.widget.*;


public class PopupTest extends Activity implements OnClickListener{
 PopupWindow popup;
 View popupview;
 LinearLayout linear;
 Button btnshow;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_popuptest);
       
        linear = (LinearLayout)findViewById(R.id.linear);
        popupview = View.inflate(this, R.layout.dialog_popup, null);
  popup = new PopupWindow(popupview,230,130,true);
  
        btnshow = (Button)findViewById(R.id.btnshow);     --> 이부분에서 btnshow가 널을 받아옵니다.
        btnshow.setOnClickListener(this);
       
        Button btnclose = (Button)popupview.findViewById(R.id.btnclose);
        btnclose.setOnClickListener(new Button.OnClickListener() {
         public void onClick(View v) {
       popup.dismiss();
         }
        });
 }

 @Override
 public void onClick(View v) {
  switch(v.getId()){
  case R.id.btnshow :
   
   // 가운데 놓기
   popup.showAtLocation(linear,Gravity.CENTER,0,0);
   
   popup.setAnimationStyle(-1);
   popup.showAsDropDown(btnshow);
   break;
  }
 }
}

 에러 메시지

04-19 05:07:50.869: ERROR/AndroidRuntime(4176): java.lang.RuntimeException: Unable to start activity ComponentInfo{exam.AndroidExam/exam.dialog.PopupTest}: java.lang.NullPointerException
04-19 05:07:50.869: ERROR/AndroidRuntime(4176):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
04-19 05:07:50.869: ERROR/AndroidRuntime(4176):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
04-19 05:07:50.869: ERROR/AndroidRuntime(4176):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
04-19 05:07:50.869: ERROR/AndroidRuntime(4176):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
04-19 05:07:50.869: ERROR/AndroidRuntime(4176):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-19 05:07:50.869: ERROR/AndroidRuntime(4176):     at android.os.Looper.loop(Looper.java:123)
04-19 05:07:50.869: ERROR/AndroidRuntime(4176):     at android.app.ActivityThread.main(ActivityThread.java:4363)
04-19 05:07:50.869: ERROR/AndroidRuntime(4176):     at java.lang.reflect.Method.invokeNative(Native Method)
04-19 05:07:50.869: ERROR/AndroidRuntime(4176):     at java.lang.reflect.Method.invoke(Method.java:521)
04-19 05:07:50.869: ERROR/AndroidRuntime(4176):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-19 05:07:50.869: ERROR/AndroidRuntime(4176):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-19 05:07:50.869: ERROR/AndroidRuntime(4176):     at dalvik.system.NativeStart.main(Native Method)
04-19 05:07:50.869: ERROR/AndroidRuntime(4176): Caused by: java.lang.NullPointerException
04-19 05:07:50.869: ERROR/AndroidRuntime(4176):     at exam.dialog.PopupTest.onCreate(PopupTest.java:27)
04-19 05:07:50.869: ERROR/AndroidRuntime(4176):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-19 05:07:50.869: ERROR/AndroidRuntime(4176):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)

책에 있는 popupTest를 수정한 코드입니다.

 

popup창에서 입력한 값을 가져오기 위해서 EditText에 id값을 줘야할거 같은데 id값을 줘버리면 에러가나니 답답한 마음으로

글 남겨봅니다.  id를 주지않으면 에러가 나지 않습니다.

발생하는 이유와 답보다는 해결하기위한 테스트 방향을 제시해주시면 감사하겠습니다.

질문은 처음이고, 공지사항 읽어봤습니다.  제대로 작성한 지 모르겠군요-;