2

I write app that include Gridview ,for display products,thus when select item in grid, return value is nullpointerException.when i use listview its okay but in Gridview its error.

grid_data.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {

        title = ((TextView) view.findViewById(R.id.title)).getText()
                .toString();

        categoriid = ((TextView) view.findViewById(R.id.categoryid))
                .getText().toString();

        String productid = ((TextView) view
                .findViewById(R.id.productid)).getText().toString();

    }
});

adapter class :

public View getView(int position, View convertView, ViewGroup parent) {



View vi = convertView;
if (vi  == null)


//LayoutInflater inflater = ((Activity) activity).getLayoutInflater();
vi = inflater.inflate(R.layout.full_image, parent, false);

TextView title = (TextView) vi.findViewById(R.id.txt_title); // title
TextView     maxprice = (TextView) vi.findViewById(R.id.txt_maxprice); // artist
TextView minprice = (TextView) vi.findViewById(R.id.txt_minprice); // artist
TextView productid = (TextView) vi.findViewById(R.id.productid); // artist
TextView categoryid = (TextView) vi.findViewById(R.id.categoryid); // artist
ImageView    thumb_image = (ImageView) vi
        .findViewById(R.id.full_image_view); // thumb
                                                // image

HashMap<String, String> song = new HashMap<String, String>();
song = data.get(position);
title.setText(song.get(Service.Key_Title));
productid.setText(song.get("Productid"));
categoryid.setText(song.get("Categoryid"));
maxprice.setText(song.get("MaxPrice"));
minprice.setText(song.get("MinPrice"));

imageLoader.DisplayImage(song.get("picPath"), thumb_image);
return vi;

}

full_image.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:orientation="vertical" >

    <ImageView
        android:id="@+id/full_image_view"
        android:layout_width="80sp"
        android:layout_height="80sp"
        android:background="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/txt_title"
        android:layout_width="80sp"
        android:layout_height="wrap_content"
        android:text="title" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/txt_maxprice"
            android:layout_width="70sp"
            android:layout_height="wrap_content"
            android:text="maxprice" />

        <ImageView
            android:id="@+id/image_view"
            android:layout_width="10sp"
            android:layout_height="10sp"
            android:background="@drawable/ic_launcher" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/txt_minprice"
            android:layout_width="70sp"
            android:layout_height="wrap_content"
            android:text="minprice" />

        <ImageView
            android:id="@+id/image_view2"
            android:layout_width="10sp"
            android:layout_height="10sp"
            android:background="@drawable/ic_launcher" />
    </LinearLayout>

    <TextView
        android:id="@+id/productid"
        android:layout_width="1sp"
        android:layout_height="1sp"
        android:visibility="gone" />

    <TextView
        android:id="@+id/categoryid"
        android:layout_width="1sp"
        android:layout_height="1sp"
        android:visibility="gone" />

</LinearLayout>

how to resolve problem that click in gridview value of title or other data save in variable thanks

4
  • why do you use the second inflator?[vi = inflater.inflate(R.layout.full_image, parent, false);] you already did it in the if statement. Commented Sep 23, 2013 at 19:15
  • yes its extras...vi = inflater.inflate(R.layout.full_image, parent, false); true... but error codes Commented Sep 23, 2013 at 19:17
  • just use the previous line vi = inflater.inflate(R.layout.full_image,null) Commented Sep 23, 2013 at 19:22
  • can you post the error log, and xml layout for full_image Commented Sep 23, 2013 at 19:31

1 Answer 1

1

Do you have a typo? In your getView method you are

TextView title = (TextView) vi.findViewById(R.id.txt_title); // title

but in your OnItemClickListener you are looking for something else

title = ((TextView) view.findViewById(R.id.title)).getText().toString()

See?

Sign up to request clarification or add additional context in comments.

1 Comment

If this helped you or was the answer, can you mark it as such or upvote it, please?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.