проблема с флажком Android - PullRequest
0 голосов
/ 13 марта 2011

У меня есть этот флажок в alerttdialog. Когда я пытаюсь проверить состояние флажка, приложение закрывается. есть идеи почему?

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>

Ответы [ 2 ]

1 голос
/ 13 марта 2011

Я думаю, проблема в том, что вы должны указать, в каком представлении найти идентификатор

Попробуйте это

final CheckBox checkBoxLab = 
                   (CheckBox) textDisplayView.findViewById(R.id.checkboxlab);
0 голосов
/ 13 марта 2011

Это фактический xml установлен как contenView?(поэтому, используя setContetnView() с этим xml где-то перед этим кодом)

В противном случае это:

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

сделает checkBoxLab нулевым указателем (null), и вы получите исключение nullPointerExceptionкак причина, где-то ниже вашей опубликованной ошибки.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...