gridview适配器空指针异常

gridview adaptor null pointer exception

应用程序崩溃并且 logcat 输出为 nullpointerException。相同的代码在之前的应用程序中有效,但在此应用程序中无效。输出错误为

"unable to start actvity componentinfo"

使用断点我发现应用程序在到达 getView()

之前崩溃

主要activity

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    System.out.println("main activity1");
    setContentView(R.layout.maingrid);
    System.out.println("main activity2");
    gridView.setAdapter(new GridAdapter(this,ITEM_LIST ));

    gridView.setOnItemClickListener(new GridClick());

}

网格视图适配器class

   public class GridAdapter extends BaseAdapter
  {
     private Context context;
     private final String[] itemName;

    public GridAdapter(Context context, String[] itemName) {
    System.out.println("gridadaptor");
    this.context = context;
    this.itemName = itemName;
    System.out.println("gridadaptor finish");
}


public int getCount() {
    System.out.println("getcount");
    return itemName.length;
}


public Object getItem(int position) {

    return null;
}



public long getItemId(int position) {

    return 0;
}


public View getView(int position, View convertView, ViewGroup parent) {
    System.out.println("getview1");
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View gridView = null;
    System.out.println("getview");
    if (convertView==null)
            {
                gridView = new View(context);
                gridView = inflater.inflate(R.layout.griditem, null);

                TextView textView = (TextView) gridView.findViewById(R.id.grid_label);
                textView.setText(itemName[position]);

                ImageView imageView = (ImageView) gridView.findViewById(R.id.grid_image);
                String item = itemName[position];

            if (item.equalsIgnoreCase("Stock Manager")) 
            {
                imageView.setImageResource(R.drawable.stock);
            }

            else if (item.equalsIgnoreCase("Sales Report")) 
            {
                imageView.setImageResource(R.drawable.sales);
            }

            else if (item.equalsIgnoreCase("Purchase Report"))  
            {
                imageView.setImageResource(R.drawable.purchase);
            }

            else if (item.equalsIgnoreCase("Collateral Report"))    
            {
                imageView.setImageResource(R.drawable.collateral);
            }

            }
            else 
            {
                gridView = (View) convertView;

            }

            return gridView;


}

}

您忘记实例化您的网格视图:

gridView = findViewById(R.id.gridveiew);