Я не могу получить событие onClick в представлении изображения из класса, который расширяет фрагмент. Что я здесь делаю неправильно? Я относительно новым для разработки для андроида ..onClick на изображении в пределах списка в диалоговом окне
В настоящее время этот код дает мне исключения нулевого указателя - когда я добавить код onClickListener
Вот мой код:
ImageView mImageView = (ImageView) v.findViewById(R.id.option_icon);
mImageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
}
});
эти иконки помещаются в виде списка, который находится на диалоге:
Диалог просмотра списка и кнопка:
<?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">
<ListView android:id="@+id/preset_list_view"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
<Button
android:id="@+id/dialogButtonOK"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="@string/ok"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_below="@+id/image"/>
</LinearLayout>
элементы в виде списка (ImageView и TextView):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/option_icon"
android:clickable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher"
/>
<TextView
android:id="@+id/option_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/image"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textSize="20dp" />
</RelativeLayout>
попробуйте решение, размещенное здесь http://stackoverflow.com/questions/8831043/listview-item-with-clickable-subview-sometime-cant-pass-the-click-event-to-the, т. Е. Добавить descendentFocusability в ваш listview RelativeLayout, также проверьте http://stackoverflow.com/questions/17502317/listview-imagebutton-descendantfocusability – CSmith
@CSmith hmmm no luck =/спасибо за ответ, хотя. любые другие предложения? – BigBug
мои извинения, я не обращал внимания на «В настоящее время этот код дает мне исключение нулевого указателя - когда я добавляю код onClickListener». Я предполагаю, что ваш findViewById не работает, поэтому нам нужно увидеть больше кода вокруг него (т. Е. Что такое переменная «v»?) – CSmith