StaggeredGridView
StaggeredGridView copied to clipboard
Crash on change screen orientation
arrayIndexoutofboundsexception after screen rotation
Me too: 09-16 16:38:20.682: E/AndroidRuntime(22002): FATAL EXCEPTION: main 09-16 16:38:20.682: E/AndroidRuntime(22002): java.lang.ArrayIndexOutOfBoundsException: length=3; index=3 09-16 16:38:20.682: E/AndroidRuntime(22002): at com.origamilabs.library.views.StaggeredGridView.populate(StaggeredGridView.java:975) 09-16 16:38:20.682: E/AndroidRuntime(22002): at com.origamilabs.library.views.StaggeredGridView.onLayout(StaggeredGridView.java:929) 09-16 16:38:20.682: E/AndroidRuntime(22002): at android.view.View.layout(View.java:14289) 09-16 16:38:20.682: E/AndroidRuntime(22002): at android.view.ViewGroup.layout(ViewGroup.java:4559) 09-16 16:38:20.682: E/AndroidRuntime(22002): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076) 09-16 16:38:20.682: E/AndroidRuntime(22002): at android.view.View.layout(View.java:14289) 09-16 16:38:20.682: E/AndroidRuntime(22002): at android.view.ViewGroup.layout(ViewGroup.java:4559) 09-16 16:38:20.682: E/AndroidRuntime(22002): at android.widget.FrameLayout.onLayout(FrameLayout.java:448) 09-16 16:38:20.682: E/AndroidRuntime(22002): at android.view.View.layout(View.java:14289) 09-16 16:38:20.682: E/AndroidRuntime(22002): at android.view.ViewGroup.layout(ViewGroup.java:4559) 09-16 16:38:20.682: E/AndroidRuntime(22002): at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:349) 09-16 16:38:20.682: E/AndroidRuntime(22002): at android.view.View.layout(View.java:14289) 09-16 16:38:20.682: E/AndroidRuntime(22002): at android.view.ViewGroup.layout(ViewGroup.java:4559) 09-16 16:38:20.682: E/AndroidRuntime(22002): at android.widget.FrameLayout.onLayout(FrameLayout.java:448) 09-16 16:38:20.682: E/AndroidRuntime(22002): at android.view.View.layout(View.java:14289) 09-16 16:38:20.682: E/AndroidRuntime(22002): at android.view.ViewGroup.layout(ViewGroup.java:4559) 09-16 16:38:20.682: E/AndroidRuntime(22002): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1976) 09-16 16:38:20.682: E/AndroidRuntime(22002): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1730) 09-16 16:38:20.682: E/AndroidRuntime(22002): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004) 09-16 16:38:20.682: E/AndroidRuntime(22002): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481) 09-16 16:38:20.682: E/AndroidRuntime(22002): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749) 09-16 16:38:20.682: E/AndroidRuntime(22002): at android.view.Choreographer.doCallbacks(Choreographer.java:562) 09-16 16:38:20.682: E/AndroidRuntime(22002): at android.view.Choreographer.doFrame(Choreographer.java:532) 09-16 16:38:20.682: E/AndroidRuntime(22002): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735) 09-16 16:38:20.682: E/AndroidRuntime(22002): at android.os.Handler.handleCallback(Handler.java:730) 09-16 16:38:20.682: E/AndroidRuntime(22002): at android.os.Handler.dispatchMessage(Handler.java:92) 09-16 16:38:20.682: E/AndroidRuntime(22002): at android.os.Looper.loop(Looper.java:137) 09-16 16:38:20.682: E/AndroidRuntime(22002): at android.app.ActivityThread.main(ActivityThread.java:5103) 09-16 16:38:20.682: E/AndroidRuntime(22002): at java.lang.reflect.Method.invokeNative(Native Method) 09-16 16:38:20.682: E/AndroidRuntime(22002): at java.lang.reflect.Method.invoke(Method.java:525) 09-16 16:38:20.682: E/AndroidRuntime(22002): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 09-16 16:38:20.682: E/AndroidRuntime(22002): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 09-16 16:38:20.682: E/AndroidRuntime(22002): at dalvik.system.NativeStart.main(Native Method)
I solved this using the Accelerometer in my project:
private void initAccelerometer() {
sensorManager1 = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
Sensor accelerometer = sensorManager1.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
sensorManager1.registerListener(this, accelerometer,SensorManager.SENSOR_DELAY_GAME);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
int currentOrientation = getResources().getConfiguration().orientation;
if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE){
buyList.setColumnCount(4);
buyList.invalidate();
}else{
buyList.setColumnCount(2);
buyList.invalidate();
}
}
And adding this in my Activity, on the Android Manifest: android:configChanges="orientation|screenSize"
Had the same problem. This happens if the nr of colums changes on screen rotation because the nr. of colums is getting restored.
There should be a check if the number of colums changed, new layouting and some sort of calculation where to scroll to.