构建RecyclerView时创建的自动生成文件中的错误

时间:2019-05-16 08:14:51

标签: java android android-recyclerview indexoutofboundsexception auto-generate

我是Android Studio的新用户,也是Java的新用户。我正在构建一个新项目,并按照本教程的说明将RecyclerView添加到我的项目:https://www.youtube.com/watch?v=Vyqz_-sJGFk。该应用似乎可以正确构建,但现在在我的设备上打开时立即崩溃。在此之前一切正常。错误消息将我带到在此过程中创建的自动生成的文件。

我尝试将错误消息跟踪到文件,如果确实存在问题,则无法弄清楚如何修复它们。在使用搜索词“ recyclerview”,“ android studio”“无法解析符号”等搜索解决方案时,我发现了一些升级到Android Studio 3.3的建议。我已经在3.4.0上,但是我尝试升级到3.4.1。不用找了。我尝试更改build.gradle文件中的依赖项,该文件似乎也没有更改。我发现的大多数其他答案都与cannot resolve symbol 'RecyclerView'有关,这不是我目前遇到的问题。我也尝试过 File> Invalidate Caches / Restart ...> Invalidate Caches and Restart ,多次,正如我在很多地方所看到的那样。

在跟踪文件时,我发现在文件的一部分中import androidx.recyclerview.R;导致cannot resolve symbol 'R'错误的几个位置,以及lst.elementData可以正常工作的多个位置,但在文件的另一部分却没有,产生cannot resolve symbol 'elementData'。 我当前的build.gradle依赖关系如下所示:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.android.material:material:1.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
    implementation 'androidx.recyclerview:recyclerview-selection:1.0.0'
}

我可以确定调用的最后一个函数是:

@Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        Log.d(TAG, "onBindViewHolder: called");

        holder.itemName.setText(mItemName.get(position));
        holder.itemDue.setText(mItemDue.get(position));

        holder.listItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: clicked on: " + mItemName.get(position));

                // Insert navigation here
            }
        });
    }
从下面的错误日志中可以看到


我上次工作时添加的唯一代码是我遵循的教程,其中对一些变量的名称进行了细微调整,然后用第二个TextView替换了每个列表项上的图像。

日志中的错误消息是:

2019-05-16 00:46:04.247 12824-12824/? D/RecyclerViewAdapter: onBindViewHolder: called
2019-05-16 00:46:04.248 12824-12824/? D/AndroidRuntime: Shutting down VM
2019-05-15 23:44:28.830 4480-4480/com.example.divideanddestress E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.divideanddestress, PID: 4480
    java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
        at java.util.ArrayList.get(ArrayList.java:411)
        at com.example.divideanddestress.RecyclerViewAdapter.onBindViewHolder(RecyclerViewAdapter.java:42)
        at com.example.divideanddestress.RecyclerViewAdapter.onBindViewHolder(RecyclerViewAdapter.java:16)
        at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781)
        at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6823)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5752)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6019)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
        at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
        at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
        at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
        at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3641)
        at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4194)
        at android.view.View.layout(View.java:19039)
        at android.view.ViewGroup.layout(ViewGroup.java:5935)
        at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1915)
        at android.view.View.layout(View.java:19039)
        at android.view.ViewGroup.layout(ViewGroup.java:5935)
        at com.google.android.material.appbar.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:142)
        at com.google.android.material.appbar.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:41)
        at com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1556)
        at androidx.coordinatorlayout.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:888)
        at android.view.View.layout(View.java:19039)
        at android.view.ViewGroup.layout(ViewGroup.java:5935)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:19039)
        at android.view.ViewGroup.layout(ViewGroup.java:5935)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
        at android.view.View.layout(View.java:19039)
        at android.view.ViewGroup.layout(ViewGroup.java:5935)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:19039)
        at android.view.ViewGroup.layout(ViewGroup.java:5935)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
        at android.view.View.layout(View.java:19039)
        at android.view.ViewGroup.layout(ViewGroup.java:5935)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at com.android.internal.policy.DecorView.onLayout(DecorView.java:954)
        at android.view.View.layout(View.java:19039)
        at android.view.ViewGroup.layout(ViewGroup.java:5935)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2711)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2426)
2019-05-15 23:44:28.831 4480-4480/com.example.divideanddestress E/AndroidRuntime:     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1569)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7296)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:930)
        at android.view.Choreographer.doCallbacks(Choreographer.java:705)
        at android.view.Choreographer.doFrame(Choreographer.java:640)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:916)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6816)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1563)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1451)

2 个答案:

答案 0 :(得分:0)

在使用arrarylist并声明itemCount值之前声明mItemName = new ArrayList()。

答案 1 :(得分:0)

感谢回答/评论的人。在查看了您关注的内容之后,我发现没有填充mItemDue ArrayList。解决此问题后,该应用程序似乎可以正常运行。