Tag Archives: Cotton

Kotlin activity Startup and Compile Error [How to Solve]

The android project added an activity written in kotlin code and tried to launch the page.
The error is reported as follows:

C:\Users\15901\Desktop\ShangXueTang\app\src\main\java\com\example\shangxuetang\MainActivity.java:41: ����: �Ҳ�������
startActivity(new Intent(MainActivity.this, LifecycleActivity.class));
^
����: �� LifecycleActivity
�: �� MainActivity
1 ������

 

Solution:

    1. Add the following two lines of configuration to build.gradle(:app)
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
    1. add the dependency path.
dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        }

The binary version of its metadata is 1.5.1, expected version is 1.1.16

The binary version of its metadata is 1.5.1, expected version is 1.1.16

In the development process of using flutter, we encountered the version conflict of kotlin. We searched many methods on the Internet and found that they could not be solved.

Finally, we found that we need to upgrade the kotlin version, and then upgrade the gradle version.

I’d like to upgrade here to:

The kotlin version changes to:

ext.kotlin_version = '1.5.0'

Gradle version changed to:

distributionUrl=https\://services.gradle.org/distributions/gradle-6.9-all.zip

If you encounter kotlin, you can try to upgrade the version. Note that kotlin corresponds to gradle.

[error record] kotlin code compilation error when Android studio compiles (process @ nonnull parameter when upgrading support library)

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;