I have experience getting ListAdapters working, and have looked online for tutorials on custom adapters to interface with a GridView, but I can’t seem to get mine working. I’m receiving a NullPointerException on my line:
image.setBackgroundColor(Color.GREEN);
My Activity and Adapter are as follows:
FixtureActivity:
package net.blakely.paul.Hyperion;
import android.os.Bundle;
import android.widget.GridView;
import com.actionbarsherlock.app.SherlockActivity;
public class FixtureActivity extends SherlockActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fixtures);
FixtureItemAdapter fia = new FixtureItemAdapter(this, 6);
GridView grids = (GridView) findViewById(R.id.grids);
grids.setAdapter(fia);
}
}
FixtureItemAdapter:
package net.blakely.paul.Hyperion;
import android.app.Activity;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class FixtureItemAdapter extends BaseAdapter {
private Activity context;
public int size;
private ImageView image;
private TextView label;
public FixtureItemAdapter(Activity context, int size) {
super();
this.context = context;
this.size = size;
}
public int getCount() {
// TODO Auto-generated method stub
return size;
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Log.v("Hyperion","convertView is Null?="+Boolean.toString(convertView==null));
if (convertView == null) {
LayoutInflater inflater = context.getLayoutInflater();
convertView = (LinearLayout) inflater.inflate(net.blakely.paul.Hyperion.R.layout.outputitem, null, true);
image = (ImageView) convertView.findViewById(net.blakely.paul.Hyperion.R.id.imageView1);
Log.v("Hyperion","image is Null?="+Boolean.toString(image==null));
label = (TextView) convertView.findViewById(net.blakely.paul.Hyperion.R.id.textView2);
}
// Decide which channel this is and set information accordingly
image.setBackgroundColor(Color.GREEN);
label.setText("LABEL");
return convertView;
}
}
Any help with this issue would be much appreciated. For what it’s worth. I’ve found that I get the NPE on the first run through the adapter, when convertView is null, and image winds up being null as well.
I’m sorry, but I found out my problem. It turned out that I was calling the wrong xml file, outputitem instead of fixtureitem, since I had copied that line from a different ListAdapter in the program. Thanks for your answers.