2011-03-13 5 views
0

나는 alertdialog에이 확인란을 가지고 있습니다. 확인란의 상태를 확인하려고하면 애플리케이션 강제 종료됩니다. 왜 어떤 생각?android 확인란 상자 문제

LayoutInflater factory = LayoutInflater.from(NewActivity.this); 
     final View textDisplayView = factory.inflate(R.layout.nearestlocs, null); 
     final AlertDialog.Builder newAlert = new AlertDialog.Builder(NewActivity.this); 
     newAlert.setView(textDisplayView); 

     final CheckBox checkBoxLab = (CheckBox) findViewById(R.id.checkboxlab); 
newAlert.setPositiveButton("Display on Map", 
       new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int whichButton) { 

       if(checkBoxLab.isChecked()){ 
        libDisplayFlag = true; 
       } 

오류 로그

03-13 08:01:58.273: ERROR/AndroidRuntime(6188): Uncaught handler: thread main exiting due to uncaught exception 
    03-13 08:01:58.292: ERROR/AndroidRuntime(6188): java.lang.NullPointerException 
    03-13 08:01:58.292: ERROR/AndroidRuntime(6188):  at com.isproj3.NewActivity$3.onClick(NewActivity.java:158) 
    03-13 08:01:58.292: ERROR/AndroidRuntime(6188):  at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158) 
    03-13 08:01:58.292: ERROR/AndroidRuntime(6188):  at android.os.Handler.dispatchMessage(Handler.java:99) 

XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <LinearLayout android:orientation="horizontal" 
     android:gravity="center" android:layout_width="fill_parent" 
     android:layout_height="fill_parent" android:layout_weight="1"> 
     <CheckBox android:id="@+id/checkboxlib" android:layout_width="wrap_content" 
      android:layout_height="wrap_content" android:text="Library" 
      android:gravity="left" android:textColor="#FF0000" 
      android:paddingBottom="5px" android:textSize="07pt" android:checked="true" /> 
     <TextView android:id="@+id/librarytext" android:layout_width="wrap_content" 
      android:layout_height="wrap_content" android:gravity="center" 
      android:paddingBottom="5px" android:textSize="8pt" /> 
    </LinearLayout> 
</LinearLayout> 

답변

1

나는 문제가 당신이하는이

을 시도해보십시오 ID

을 찾아 볼 지정해야한다는 생각

final CheckBox checkBoxLab = 
        (CheckBox) textDisplayView.findViewById(R.id.checkboxlab); 
+1

아. 일하고 신 .. 두 번째 연속으로 나는이 실수를 저질렀다. –

0

xml이 contenView로 설정되어 있습니까?

final CheckBox checkBoxLab = (CheckBox) findViewById(R.id.checkboxlab); 

이 checkBoxLab에게 nullpointer (null)을 만들 것, 그리고 당신은 somehwere하여 게시 오류 아래 원인으로는 NullPointerException를 얻을 수 있습니다 : 그렇지 않으면이

을 (그래서 곳이 코드 전에 XML에 setContetnView() 사용) .