Gallery中的NullPointerException

时间:2011-12-01 06:47:20

标签: android

我正在尝试制作一个包含资源文件夹中所有图片的图库,但我一直收到NullPointerException错误,有人能告诉我哪里出错了吗?

public class MyGallery extends Activity {

        private GridView gv;
        private SlidingDrawer sd;
        private ImageView im;
        private int[] icons={R.drawable.download,R.drawable.setwallpaper,
                       R.drawable.list,R.drawable.other};
        private String[] items={"下载到本地","设置为桌面","列表查看","支持本APP"};


      @Override
      public void onCreate(Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
            gv = (GridView)findViewById(R.id.grid); 
            sd = (SlidingDrawer)findViewById(R.id.drawer);
            im=(ImageView)findViewById(R.id.openicon);

            /* 使用告定义的MyGridViewAdapter设置GridView里面的item内容 */
            MyGridViewAdapter adapter=new MyGridViewAdapter(this,items,icons);
            gv.setAdapter(adapter);

            /* 设定SlidingDrawer被打开的事件处理 */
            sd.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener()
            {
              @Override
              public void onDrawerOpened()
              {
                im.setImageResource(R.drawable.closedrawer);
              }
            });
            /* 设置SlidingDrawer被关闭的事件处理 */
            sd.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener()
            {
              @Override
              public void onDrawerClosed()
              {
                im.setImageResource(R.drawable.showdrawer);
              }
            });

            Log.d("tag", "抽屉初始化成功");

            Gallery g = (Gallery) findViewById(R.id.mygallery);

            /*新增几ImageAdapter并设定给Gallery对象*/
            g.setAdapter(new ImageAdapter(this,getImage()));

            /*设定一个itemclickListener事件*/
            g.setOnItemClickListener(new OnItemClickListener() 
            {
              public void onItemClick(AdapterView<?> parent, 
                               View v, int position, long id) 
              { 

              }
            });

            Log.d("tag", "图片初始化成功");

          }
      private List<String> getImage()
      {
        /* 设定目前所在路径 */
        List<String> it=new ArrayList<String>();      
        File f=new File("file:///android_asset/");  
        File[] files=f.listFiles();
        Log.d("tag", "读取asset资源");

        /* 将所有文件存入ArrayList中 */
        for(int i=0;i<files.length;i++)
        {
          File file=files[i];
          if(getImageFile(file.getPath()))
            it.add(file.getPath());
        }
        return it;

      }

      private boolean getImageFile(String fName)
      {
        boolean re;

        /* 取得扩展名 */
        String end=fName.substring(fName.lastIndexOf(".")+1,
                      fName.length()).toLowerCase(); 

        /* 按扩展名的类型决定MimeType */
        if(end.equals("jpg")||end.equals("gif")||end.equals("png")
                ||end.equals("jpeg")||end.equals("bmp"))
        {
          re=true;
        }
        else
        {
          re=false;
        }
        return re; 
      }

      /*改写BaseAdapter自定义一ImageAdapter class*/
      public class ImageAdapter extends BaseAdapter 
      {
        /*声明变量*/
        int mGalleryItemBackground;
        private Context mContext;
        private List<String> lis;

        /*ImageAdapter的构造符*/
        public ImageAdapter(Context c,List<String> li) 
        {
          mContext = c;
          lis=li;
          /* 使用res/values/attrs.xml中的<declare-styleable>定义
          * 的Gallery属性.*/
          TypedArray a = obtainStyledAttributes(R.styleable.Gallery);
          /*取得Gallery属性的Index id*/
          mGalleryItemBackground = a.getResourceId(
              R.styleable.Gallery_android_galleryItemBackground, 0);
          /*让对象的styleable属性能够反复使用*/ 
          a.recycle();
        }

        /*几定要重写的方法getCount,传回图片数目*/
        public int getCount() 
        {
          return lis.size();
        }

        /*一定要重写的方法getItem,传回position*/
        public Object getItem(int position) 
        {
          return position;
        }

        /*一定要重写的方法getItemId,传并position*/
        public long getItemId(int position) 
        {
          return position;
        }

        /*几定要重写的方法getView,传并几View对象*/
        public View getView(int position, View convertView, 
                              ViewGroup parent) 
        {
          /*产生ImageView对象*/
          ImageView i = new ImageView(mContext);
          /*设定图片给imageView对象*/
          Bitmap bm = BitmapFactory.decodeFile(lis.
                                get(position).toString());
          i.setImageBitmap(bm);
          /*重新设定图片的宽高*/
          i.setScaleType(ImageView.ScaleType.FIT_XY);
          /*重新设定Layout的宽高*/
          i.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
          /*设定Gallery背景图*/
          i.setBackgroundResource(mGalleryItemBackground);
          /*传回imageView对象*/
          return i;
        }     
      } 
    }

我真的很感激。

1 个答案:

答案 0 :(得分:0)

您是否尝试从项目的资源文件夹中加载图片???那么,在这种情况下尝试通过AssetManager读取资产文件。我认为这可能会有所帮助