Android Studio Kotlin class.java unresolved reference

Viewed 823

Hey guys I wanted to create a button to switch between two activities. So I've created an Intent but as I type

buttonWinner.setOnClickListener {
    val intent = Intent(this, redWin::class.java)
    startActivity(intent)
}

Then the java gets red and becomes an error. How can I fix this?

This is the code of redWin:

package com.example.change

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class redWin : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_red_win)
    }
}

I somehow get no error but the java gets red and my MainActivity gets underlined in red. Also when I run the app and press the button then the app crashes

build.gradle (project):

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext.kotlin_version = '1.4.10'
    repositories {
        google()
        jcenter()
        
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.6.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

build.gradle (app):

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 30
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "com.example.change"
        minSdkVersion 21
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'androidx.core:core-ktx:1.6.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    implementation 'com.google.android.material:material:1.4.0'
}

Logcat:

2021-08-10 18:52:35.874 21902-21902/? I/.example.chang: Not late-enabling -Xcheck:jni (already on)
2021-08-10 18:52:35.917 21902-21902/? W/.example.chang: Suspending all threads took: 5.659ms
2021-08-10 18:52:35.919 21902-21902/? E/.example.chang: Unknown bits set in runtime_flags: 0x8000
2021-08-10 18:52:35.922 21902-21902/? W/.example.chang: Unexpected CPU variant for X86 using defaults: x86
2021-08-10 18:52:36.252 21902-21951/com.example.change D/libEGL: Emulator has host GPU support, qemu.gles is set to 1.
2021-08-10 18:52:36.252 21902-21951/com.example.change W/libc: Unable to set property "qemu.gles" to "1": connection failed; errno=13 (Permission denied)
2021-08-10 18:52:36.255 21902-21951/com.example.change D/libEGL: loaded /vendor/lib/egl/libEGL_emulation.so
2021-08-10 18:52:36.256 21902-21951/com.example.change D/libEGL: loaded /vendor/lib/egl/libGLESv1_CM_emulation.so
2021-08-10 18:52:36.234 21902-21902/com.example.change W/RenderThread: type=1400 audit(0.0:332): avc: denied { write } for name="property_service" dev="tmpfs" ino=7399 scontext=u:r:untrusted_app:s0:c161,c256,c512,c768 tcontext=u:object_r:property_socket:s0 tclass=sock_file permissive=0
2021-08-10 18:52:36.298 21902-21951/com.example.change D/libEGL: loaded /vendor/lib/egl/libGLESv2_emulation.so
2021-08-10 18:52:36.429 21902-21902/com.example.change W/.example.chang: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
2021-08-10 18:52:36.429 21902-21902/com.example.change W/.example.chang: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
2021-08-10 18:52:36.672 21902-21949/com.example.change D/HostConnection: HostConnection::get() New Host Connection established 0xd3a87af0, tid 21949
2021-08-10 18:52:36.676 21902-21949/com.example.change D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_native_sync_v2 ANDROID_EMU_native_sync_v3 ANDROID_EMU_native_sync_v4 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_vulkan ANDROID_EMU_deferred_vulkan_commands ANDROID_EMU_vulkan_null_optional_strings ANDROID_EMU_vulkan_create_resources_with_requirements ANDROID_EMU_YUV420_888_to_NV21 ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer GL_OES_EGL_image_external_essl3 GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_gles_max_version_3_0 
2021-08-10 18:52:36.681 21902-21949/com.example.change W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
2021-08-10 18:52:36.685 21902-21949/com.example.change D/eglCodecCommon: setVertexArrayObject: set vao to 0 (0) 0 0
2021-08-10 18:52:36.685 21902-21949/com.example.change D/EGL_emulation: eglCreateContext: 0xdf18b280: maj 3 min 0 rcv 3
2021-08-10 18:52:36.698 21902-21949/com.example.change D/EGL_emulation: eglMakeCurrent: 0xdf18b280: ver 3 0 (tinfo 0xdf1d02d0)
2021-08-10 18:52:36.717 21902-21949/com.example.change W/Gralloc3: mapper 3.x is not supported
2021-08-10 18:52:36.718 21902-21949/com.example.change D/HostConnection: createUnique: call
2021-08-10 18:52:36.718 21902-21949/com.example.change D/HostConnection: HostConnection::get() New Host Connection established 0xd3a89760, tid 21949
2021-08-10 18:52:36.719 21902-21949/com.example.change D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_native_sync_v2 ANDROID_EMU_native_sync_v3 ANDROID_EMU_native_sync_v4 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_vulkan ANDROID_EMU_deferred_vulkan_commands ANDROID_EMU_vulkan_null_optional_strings ANDROID_EMU_vulkan_create_resources_with_requirements ANDROID_EMU_YUV420_888_to_NV21 ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer GL_OES_EGL_image_external_essl3 GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_gles_max_version_3_0 
2021-08-10 18:52:36.720 21902-21949/com.example.change D/eglCodecCommon: allocate: Ask for block of size 0x1000
2021-08-10 18:52:36.720 21902-21949/com.example.change D/eglCodecCommon: allocate: ioctl allocate returned offset 0x3ffff6000 size 0x2000
2021-08-10 18:52:36.739 21902-21949/com.example.change D/EGL_emulation: eglMakeCurrent: 0xdf18b280: ver 3 0 (tinfo 0xdf1d02d0)
2021-08-10 18:52:36.750 21902-21949/com.example.change D/eglCodecCommon: setVertexArrayObject: set vao to 0 (0) 1 0
2021-08-10 18:52:38.806 21902-21902/com.example.change W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@f16c7c0
2021-08-10 18:52:38.859 21902-21949/com.example.change D/EGL_emulation: eglMakeCurrent: 0xdf18b280: ver 3 0 (tinfo 0xdf1d02d0)
2021-08-10 18:52:38.883 21902-21949/com.example.change D/EGL_emulation: eglMakeCurrent: 0xdf18b280: ver 3 0 (tinfo 0xdf1d02d0)
2021-08-10 18:52:38.914 21902-21949/com.example.change D/EGL_emulation: eglMakeCurrent: 0xdf18b280: ver 3 0 (tinfo 0xdf1d02d0)
2021-08-10 18:52:38.929 21902-21949/com.example.change D/EGL_emulation: eglMakeCurrent: 0xdf18b280: ver 3 0 (tinfo 0xdf1d02d0)
2021-08-10 18:52:39.322 21902-21949/com.example.change D/EGL_emulation: eglMakeCurrent: 0xdf18b280: ver 3 0 (tinfo 0xdf1d02d0)
2021-08-10 18:52:41.259 21902-21949/com.example.change D/EGL_emulation: eglMakeCurrent: 0xdf18b280: ver 3 0 (tinfo 0xdf1d02d0)
2021-08-10 18:52:49.040 21902-21902/com.example.change W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@2a29126
2021-08-10 18:52:49.091 21902-21949/com.example.change D/EGL_emulation: eglMakeCurrent: 0xdf18b280: ver 3 0 (tinfo 0xdf1d02d0)
2021-08-10 18:52:49.109 21902-21949/com.example.change D/EGL_emulation: eglMakeCurrent: 0xdf18b280: ver 3 0 (tinfo 0xdf1d02d0)
2021-08-10 18:52:49.132 21902-21949/com.example.change D/EGL_emulation: eglMakeCurrent: 0xdf18b280: ver 3 0 (tinfo 0xdf1d02d0)
2021-08-10 18:52:49.145 21902-21949/com.example.change D/EGL_emulation: eglMakeCurrent: 0xdf18b280: ver 3 0 (tinfo 0xdf1d02d0)
2021-08-10 18:52:49.539 21902-21949/com.example.change D/EGL_emulation: eglMakeCurrent: 0xdf18b280: ver 3 0 (tinfo 0xdf1d02d0)
2021-08-10 18:52:50.229 21902-21949/com.example.change D/EGL_emulation: eglMakeCurrent: 0xdf18b280: ver 3 0 (tinfo 0xdf1d02d0)
2021-08-10 18:52:57.804 21902-21902/com.example.change W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@2a100d3
2021-08-10 18:52:57.862 21902-21949/com.example.change D/EGL_emulation: eglMakeCurrent: 0xdf18b280: ver 3 0 (tinfo 0xdf1d02d0)
2021-08-10 18:52:57.877 21902-21949/com.example.change D/EGL_emulation: eglMakeCurrent: 0xdf18b280: ver 3 0 (tinfo 0xdf1d02d0)
2021-08-10 18:52:57.895 21902-21949/com.example.change D/EGL_emulation: eglMakeCurrent: 0xdf18b280: ver 3 0 (tinfo 0xdf1d02d0)
2021-08-10 18:52:57.911 21902-21949/com.example.change D/EGL_emulation: eglMakeCurrent: 0xdf18b280: ver 3 0 (tinfo 0xdf1d02d0)
2021-08-10 18:52:58.298 21902-21949/com.example.change D/EGL_emulation: eglMakeCurrent: 0xdf18b280: ver 3 0 (tinfo 0xdf1d02d0)
2021-08-10 18:52:59.025 21902-21949/com.example.change D/EGL_emulation: eglMakeCurrent: 0xdf18b280: ver 3 0 (tinfo 0xdf1d02d0)
2021-08-10 18:53:04.632 21902-21902/com.example.change W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@148e3ff
2021-08-10 18:53:04.682 21902-21949/com.example.change D/EGL_emulation: eglMakeCurrent: 0xdf18b280: ver 3 0 (tinfo 0xdf1d02d0)
2021-08-10 18:53:04.714 21902-21949/com.example.change D/EGL_emulation: eglMakeCurrent: 0xdf18b280: ver 3 0 (tinfo 0xdf1d02d0)
2021-08-10 18:53:04.742 21902-21949/com.example.change I/chatty: uid=10161(com.example.change) RenderThread identical 2 lines
2021-08-10 18:53:05.121 21902-21949/com.example.change D/EGL_emulation: eglMakeCurrent: 0xdf18b280: ver 3 0 (tinfo 0xdf1d02d0)
2021-08-10 18:53:06.683 21902-21949/com.example.change D/EGL_emulation: eglMakeCurrent: 0xdf18b280: ver 3 0 (tinfo 0xdf1d02d0)
1 Answers

Instead of "redWin" type the name of the activity itself (ex: MainActivity). Also please share the red text you saw when the error occurred.

Related