Proguard-IllegalStateException:创建视图时不得附加ViewHolder视图

时间:2018-10-20 00:56:55

标签: android android-recyclerview proguard

在我的gradle文件中启用minifyEnabled之后,yy应用程序继续崩溃。我进行了很多搜索,但是对Proguard感到不舒服,并且不确定问题可能出在哪里。也许我需要在proguard-rules pro中添加更多内容?

build.gradle

buildTypes {
    release {
        minifyEnabled true
        shrinkResources true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

proguard-rules.pro

##---------------Begin: proguard configuration for Gson  ----------
# Gson uses generic type information stored in a class file when working with fields. Proguard
# removes such information by default, so configure it to keep all of it.
-keepattributes Signature

# For using GSON @Expose annotation
-keepattributes *Annotation*

# Gson specific classes
-dontwarn sun.misc.**
#-keep class com.google.gson.stream.** { *; }

# Application classes that will be serialized/deserialized over Gson
-keep class com.google.gson.examples.android.model.** { *; }

# Prevent proguard from stripping interface information from TypeAdapterFactory,
# JsonSerializer, JsonDeserializer instances (so they can be used in @JsonAdapter)
-keep class * implements com.google.gson.TypeAdapterFactory
-keep class * implements com.google.gson.JsonSerializer
-keep class * implements com.google.gson.JsonDeserializer

##---------------End: proguard configuration for Gson  ----------

-keep class com.sterbsociety.** {*;}
-keep class package.model.* {*;}
-keep class androidx.recyclerview.widget.RecyclerView.** {*;}
-keepattributes *Annotation*,Signature, InnerClasses

错误:

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 androidx.recyclerview.widget.RecyclerView$a.createViewHolder(:6)
        at androidx.recyclerview.widget.RecyclerView$p.a(:60)
        at androidx.recyclerview.widget.RecyclerView$p.a(:26)
        at androidx.recyclerview.widget.RecyclerView$p.c(:2)

有什么主意吗?预先感谢。

1 个答案:

答案 0 :(得分:1)

该错误可能与proguard不相关,但与您如何扩展ViewHolder的布局有关。尝试确保使用false参数夸大布局,如下所示:

  @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    Context context = viewGroup.getContext();
    LayoutInflater inflater = LayoutInflater.from(context);

    // Inflate the custom layout, use false.
    View contactView = inflater.inflate(R.layout.item_of_yours, viewGroup, false);

    ...

    return viewHolder;
  }