gridview适配器空指针异常

时间:2015-02-08 02:47:52

标签: android gridview nullpointerexception

应用程序崩溃,logcat输出为nullpointerException。相同的代码在以前的应用程序之前工作,但在此应用程输出错误是

  

“无法启动actvity componentinfo”

使用断点我在到达getView()

之前发现了应用崩溃

主要活动

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());

}

网格视图适配器类

   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;


}

}

1 个答案:

答案 0 :(得分:0)

您忘记实例化网格视图:

gridView = findViewById(R.id.gridveiew);