我似乎無法從一個擴展片段的類的ImageView上的onClick事件。我在這裏做錯了什麼?我是比較新的Android開發方面的..onClick在一個對話框內的列表視圖中的一個圖像視圖
目前,該代碼給了我一個空指針異常 - 當我加入onClickListener代碼
這裏是我的代碼:
ImageView mImageView = (ImageView) v.findViewById(R.id.option_icon);
mImageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
}
});
這些圖標被放置在其被放置在對話列表視圖:
Dialog的列表視圖和按鈕:
<?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嗯,沒有運氣= /謝謝你的迴應。任何其他建議? – BigBug
我的歉意,我沒有注意「目前,這段代碼給了我一個空指針異常 - 當我添加onClickListener代碼」。我假設你的findViewById不起作用,所以我們需要看到更多的代碼(即什麼是變量「v」?) – CSmith