Search code examples
javaandroidandroid-gallery

Infinite Endless Gallery? Well,its not working for me


I did check those posts about Infinite gallery before posting this question but it didn't work for me. The gallery remained as it was. This piece of code : gallery.setSelection((int)(Integer.MAX_VALUE / 2) - (Integer.MAX_VALUE / 2)% mImageIds.length); doesn't affect the gallery even a bit. And whats more,instead of defining the gallery items in the onCreate itself just for getting the length for above setSelection() statement,I just wrote 7 instead of mImageIds.length since my gallery contains 7 items. But the LogCat started showing ArrayIndexOutOfBounds Exception. So what choice did I have? I did what everyone would do. I removed that 7 and undid the code to the point where it was running fine. But again,the LogCat didn't come out of that error. It really is frustating. Please help.

And it will be a lot helpful if someone can help with the Endless Gallery.

Thanking you in anticipation.

LogCat :

03-17 18:46:09.681: D/android.widget.GridLayout(626): horizontal constraints: x7 - x0 > 320, x4 - x3 > 87, x5 - x4 > 0, x6 - x5 > 77, x4 - x2 < 64, x4 - x3 < 64, x2 - x1 < 64, x1 - x0 < 64 are inconsistent; permanently removing: x7 - x0 < 320, x4 - x2 < 64, x4 - x3 < 64. 03-17 18:46:09.691: D/android.widget.GridLayout(626): vertical constraints: y1 - y0 > 21, y2 - y1 > 45, y3 - y2 > 42, y4 - y3 > 40, y5 - y4 > 10, y6 - y5 > 48, y7 - y6 > 0, y8 - y7 > 48, y9 - y8 > 0, y10 - y9 > 48, y11 - y10 > 0, y12 - y11 > 44, y12 - y10 > 48, y13 - y12 > 82, y13 - y0 < 199, y12 - y11 < 44 are inconsistent; permanently removing: y13 - y0 < 199. 03-17 18:46:09.751: D/AndroidRuntime(626): Shutting down VM 03-17 18:46:09.751: W/dalvikvm(626): threadid=1: thread exiting with uncaught exception (group=0x409c01f8) 03-17 18:46:09.771: E/AndroidRuntime(626): FATAL EXCEPTION: main 03-17 18:46:09.771: E/AndroidRuntime(626): java.lang.ArrayIndexOutOfBoundsException: length=7; index=1073741823 03-17 18:46:09.771: E/AndroidRuntime(626): at com.sanjay.frames.FramesActivity$ImageAdapter.getView(FramesActivity.java:86) 03-17 18:46:09.771: E/AndroidRuntime(626): at android.widget.Gallery.makeAndAddView(Gallery.java:844) 03-17 18:46:09.771: E/AndroidRuntime(626): at android.widget.Gallery.layout(Gallery.java:645) 03-17 18:46:09.771: E/AndroidRuntime(626): at android.widget.Gallery.onLayout(Gallery.java:346) 03-17 18:46:09.771: E/AndroidRuntime(626): at android.view.View.layout(View.java:11278) 03-17 18:46:09.771: E/AndroidRuntime(626): at android.view.ViewGroup.layout(ViewGroup.java:4224) 03-17 18:46:09.771: E/AndroidRuntime(626): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628) 03-17 18:46:09.771: E/AndroidRuntime(626): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486) 03-17 18:46:09.771: E/AndroidRuntime(626): at android.widget.LinearLayout.onLayout(LinearLayout.java:1399) 03-17 18:46:09.771: E/AndroidRuntime(626): at android.view.View.layout(View.java:11278) 03-17 18:46:09.771: E/AndroidRuntime(626): at android.view.ViewGroup.layout(ViewGroup.java:4224) 03-17 18:46:09.771: E/AndroidRuntime(626): at android.widget.FrameLayout.onLayout(FrameLayout.java:431) 03-17 18:46:09.771: E/AndroidRuntime(626): at android.view.View.layout(View.java:11278) 03-17 18:46:09.771: E/AndroidRuntime(626): at android.view.ViewGroup.layout(ViewGroup.java:4224) 03-17 18:46:09.771: E/AndroidRuntime(626): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628) 03-17 18:46:09.771: E/AndroidRuntime(626): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486) 03-17 18:46:09.771: E/AndroidRuntime(626): at android.widget.LinearLayout.onLayout(LinearLayout.java:1399) 03-17 18:46:09.771: E/AndroidRuntime(626): at android.view.View.layout(View.java:11278) 03-17 18:46:09.771: E/AndroidRuntime(626): at android.view.ViewGroup.layout(ViewGroup.java:4224) 03-17 18:46:09.771: E/AndroidRuntime(626): at android.widget.FrameLayout.onLayout(FrameLayout.java:431) 03-17 18:46:09.771: E/AndroidRuntime(626): at android.view.View.layout(View.java:11278) 03-17 18:46:09.771: E/AndroidRuntime(626): at android.view.ViewGroup.layout(ViewGroup.java:4224) 03-17 18:46:09.771: E/AndroidRuntime(626): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1489) 03-17 18:46:09.771: E/AndroidRuntime(626): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442) 03-17 18:46:09.771: E/AndroidRuntime(626): at android.os.Handler.dispatchMessage(Handler.java:99) 03-17 18:46:09.771: E/AndroidRuntime(626): at android.os.Looper.loop(Looper.java:137) 03-17 18:46:09.771: E/AndroidRuntime(626): at android.app.ActivityThread.main(ActivityThread.java:4424) 03-17 18:46:09.771: E/AndroidRuntime(626): at java.lang.reflect.Method.invokeNative(Native Method) 03-17 18:46:09.771: E/AndroidRuntime(626): at java.lang.reflect.Method.invoke(Method.java:511) 03-17 18:46:09.771: E/AndroidRuntime(626): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 03-17 18:46:09.771: E/AndroidRuntime(626): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 03-17 18:46:09.771: E/AndroidRuntime(626): at dalvik.system.NativeStart.main(Native Method)

