我刚尝试在虚拟Android设备上使用新的Android Oreo系统映像启动我的应用程序
然而,应用程序立即崩溃在这一行:
params1.removeRule(params1.getRule(RelativeLayout.LEFT_OF));
完整的代码段如下所示:
// Center the c's by removing the layout relation with the 'inna'
RelativeLayout.LayoutParams params1 = ((RelativeLayout.LayoutParams)c1.getLayoutParams());
RelativeLayout.LayoutParams params2 = ((RelativeLayout.LayoutParams)c2.getLayoutParams());
if (Build.VERSION.SDK_INT >= 23) {
params1.removeRule(params1.getRule(RelativeLayout.LEFT_OF));
params2.removeRule(params2.getRule(RelativeLayout.RIGHT_OF));
} else {
params1.addRule(RelativeLayout.LEFT_OF, 0);
params1.addRule(RelativeLayout.ALIGN_START, 0);
params1.addRule(RelativeLayout.START_OF, 0);
params2.addRule(RelativeLayout.RIGHT_OF, 0);
params2.addRule(RelativeLayout.ALIGN_END, 0);
params2.addRule(RelativeLayout.END_OF, 0);
}
堆栈跟踪:
java.lang.ArrayIndexOutOfBoundsException: length=22; index=2131296620
at android.widget.RelativeLayout$LayoutParams.addRule(RelativeLayout.java:1453)
at android.widget.RelativeLayout$LayoutParams.removeRule(RelativeLayout.java:1475)
at my.super.app.screens.startUp.LoginScreen.onWindowFocusChanged(LoginScreen.java:160)
at android.support.v7.view.WindowCallbackWrapper.onWindowFocusChanged(WindowCallbackWrapper.java:123)
at com.android.internal.policy.DecorView.onWindowFocusChanged(DecorView.java:1539)
at android.view.View.dispatchWindowFocusChanged(View.java:11974)
at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:1387)
at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3854)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
所以,直到SDK Version 26,这段代码才能正常工作 有什么想法吗?