1

當我們更改android手機方向時,我們正在獲取以下錯誤。目前正在測試這在android v 5和4.4.4。我們的應用被鎖定爲肖像。所以,如果我們將手機扭轉到橫向模式,視圖將不會改變,但仍保持縱向模式。Android手機方向更改中的隨機ArrayIndexOutOfBoundsException

當我們將手機方向更改爲風景時,會拋出此異常並且應用程序崩潰。這將無法始終複製。但是一旦出現10次或15次,應用程序崩潰時出現以下錯誤。即使它是隨機的,也可以不斷複製。如果我們在任何特定的時間轉動手機,但可能不確定。當我在登錄或註銷操作期間扭動手機時,經常會遇到這種情況,這需要幾秒鐘的時間才能完成這些操作。

這個異常不是從特定的活動或代碼段拋出的。正如我在帖子中提到的,當我在任意視圖中改變手機方向一次10或15次隨機時拋出。但不能不斷複製。

以下是完整的堆棧跟蹤。

java.lang.ArrayIndexOutOfBoundsException: length=3; index=-2147483648 
at android.widget.GridLayout$Axis.groupArcsByFirstVertex(GridLayout.java:1413) 
at android.widget.GridLayout$Axis$1.<init>(GridLayout.java:1432) 
at android.widget.GridLayout$Axis.topologicalSort(GridLayout.java:1429) 
at android.widget.GridLayout$Axis.topologicalSort(GridLayout.java:1468) 
at android.widget.GridLayout$Axis.createArcs(GridLayout.java:1502) 
at android.widget.GridLayout$Axis.getArcs(GridLayout.java:1516) 
at android.widget.GridLayout$Axis.computeLocations(GridLayout.java:1687) 
at android.widget.GridLayout$Axis.getLocations(GridLayout.java:1708) 
at android.widget.GridLayout$Axis.getMeasure(GridLayout.java:1729) 
at android.widget.GridLayout$Axis.getMeasure(GridLayout.java:1740) 
at android.widget.GridLayout.onMeasure(GridLayout.java:1056) 
at android.view.View.measure(View.java:16557) 
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719) 
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455) 
at android.view.View.measure(View.java:16557) 
at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1226) 
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
at android.widget.ScrollView.onMeasure(ScrollView.java:326) 
at android.view.View.measure(View.java:16557) 
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719) 
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455) 
at android.view.View.measure(View.java:16557) 
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137) 
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
at android.view.View.measure(View.java:16557) 
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137) 
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404) 
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695) 
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588) 
at android.view.View.measure(View.java:16557) 
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:868) 
at android.view.View.measure(View.java:16557) 
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137) 
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
at android.view.View.measure(View.java:16557) 
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137) 
at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327) 
at android.view.View.measure(View.java:16557) 
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137) 
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291) 
at android.view.View.measure(View.java:16557) 
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1942) 
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1132) 
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1321) 
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1019) 
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5725) 
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761) 
at android.view.Choreographer.doCallbacks(Choreographer.java:574) 
at android.view.Choreographer.doFrame(Choreographer.java:544) 
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747) 
at android.os.Handler.handleCallback(Handler.java:733) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:136) 
at android.app.ActivityThread.main(ActivityThread.java:5097) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
at dalvik.sys 
+0

顯示你得到這個異常的代碼。 – Ryan

+0

請分享您的代碼。它可能是因爲你正在減少或增加onCreate中的數組指針,並且它超出了數組大小的範圍。 – Kay

+0

這個異常不是從特定的活動或代碼中拋出的。正如我在帖子中提到的,當我在任意視圖中改變手機方向一次10或15次隨機時拋出。但不能不斷複製。 –

回答

-1

您可以在活動中覆蓋onConfigurationChanged,並在每次更改方向時更新數組。

@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 

    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { 
     // Update your array here 
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { 
     // Update your array here 
    } 
} 

您還需要機器人:configChanges你的活動:

<activity 
    android:name=".MyActivity" 
    android:configChanges="orientation|keyboardHidden|screenSize" 
    android:label="@string/app_name"> 

當方向發生變化,該代碼將停止重新啓動活動。然而,這可能不起作用,因爲我不知道你的代碼是什麼。

+1

這個異常不是從特定的活動或代碼中拋出的。正如我在帖子中提到的,當我在任意視圖中改變手機方向一次10或15次隨機時拋出。但不能不斷複製。 –

+0

因此,我認爲整個應用程序都會發生這種情況? –

+0

是的。這發生在整個應用程序中。但隨機並不總是。 –