Android:Snackbar 给出 ArrayIndexOutOfBoundsException

Posted

技术标签:

【中文标题】Android:Snackbar 给出 ArrayIndexOutOfBoundsException【英文标题】:Android: Snackbar gives ArrayIndexOutOfBoundsException 【发布时间】:2017-01-09 17:38:59 【问题描述】:

似乎给出异常的代码如下:

Snackbar.make(findViewById(R.id.content_main), "E' necessario abilitare il GPS per aggiornare la lista", Snackbar.LENGTH_INDEFINITE)
        .setAction("ATTIVA", new View.OnClickListener() 

             @Override
             public void onClick(View v) 

                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(intent);
             
).show();

我这么说是因为我尝试调试应用程序,但它只在删除这部分代码时起作用。

这是错误日志:

E/androidRuntime: FATAL EXCEPTION: main
Process: d.g.movida, PID: 22151
java.lang.IndexOutOfBoundsException: Invalid index 3, size is 3
    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
    at java.util.ArrayList.get(ArrayList.java:308)
    at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:848)
    at android.view.View.layout(View.java:17938)
    at android.view.ViewGroup.layout(ViewGroup.java:5812)
    at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1197)
    at android.view.View.layout(View.java:17938)
    at android.view.ViewGroup.layout(ViewGroup.java:5812)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:344)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:281)
    at android.view.View.layout(View.java:17938)
    at android.view.ViewGroup.layout(ViewGroup.java:5812)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1742)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
    at android.view.View.layout(View.java:17938)
    at android.view.ViewGroup.layout(ViewGroup.java:5812)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:344)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:281)
    at android.view.View.layout(View.java:17938)
    at android.view.ViewGroup.layout(ViewGroup.java:5812)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1742)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
    at android.view.View.layout(View.java:17938)
    at android.view.ViewGroup.layout(ViewGroup.java:5812)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:344)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:281)
    at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:3193)
    at android.view.View.layout(View.java:17938)
    at android.view.ViewGroup.layout(ViewGroup.java:5812)
    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2666)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2367)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1437)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7403)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:920)
    at android.view.Choreographer.doCallbacks(Choreographer.java:695)
    at android.view.Choreographer.doFrame(Choreographer.java:631)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:906)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:158)
    at android.app.ActivityThread.main(ActivityThread.java:7229)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

我使用 ArrayList 的唯一时刻是初始化它的时候:

ArrayList<CustomLVArray> retrievedData;
ListAdapter listAdapter;
ListView listView;

....

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);

    ....

    retrievedData = new ArrayList<>();
    listView = (ListView) findViewById(R.id.content_main);

    listAdapter = new CustomListViewAdapter(this, retrievedData);
    listView.setAdapter(listAdapter);

    listView.setOnScrollListener....

当我向它添加东西时:

CustomLVArray message = new CustomLVArray("Title", "subtitle", "Lorem ipsum dolor sit amet ipsum dolor sit amet ipsum dolor sit amet ipsum dolor sit amet", getResources().getDrawable(R.drawable.ic_menu_camera));

        retrievedData.clear();

        for (int i = 0; i <= 10; i++) 

            retrievedData.add(message);
        

【问题讨论】:

【参考方案1】:

似乎实施第一个答案中解释的“技巧”:ArrayIndexOutOfBoundsException while dismissing Snackbar/ViewDragHelper 应用程序不会崩溃。

有人知道这是为什么吗?

【讨论】:

有趣...如果您觉得可以在 AOSP 中发布问题 issue tracker 答案的第一部分可能会回答问题,但新问题不属于答案。询问解决方案为什么有效应该作为一个单独的问题提出。 您是否费心阅读整个答案,它链接到另一个关于该问题的 SO 问题,该问题链接到 google 上的错误报告。

以上是关于Android:Snackbar 给出 ArrayIndexOutOfBoundsException的主要内容,如果未能解决你的问题,请参考以下文章

Android开发学习之路-SnackBar使用心得

Android Snackbar简单解析

Android - MD之Snackbar的使用

Android Snackbar 的使用

Android 多行 Snackbar

Android Material Design 系列之 SnackBar详解