0

I am trying to add Text below my Image in Grid View. I am getting Null point Exception error.

Below is the layout file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:orientation="vertical"
   android:gravity="center_horizontal"
   android:background="#000080">

   <imageview android:id="@+id/grid_item_image"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content">
   </imageview>

   <textview android:id="@+id/grid_item_text"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="TextView"
      android:gravity="center_horizontal"
      android:textColor="#000000">
   </textview>

</LinearLayout>

The ImageAdapter class has below code:

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class ImageAdapter extends BaseAdapter {
    Context mContext;
    private LayoutInflater mInflater;

    // Keep all Images in array
    public Integer[] mThumbIds = {
            R.drawable.image1, R.drawable.image2,
            R.drawable.image6, R.drawable.image3,
            R.drawable.image5, R.drawable.image4



    };

    // Constructor
    public ImageAdapter(Context c){
        mContext = c;
        mInflater = LayoutInflater.from(c);
    }

    public ImageAdapter(Residential residential) {
        // TODO Auto-generated constructor stub
    }

    @Override
    public int getCount() {
        return mThumbIds.length;
    }

    @Override
    public Object getItem(int position) {
        return mThumbIds[position];
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

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

        View MyView = convertView;

        if (convertView == null)
        {

        convertView = mInflater.inflate(R.layout.grid_item, 
                    parent,false);

        TextView tv = (TextView)MyView.findViewById(R.id.grid_item_text);
        tv.setText("Item");

        ImageView imageView = (ImageView)MyView.findViewById(R.id.grid_item_image);

        //ImageView imageView = new ImageView(mContext);
        imageView.setImageResource(mThumbIds[position]);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(new GridView.LayoutParams(190, 100));
        }
        return MyView;
    }

}

It's throwing null exception error at below line:

convertView = mInflater.inflate(R.layout.grid_item, 
                    parent,false);

I tried with various option suggested but all giving same results

LayoutInflater li = ((Activity) mContext).getLayoutInflater();

LayoutInflater li = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

Please suggest how to resolve this. Thanks.

Gaurav
  • 535
  • 1
  • 8
  • 28

3 Answers3

1

From your comment's you said

    Residential is the gridview xml file

So use

convertView = mInflater.inflate(R.layout.resedential, 
                parent,false);  

Also use convertview to initialize your views

TextView tv = (TextView)convertView.findViewById(R.id.grid_item_text);

and return convertView.

You should also consider using a ViewHolder.

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

Use a ViewHolder

static class ViewHolder
{
       TextView textview;
       ImageView imageView;
}

In getView

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

    ViewHolder vh;

    if (convertView == null)
    {
    vh = new ViewHolder();
    convertView = mInflater.inflate(R.layout.resedential, 
                parent,false);
    vh.tv = (TextView)convertView.findViewById(R.id.grid_item_text)
    vh.imageView = (ImageView)convertView.findViewById(R.id.grid_item_image);
    convertView.setTag(vh); 
    }
    else 
    { 
    vh = (ViewHolder) convertView.getTag();  
    }       
    vh.tv.setText("Item");   
    vh.imageView.setImageResource(mThumbIds[position]);
    vh.imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    vh.imageView.setLayoutParams(new GridView.LayoutParams(190, 100));

    return convertView;
}
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

Since MyView is pointing to convertView which could be null but after assigning a new view you have not updated MyView with new convertView.

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

        View MyView = convertView;

        if (convertView == null)
        {

        convertView = mInflater.inflate(R.layout.grid_item, 
                    parent,false);
MyView = convertView;
        TextView tv = (TextView)MyView.findViewById(R.id.grid_item_text);
        tv.setText("Item");

        ImageView imageView = (ImageView)MyView.findViewById(R.id.grid_item_image);

        //ImageView imageView = new ImageView(mContext);
        imageView.setImageResource(mThumbIds[position]);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(new GridView.LayoutParams(190, 100));
        }
        return MyView;
    }
Vivek Khandelwal
  • 7,829
  • 3
  • 25
  • 40
0

That's how inflate works:

static View inflate(Context context, int resource, ViewGroup root)
Inflate a view from an XML resource.

So you're passing wrong params..

so

convertView= mInflater.inflate(getBaseContext(),R.layout.grid_item,parent);
Tizianoreica
  • 2,142
  • 3
  • 28
  • 43
  • Thanks. I changed the parameters but it's giving 'The method getBaseContext() is undefined for the type ImageAdapter' error. – Gaurav Jul 31 '13 at 08:12
  • 1
    don't think that's a problem http://developer.android.com/reference/android/view/LayoutInflater.html#inflate(int, android.view.ViewGroup, boolean). check the docs – Raghunandan Jul 31 '13 at 08:34
  • @Raghunandan pls help me in this code http://stackoverflow.com/questions/22986878/android-json-is-not-able-to-retrieve-any-files-from-mysql-database-it-is-empty/22987428?noredirect=1#22987428 – Sandeep V Apr 10 '14 at 12:16