I'm getting a null pointer exception

Following is the code of the GridAdaper.java used:

 package com.example.toggle;

 import java.util.ArrayList;
 import java.util.List;

 import com.example.toggle.R;

 import android.content.Context;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.ArrayAdapter;
 import android.widget.BaseAdapter;
 import android.widget.Toast;
import android.widget.ToggleButton;

public class GridAdapter extends ArrayAdapter{
private Context context;
private int layoutResourceId;
private ArrayList<String> data = new ArrayList();


public GridAdapter(Context context, int textViewResourceId, ArrayList<String> objects) {
    super(context, textViewResourceId, objects);
    // TODO Auto-generated constructor stub
    this.context= context;
    this.layoutResourceId = textViewResourceId;
    this.data = objects;

}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View gridItem = convertView;
    ViewHolder holder = null;
    if(gridItem == null)
    {
        LayoutInflater inflater = (LayoutInflater)        context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        gridItem = inflater.inflate(layoutResourceId, parent, false);
        holder = new ViewHolder();
        holder.toggle = (ToggleButton) gridItem.findViewById(R.id.togglebutton);
        gridItem.setTag(holder);
    }
    else
        holder = (ViewHolder) gridItem.getTag();
    String item = (String) data.get(position);
    data.add("add");
    if(item == "add")
        holder.toggle.setBackgroundResource(R.drawable.add_grid);

    return gridItem;
     }


   }

     class ViewHolder
   {
       ToggleButton toggle;
    }

The code for MainActivity.java is:

 public class MainActivity extends Activity {
    ArrayList<String> al;
   @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    GridView grid = (GridView) findViewById(R.id.gridView);
    al.add("add");
    GridAdapter ada = new GridAdapter(MainActivity.this, R.layout.grid_item, al);
    grid.setAdapter(ada);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

The main_activity.xml is :

<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
>
</GridView>

and the griditem.xml is :

<?xml version="1.0" encoding="utf-8"?>
<ToggleButton xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/togglebutton"
android:layout_width="match_parent"
android:layout_height="match_parent" 
>


 </ToggleButton>

The add_grid.xml works fine I've checked it! Please help me out. Thanks in advance!

Related posts

Recent Viewed