GridView Clickable

时间:2015-05-13 10:16:04

标签: android android-gridview

我正在努力使这个gridview可点击。一切都很好,但最后一个细胞。当我点击最后一个单元格,即第8个单元格时,程序崩溃。任何有关崩溃原因的建议都会有所帮助。



Blue-Complex

public class MainView extends Activity {
	
	ImageView back, home, site;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);       
   	 setContentView(R.layout.mainview);

        GridView gridView = (GridView)findViewById(R.id.gridview);
        gridView.setAdapter(new MyAdapter(this));
        
        gridView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {
           	
            	 Intent myIntent = null;
                 if(position == 0){
                     myIntent = new Intent(v.getContext(), TheShow.class);
                 }
                 if(position == 1){
                     myIntent = new Intent(v.getContext(), Exhibitor.class);
                 }
                 if(position ==2){
                     myIntent = new Intent(v.getContext(), Visitor.class);
                 }  
                 if(position ==3){
                     myIntent = new Intent(v.getContext(), Conference.class);
                 }  
                 if(position ==4){
                     myIntent = new Intent(v.getContext(), ContactUs.class);
                 }
                 if(position == 5){
                	 myIntent = new Intent(v.getContext(), VisitorReg.class);
                 }
                 if(position == 6){
                	 myIntent = new Intent(v.getContext(), ExhibitorReg.class);
                 }
                 
                 if(position == 7){
                	 Toast.makeText(getApplicationContext(), "Pending", 4000).show();
                 }
                
                 startActivity(myIntent);          	
            	
            }
        });       
    }

    private class MyAdapter extends BaseAdapter
    {
        private List<Item> items = new ArrayList<Item>();
        private LayoutInflater inflater;

        public MyAdapter(Context context)
        {
            inflater = LayoutInflater.from(context);

            items.add(new Item("The Show", R.drawable.globecopy1));
            items.add(new Item("Exhibitor", R.drawable.workcopy1));
            items.add(new Item("Visitor", R.drawable.visitor1copy1));
            items.add(new Item("Conference", R.drawable.conferencecopy1));
            items.add(new Item("Contact Us", R.drawable.contactus1copy1));
            items.add(new Item("Visitor Registration", R.drawable.registercopy1));
            items.add(new Item("Exhibitor Registration", R.drawable.exregistercopy1));
            items.add(new Item("Delegate Registration", R.drawable.anothercopy1));
        }

        @Override
        public int getCount() {
            return items.size();
        }

        @Override
        public Object getItem(int i)
        {
            return items.get(i);
        }

        @Override
        public long getItemId(int i)
        {
            return items.get(i).drawableId;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup)
        {
            View v = view;
            ImageView picture;
            TextView name;

            if(v == null)
            {
               v = inflater.inflate(R.layout.gridview_item, viewGroup, false);
               v.setTag(R.id.picture, v.findViewById(R.id.picture));
               v.setTag(R.id.text, v.findViewById(R.id.text));
            }

            picture = (ImageView)v.getTag(R.id.picture);
            name = (TextView)v.getTag(R.id.text);

            Item item = (Item)getItem(i);

            picture.setImageResource(item.drawableId);
            name.setText(item.name);

            return v;
        }

        private class Item
        {
            final String name;
            final int drawableId;

            Item(String name, int drawableId)
            {
                this.name = name;
                this.drawableId = drawableId;
            }
        }
    }

}
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:3)

您的myIntent在最后一个条件中是null。这就是崩溃发生的原因。因此,在启动之前检查myIntent是否为null。请尝试以下方法:

if(myIntent!=null){
   startActivity(myIntent); 
}

答案 1 :(得分:0)

这里有8个项目,正如你所说,你选择的是最后一个位置,即没有7个位置。并且您已将Intent声明为null,并且在检查条件时您没有设置Intent的值。

if(position == 7)
{
   Toast.makeText(getApplicationContext(), "Pending", 4000).show();
}

将上述代码替换为此

if(position == 7)
{
      myIntent = new Intent(v.getContext(), YOUR_CLASS_NAME.class);
}