Gihan Dilusha
Gihan Dilusha

Reputation: 921

Android Gridview not working

I wrote a code for android grid view, but unfortunately when i run the code Application returns error like this "The Appication Nues Hound RSS(process com.nues.rss) has stoped unexpectedly Please try agian later"

If anyone knows how to solve this please help out me. I cant find any coding error. So experts are welcome to give proper instructions.

This is my activity class

public void onCreate(Bundle savedInstanceState) {

    if (!isNetworkAvailbale()) {
        Toast.makeText(getApplicationContext(), "Internet Connection not available", Toast.LENGTH_SHORT).show();
        finish();
    }

    super.onCreate(savedInstanceState);
    setContentView(R.layout.gridview);

    GridView gridView;

    ImageAdapter mAdapter;

    mAdapter = new ImageAdapter(this);

    gridView = (GridView) findViewById(R.id.gridview);

    gridView.setAdapter(mAdapter);

    gridView.setOnItemClickListener(new OnItemClickListener() {@
        Override
        public void onItemClick(AdapterView <? > arg0, View arg1, int position, long

            arg3) {
            Toast.makeText(NuesHoundRSSActivity.this, "Item co" + osition, Toast.LENGTH_SHORT).show();
        }
    });
}

This is the adapter class

package com.nues.rss;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {
    private Activity activity;  

public ImageAdapter(Context c) {

}

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

public Object getItem(int position) {
    return null;
}

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

   // create a new ImageView for each item referenced by the Adapter
public static class ViewHolder  
{  
    public ImageView imgView;    
}  

@Override  
public View getView(int position, View convertView, ViewGroup parent) {  
    // TODO Auto-generated method stub  
    ViewHolder view;  
    LayoutInflater inflator = activity.getLayoutInflater();  

    if(convertView==null)  
    {  
        view = new ViewHolder();  
        convertView = inflator.inflate(R.layout.gridview_row, null);  

        view.imgView = (ImageView) convertView.findViewById(R.id.imageView1);  

        convertView.setTag(view);  

    }  
    else  
    {  
        view = (ViewHolder) convertView.getTag();  
    }  

    view.imgView.setImageResource(mThumbIds[position]);  

    return convertView;  
}  

// references to our images
private Integer[] mThumbIds = {
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7,
        R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7,
        R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7
};


}

This is my Gridview XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="5dp">

<ImageView
    android:layout_height="64dp"
    android:id="@+id/imageView1"
    android:layout_width="64dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true">
</ImageView>

<TextView
    android:text="TextView"
    android:layout_height="wrap_content"
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_below="@+id/imageView1"
    android:layout_marginTop="2dp"
    android:layout_centerHorizontal="true"
    android:textSize="18sp"
    android:ellipsize="marquee"></TextView>
</RelativeLayout>

Upvotes: 0

Views: 3744

Answers (3)

Harish
Harish

Reputation: 3117

private Activity activity;  

public ImageAdapter(Context c) {

}

replace this with the following

private Context c;

        public ImageAdapter(Context c) {
            this.c = c;

        }

And

LayoutInflater inflator = activity.getLayoutInflater();  

with

LayoutInflater inflator = ((Activity) c).getLayoutInflater(); 

this will work fine.

Upvotes: 2

Joru
Joru

Reputation: 4436

In your code private Activity activity; is never set to anything and is null, so activity.getLayoutInflater(); crashes.

You want to do something like this:

private Context context; 

public ImageAdapter(Context c) { 
    this.context = c;
} 

And then context.getLayoutInflater();.

Upvotes: 0

Zaz Gmy
Zaz Gmy

Reputation: 4356

see here:

public class ImageAdapter extends BaseAdapter {
        private Activity activity;  

    public ImageAdapter(Context c) {

    }

you are not initialize your activity object.

try this:

 public ImageAdapter(Activity ac) {
    this.activity=ac;
    }

Upvotes: 0

Related Questions