Android:Snackbar提供ArrayIndexOutOfBoundsException

时间:2016-09-01 18:09:15

标签: java android arraylist indexoutofboundsexception android-snackbar

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

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 个答案:

答案 0 :(得分:0)

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

有谁知道为什么会这样?