private int[] myImage={R.drawable.photo2,R.drawable.photo3,R.drawable.photo6};
作为主类的成员变量,不要做内部类ImageAdapter的成员变量。

解决方案 »

  1.   

    我看到相应的例子,private int[] myImage也是放在内部类ImageAdapter中的,郁闷的是,我的图片怎么也显示不出来。
      

  2.   

    public Object getItem(int position)     {return position;}
    这个好像有问题吧,应该返回int的position么?
      

  3.   

    public Object getItem(int position)     {return position;}
    我也觉得这有问题,返回空试试,不是没data么
      

  4.   

    public Object getItem(int position) {return position;}
    这个函数没问题。下面是我的ImageAdapter,好些也没什么差别,只多了一个函数getScale
    // Inner class to rewrite BaseAdapter
        public class ImageAdapter extends BaseAdapter {
         private Context mContext;
        
         public ImageAdapter(Context c){
         this.mContext = c;
         }
        
         public int getCount() { 
         // 第1点改进,返回一个很大的值,例如,Integer.MAX_VALUE
    //     return Integer.MAX_VALUE; // mImageIds.length; //获取图片的个数
         return mImageIds.length; //获取图片的个数
         }
        
         public Object getItem(int position) {
         return position;// 获取图片在库中的位置
         }
        
         public long getItemId(int position) {
         return position;// 获取图片在库中的位置 
         } 
        
         public View getView(int position, View convertView, ViewGroup parent) {
         Log.i(TAG, "position = " + position);
         ImageView imageView = new ImageView(mContext);
        
         // 第2点改进,通过取余来循环取得resIds数组中的图像资源ID
         imageView.setImageResource(mImageIds[position % mImageIds.length]);
         imageView.setLayoutParams(new Gallery.LayoutParams(100, 120));// 设置图片大小
         imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);// 设置显示比例类型
         imageView.setPadding(2,0,2,0);
         imageView.setBackgroundResource(R.drawable.gallery_style);
         imageId = mImageIds[position % mImageIds.length];
         Log.i(TAG, "getView, imageId = " + imageId);
        
         return imageView;
         }
        
         /* 依据距离中央的位移量 利用getScale返回views的大小(0.0f to 1.0f) */
            public float getScale(boolean focused, int offset)
            {
             return Math.max(0, 1.0f / (float) Math.pow(2, Math.abs(offset)));
            }
        }
      

  5.   

           public Object getItem(int position) {
                return position;// 获取图片在库中的位置
            }
    不是返回该位置对象么。怎么会是位置呢?
      

  6.   

    会不会是资源引用出了问题;myImage[]?
      

  7.   

    楼主给分:import android.content.Context;
    import android.content.res.TypedArray;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.Gallery;
    import android.widget.ImageView;
    public class ImageAdapter extends BaseAdapter
    {
    int mGalleryItemBackground;
    private Context mContext;

    public ImageAdapter(Context c){
    mContext = c;
    TypedArray a = mContext.obtainStyledAttributes(R.styleable.Gallery);

    mGalleryItemBackground = a.getResourceId
    (R.styleable.Gallery_android_galleryItemBackground, 0);

    a.recycle();
    }

    @Override
    public int getCount() {
    // TODO Auto-generated method stub
    return myImageIds.length;
    } @Override
    public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
    } @Override
    public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
    } @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ImageView i = new ImageView(mContext);
    i.setImageResource(myImageIds[position]);
    i.setScaleType(ImageView.ScaleType.FIT_XY);
    i.setLayoutParams(new Gallery.LayoutParams(136, 88));
    i.setBackgroundResource(mGalleryItemBackground);
    return i;
    }


    private Integer[] myImageIds =
    {
    R.drawable.photo1,
    R.drawable.photo2,
    R.drawable.photo3,
    R.drawable.photo4,
    R.drawable.photo5,
    R.drawable.photo6,
    };
    }详情 请见: 书籍: Google Android SDK开发范例大全
    例子:Ex04_10随便 到网站上去下载就OK了
      

  8.   

    看看 后台 报错没 是不是 1.6 的 工程 在 2.0 以上 运行啊 有时 会出现 找不到资源的问题
    2.0 res下建drawable文件夹 放置图片资源
      

  9.   

    <Gallery android:layout_width="wrap_parent"/>改成]<Gallery android:layout_width="fill_parent"/>你试试