2013-12-17 45 views
0

我似乎無法從一個擴展片段的類的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> 
+0

嘗試這裏發佈的解決方案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

+0

@CSmith嗯,沒有運氣= /謝謝你的迴應。任何其他建議? – BigBug

+1

我的歉意,我沒有注意「目前,這段代碼給了我一個空指針異常 - 當我添加onClickListener代碼」。我假設你的findViewById不起作用,所以我們需要看到更多的代碼(即什麼是變量「v」?) – CSmith

回答

1

它必須是你要找到它的任何訴範圍之外的ImageView指。

如果圖像視圖是一個列表視圖中,那麼你應該從適配器

0

imageView你正在努力尋找中添加onClickListener不是來自v訪問。如果imageView處於主佈局中,則使用findViewById而不使用v,否則首先對view進行充氣,然後使用view.findViewById

相關問題