GridView.setAdapter = nullPointerException

Интерфейс, диалоги, темы, стили, меню
Ответить
JDev
Сообщения: 88
Зарегистрирован: 05 авг 2012, 19:42

GridView.setAdapter = nullPointerException

Сообщение JDev » 17 сен 2012, 12:38

Подскажите пож, а то я не понимаю почему нульпоинтер мне валит.
Делаю диалоговое окно, одна кнопочка и 9 айтемов.
Кнопочка одна работает, а айтемы почему-то добавляться не хотят.. :(
Вылетает в этом месте.

Код: Выделить всё

		gvChoiceDialog = (GridView) findViewById(R.id.gvChoiceDialog);
		gvChoiceDialog.setAdapter(choiceAdapter);
- пробегал дебаггером, - действительно gvChoiceDialog = null
Но не могу понять почему. Вот код.

Код: Выделить всё

	private void choiceDialog() {

		String[] choiceData = new String[10];
		GridView gvChoiceDialog;
		ArrayAdapter<String> choiceAdapter;

		choiceDialog = new Dialog(context);
		choiceDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		choiceDialog.setContentView(R.layout.dialog_choice);

		for (int i = 0; i < 10; i++) {
			choiceData[i] = String.valueOf(i + 1);
		}

		choiceAdapter = new ArrayAdapter<String>(this, R.layout.item, R.id.tvText, choiceData);
		gvChoiceDialog = (GridView) findViewById(R.id.gvChoiceDialog);
		gvChoiceDialog.setAdapter(choiceAdapter);

		Button dialogButton = (Button) choiceDialog.findViewById(R.id.dialog_choice_btn_clear);

		dialogButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				choiceDialog.dismiss();
			}
		});

		choiceDialog.show();
	}
dialog_choice.xml

Код: Выделить всё

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/rect_gray"
    android:gravity="center"
    android:orientation="vertical" >

    <GridView
        android:id="@+id/gvChoiceDialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.35" >

    </GridView>

    <Button
        android:id="@+id/dialog_choice_btn_clear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.35"
        android:text="@string/dialog_choice_clear_text" />

</LinearLayout>
item.xml

Код: Выделить всё

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/rect_gray"
    android:gravity="center"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tvText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:minHeight="35dp"
        android:text=""
        android:textSize="20sp" >
    </TextView>

</LinearLayout>
Спасибо.

JDev
Сообщения: 88
Зарегистрирован: 05 авг 2012, 19:42

Re: GridView.setAdapter = nullPointerException

Сообщение JDev » 17 сен 2012, 13:40

Разобрался. :):):)
Моя ошибка была здесь

gvChoiceDialog = (GridView) choiceDialog.findViewById(R.id.gvChoiceDialog);

Ответить