0

I can't seem to get an onClick event on an imageview from a class that extends Fragment. What am i doing wrong here? I'm relatively new to developing for android..

Currently, this code gives me a null pointer exception - when i add the onClickListener code

Here is my code:

ImageView mImageView = (ImageView) v.findViewById(R.id.option_icon);
    mImageView.setOnClickListener(new OnClickListener() {
       @Override
       public void onClick(View arg0) {

    }
});

these icons are placed in a listview which is placed on a Dialog:

Dialog's list view and button:

<?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>

items within the list view (imageview and 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>
BigBug
  • 6,202
  • 23
  • 87
  • 138
  • try the solution posted here http://stackoverflow.com/questions/8831043/listview-item-with-clickable-subview-sometime-cant-pass-the-click-event-to-the, i.e. add descendentFocusability to your listview RelativeLayout, also check http://stackoverflow.com/questions/17502317/listview-imagebutton-descendantfocusability – CSmith Dec 17 '13 at 17:08
  • @CSmith hmmm no luck =/ thanks for the response though. any other suggestions? – BigBug Dec 17 '13 at 17:13
  • 1
    my apologies, I didn't pay attention to "Currently, this code gives me a null pointer exception - when i add the onClickListener code ". I assume your findViewById isn't working, so we need to see more code around that (i.e. what is variable "v"?) – CSmith Dec 17 '13 at 17:18
  • OHHH, i see, okay, now it makes sense. i got it. thanks a million!!! – BigBug Dec 17 '13 at 17:20

2 Answers2

1

It must be that the ImageView you're trying to find it outside the scope of whatever v refers to.

If the image view is inside a list view then you should add the onClickListener from within the adapter

Dreagen
  • 1,733
  • 16
  • 21
0

The imageView that you are trying to find is not accessible from v. If the imageView is in the main layout then use findViewById without using v else first inflate the view and then use view.findViewById.

Rohan Kandwal
  • 9,112
  • 8
  • 74
  • 107