java.lang.IllegalStateException

今天解決了一個(gè)困擾我一天的BUG。

我要用RecyclerView+PagerSnapHelper做一個(gè)類似無限循環(huán)切換的ViewPager。我采用的最簡(jiǎn)單的方法將adapter里面的getItemCount()調(diào)大;用recycleViewRun.scrollToPosition(333332);移到中間位置。

頁面為2張時(shí),慢速切換沒問題,快速切換時(shí)會(huì)出現(xiàn)下面的問題。

java.lang.IllegalStateException: ViewHolder views must not be attached when created. Ensure that you are not passing 'true' to the attachToRoot parameter of LayoutInflater.inflate(..., boolean attachToRoot)

? ? ? ? at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6796)

? ? ? ? at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5975)

? ? ? ? at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)

? ? ? ? at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)

? ? ? ? at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)

? ? ? ? at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)

? ? ? ? at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)

? ? ? ? at android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1331)

? ? ? ? at android.support.v7.widget.LinearLayoutManager.scrollHorizontallyBy(LinearLayoutManager.java:1063)

? ? ? ? at android.support.v7.widget.RecyclerView.scrollStep(RecyclerView.java:1829)

? ? ? ? at android.support.v7.widget.RecyclerView$SmoothScroller.onAnimation(RecyclerView.java:11651)

? ? ? ? at android.support.v7.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:5149)

? ? ? ? at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)

? ? ? ? at android.view.Choreographer.doCallbacks(Choreographer.java:574)

? ? ? ? at android.view.Choreographer.doFrame(Choreographer.java:543)

? ? ? ? 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:5017)

? ? ? ? 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:779)

? ? ? ? at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)

? ? ? ? at dalvik.system.NativeStart.main(Native Method)

搜了很多答案,一種是inflater.inflate參數(shù)改為null或者false。我用的參數(shù)就是false。問題沒有解決。

直到我搜到了一個(gè)博主的回答,才解決了問題。非常感謝jiluyixia

java.lang.IllegalStateException: ViewHolder views must not be attached when created. Ensure that ... - 簡(jiǎn)書 (jianshu.com)


我的原因應(yīng)該是:快速切換時(shí),新建的布局還在父布局里面,沒有移除。我在onCreateViewHolder里面添加了如下語句,解決了。

ViewGroup elViewGroup = (ViewGroup)view.getParent();

if (elViewGroup !=null) {

elViewGroup.removeView(view);

}

return new RecyclerHolder(view);

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容