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)
// Create new views (invoked by the layout manager)
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
// Create a new view, which defines the UI of the list item
View view = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.text_row_item, viewGroup, false);
return new ViewHolder(view);
}
关键点是给attachToRoot
参数设为false
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)
第一个参数resource
:是传入的布局参数
第二个参数root
:是这个布局的父布局
第三个参数attachToRoot
:就是是否要将这个布局加载到父布局里
android {
buildTypes {
release {
// Enables code shrinking, obfuscation, and optimization for only
// your project's release build type. Make sure to use a build
// variant with `debuggable false`.
minifyEnabled true
// Enables resource shrinking, which is performed by the
// Android Gradle plugin.
shrinkResources true
// Includes the default ProGuard rules files that are packaged with
// the Android Gradle plugin. To learn more, go to the section about
// R8 configuration files.
proguardFiles getDefaultProguardFile(
'proguard-android-optimize.txt'),
'proguard-rules.pro'
}
}
...
}
06-20 19:28:46.753 14887 14887 E AndroidRuntime: FATAL EXCEPTION: main
06-20 19:28:46.753 14887 14887 E AndroidRuntime: Process: com.freeme.freemenote, PID: 14887
06-20 19:28:46.753 14887 14887 E AndroidRuntime: 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)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at androidx.recyclerview.widget.RecyclerView$g.a(Unknown Source:29)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at androidx.recyclerview.widget.RecyclerView$v.j(Unknown Source:841)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at androidx.recyclerview.widget.StaggeredGridLayoutManager.B0(Unknown Source:105)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at androidx.recyclerview.widget.StaggeredGridLayoutManager.O0(Unknown Source:764)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at androidx.recyclerview.widget.StaggeredGridLayoutManager.X(Unknown Source:1)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(Unknown Source:38)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at androidx.recyclerview.widget.RecyclerView.dispatchLayout(Unknown Source:37)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at androidx.recyclerview.widget.RecyclerView.onLayout(Unknown Source:7)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at android.view.View.layout(View.java:24625)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:6513)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1891)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1729)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at android.widget.LinearLayout.onLayout(LinearLayout.java:1638)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at android.view.View.layout(View.java:24625)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:6513)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1103)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at android.view.View.layout(View.java:24625)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:6513)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1103)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at android.view.View.layout(View.java:24625)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:6513)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at t0.b.onLayout(Unknown Source:256)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at android.view.View.layout(View.java:24625)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:6513)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1103)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at android.view.View.layout(View.java:24625)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:6513)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at android.view.View.layout(View.java:24625)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:6513)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:510)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at android.view.View.layout(View.java:24625)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:6513)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at com.android.internal.policy.DecorView.onLayout(DecorView.java:810)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at android.view.View.layout(View.java:24625)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:6513)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:4515)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:3822)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2629)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:9843)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1632)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1641)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at android.view.Choreographer.doCallbacks(Choreographer.java:1184)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at android.view.Choreographer.doFrame(Choreographer.java:1073)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1615)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:958)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at android.os.Looper.loopOnce(Looper.java:240)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at android.os.Looper.loop(Looper.java:346)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:8344)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:594)
06-20 19:28:46.753 14887 14887 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)
添加混淆规则:
-keep class androidx.recyclerview.widget.**{*;}
Android RecyclerView和Gson混淆问题
https://juejin.cn/post/6903437752142921741
https://blog.csdn.net/baidu_31093133/article/details/83306794
混淆规则:
https://blog.51cto.com/u_16213607/7452100
https://developer.android.com/build/shrink-code?hl=zh-cn#groovy
https://www.guardsquare.com/manual/configuration/usage