Contents of articles
1、 Error information 2. Error analysis 3. Solutions
1、 Error information
Recently, we have upgraded all the support libraries to
28.0.0
28.0.0
28.0.0 ;
implementation 'com.android.support:recyclerview-v7:28.0.0'
There are a lot of errors in the support library code of kotlin language;
2、 Error analysis
Here, we inherit the recyclerview.itemdecision class and override the getitemoffsets method,
object : RecyclerView.ItemDecoration() {
override fun getItemOffsets(outRect: Rect?, view: View?, parent: RecyclerView?, state: RecyclerView.State?) {
super.getItemOffsets(outRect, view, parent, state)
}
Note that in the overridden method, the parameter type
outRect: Rect?view: View?parent: RecyclerView?state: RecyclerView.State?
All are nullable types;
Look at the real code of itemdeclaration. The four parameters of getitemoffsets method are all non empty types, and the @ nonnull annotation is added. Therefore, non empty parameters must be passed in here. When inheriting, they are nullable parameters. An error will be reported, and the parameter types are inconsistent;
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
this.getItemOffsets(outRect, ((RecyclerView.LayoutParams)view.getLayoutParams()).getViewLayoutPosition(), parent);
}
Item decision complete code reference:
public abstract static class ItemDecoration {
public ItemDecoration() {
}
public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
this.onDraw(c, parent);
}
/** @deprecated */
@Deprecated
public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent) {
}
public void onDrawOver(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
this.onDrawOver(c, parent);
}
/** @deprecated */
@Deprecated
public void onDrawOver(@NonNull Canvas c, @NonNull RecyclerView parent) {
}
/** @deprecated */
@Deprecated
public void getItemOffsets(@NonNull Rect outRect, int itemPosition, @NonNull RecyclerView parent) {
outRect.set(0, 0, 0, 0);
}
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
this.getItemOffsets(outRect, ((RecyclerView.LayoutParams)view.getLayoutParams()).getViewLayoutPosition(), parent);
}
}
3、 Solutions
Google did not add the @ nonnull annotation to the parameters of the support library method before. In the
28.0.0
28.0.0
In version 28.0.0, the annotation is added, which leads to the incompatibility of inheritance;
The modification method is to set all parameter types to non empty type and delete the question mark in each parameter;
Read More:
- [error record] as compilation error (Android support plugin version is too high | upgrade Android studio to the latest version)
- android studio:Error inflating class android.support.v4.widget.SwipeRefreshLayout
- Android Studio sync build.gradle appears: Failed to resolve: com.android.support:appcompat problem
- Android studio compilation error: style attribute ‘@ android:attr/windowEnterAnimation ‘not found the ultimate solution
- About Android studio error: (26,13) failed to resolve: com.android.support : appcompat-v7:27. + error
- [problem record] installed build tools revision 31.0.0 is broken during Android studio compilation
- Solve the problem of available for offline mode in Android studio compilation
- [error log] 27.1.0 series of errors Error:Failed to resolve: com.android.support :support-an notations:27.1.0
- Visual studio 2013 compiles vtk7.1.0
- Android Studio Series: after Android studio is opened, the project window will not be displayed.
- Android studio error compilation failed see the compiler error output for details.
- Android studio introduces code error, but it can run normally
- Error: package android.support.annotation Does not exist
- Gradle tasks disappeared after upgrading Android stuido 4.2?
- Error lnk2038 occurred during PCL code compilation: detected “error” during PCL code compilation_ ITERATOR_ DEBUG_ Mismatch of level: value ‘0’ does not match value ‘2’ solution
- Solve the problem of installing APK after Android studio compilation: error while installing apk
- Android project error: could not resolve com.android.support .c onstraint:constraint-layout :2.0.2.
- Android Studio | Failed to find target with hash string ‘android-26’ in: D:\Android\sdk
- Android Studio – add configuration for Android studio
- Error:Kotlin: Module was compiled with an incompatible version of Kotlin. The binary version of its