Here's the code for my gallery :

public class FramesActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
   DisplayMetrics metrics=new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    Gallery gallery = (Gallery) findViewById(R.id.gallery);
    Integer[] mImageIds = {
            R.drawable.ic_launcher,
            R.drawable.ic_menu_template,
            R.drawable.th,
            R.drawable.wt,
            R.drawable.cur,
            R.drawable.ic_launcher,
            R.drawable.ic_launcher
    };     

    gallery.setAdapter(new ImageAdapter(this));
    gallery.setSelection((int)(Integer.MAX_VALUE / 2) - (Integer.MAX_VALUE / 2)%       mImageIds.length);

    gallery.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView parent, View v, int position, long id) {
            EditText editText2=(EditText) findViewById(R.id.editText2);
            editText2.setText(""+position);
        }
    });
}
public class ImageAdapter extends BaseAdapter {
    int mGalleryItemBackground;
    private Context mContext;

    private Integer[] mImageIds = {
            R.drawable.ic_launcher,
            R.drawable.ic_menu_template,
            R.drawable.th,
            R.drawable.wt,
            R.drawable.cur,
            R.drawable.ic_launcher,
            R.drawable.ic_launcher
    };

    public ImageAdapter(Context c) {
        mContext = c;
        TypedArray attr = mContext.obtainStyledAttributes(R.styleable.Frames);
        mGalleryItemBackground = attr.getResourceId(
                R.styleable.Frames_android_galleryItemBackground, 0);
        attr.recycle();
    }

    public int getCount() {
        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) {
        ImageView imageView = new ImageView(mContext);

        imageView.setImageResource(mImageIds[position]);
        imageView.setLayoutParams(new Gallery.LayoutParams(150, 100));
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setBackgroundResource(mGalleryItemBackground);

        return imageView;
    }
}   
}

Also,I am a newbie in android development.So if you see any other mistake in the above code,don't mind pointing them out.


Solution

  • You should pass the list to your adapter like so (or use an ArrayAdapter):

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       DisplayMetrics metrics=new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        Gallery gallery = (Gallery) findViewById(R.id.gallery);
        Integer[] mImageIds = {
                R.drawable.ic_launcher,
                R.drawable.ic_menu_template,
                R.drawable.th,
                R.drawable.wt,
                R.drawable.cur,
                R.drawable.ic_launcher,
                R.drawable.ic_launcher
        };     
    
        gallery.setAdapter(new ImageAdapter(this, mImageIds));
        gallery.setSelection((int)(Integer.MAX_VALUE / 2) - (Integer.MAX_VALUE / 2)%       mImageIds.length);
    
        gallery.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView parent, View v, int position, long id) {
                EditText editText2=(EditText) findViewById(R.id.editText2);
                editText2.setText(""+position);
            }
        });
    }
    public class ImageAdapter extends BaseAdapter {
        int mGalleryItemBackground;
        private Context mContext;
    
        private Integer[] mImageIds = null;
    
        public ImageAdapter(Context c, Integer[] imageIds) {
            mContext = c;
            mImageIds = imageIds;
            TypedArray attr = mContext.obtainStyledAttributes(R.styleable.Frames);
            mGalleryItemBackground = attr.getResourceId(
                    R.styleable.Frames_android_galleryItemBackground, 0);
            attr.recycle();
        }
    
        public int getCount() {
            return (mImageIds != null) ? mImageIds.length : 0;
        }
    // etc
    

    if that's not helping post the error from the log and highlight the line where it happens.

    Edit:

    gallery.setSelection((int)(Integer.MAX_VALUE / 2) - (Integer.MAX_VALUE / 2)%       mImageIds.length);
    

    is causing the Error.
    (2000 / 2) - (2000 / 2) % 7 = 1000 - 1000 % 7 = 1000 - (1000 % 7) = 1000 - 6 = Way too big.

    The result must be 0..6 if mImageIds.size == 7

    What should that line do?