简体   繁体   English

SwitchCompat 未解决的参考 - Android Kotlin

[英]SwitchCompat Unresolved reference - Android Kotlin

I'm using a SwitchCompat in a fragment where I keep getting and Unresolved reference that don't let me compile.我在一个片段中使用 SwitchCompat,我不断获得和未解决的引用,这些引用不允许我编译。 I don't know where to look at any more.. I believe I have my gradle files how they are supposed to be but I still suspect is something from there.. Any idea what I might be doing wrong?我不知道在哪里再看..我相信我的 gradle 文件应该是这样的,但我仍然怀疑是从那里开始的..知道我可能做错了什么吗?

Thank you in advance!!先感谢您!! Any tip would be very appreciated任何提示将不胜感激

My gradle files:我的 gradle 文件:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    ext.kotlin_version = "1.4.21"
    ext.nav_version = '2.3.0'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.1.1"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_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
}

And:和:

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'androidx.navigation.safeargs.kotlin'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.2"

    defaultConfig {
        applicationId "com.example.onemorepassword"
        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'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }

    buildFeatures {
        dataBinding true
    }

}

dependencies {

    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.3.2'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.2.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
    implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

    //ViewModel
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'
}

I have my XML:我有我的 XML:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.appcompat.widget.SwitchCompat
            android:id="@+id/lowLetters_switch"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="30dp"
            android:checked="true"
            android:fontFamily="@font/nunito_sans_bold"
            android:text="@string/switchLowLetters"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/lengthSize_seekBar" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

My Fragment:我的片段:

package com.example.onemorepassword

import android.annotation.SuppressLint
import android.content.ClipData
import android.content.ClipboardManager
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import android.widget.Button
import android.widget.SeekBar
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.example.onemorepassword.databinding.OneMorePasswordFragmentBinding

class OneMorePasswordFragment : Fragment() {

    private lateinit var binding: OneMorePasswordFragmentBinding
    private lateinit var viewModel: OneMorePasswordViewModel

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        binding = DataBindingUtil.inflate<OneMorePasswordFragmentBinding>(inflater, R.layout.one_more_password_fragment, container, false)

        viewModel = ViewModelProvider(this).get(OneMorePasswordViewModel::class.java)

        //All fun that update UI from viewModel
        savedGeneratedPass()

        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        **val myLowLettersSwitch = binding.lowLettersSwitch
        myLowLettersSwitch.setOnCheckedChangeListener { _, b ->
            Toast.makeText(requireActivity(),b.toString(),Toast.LENGTH_SHORT).show(**)
        }

......
More code...
....

    **private fun switchLowLetters(): Boolean {
        val myLowLettersSwitch= binding.lowLettersSwitch
        return myLowLettersSwitch.isChecked
    }**

....
More code...

And last my mainActivity最后我的 mainActivity

package com.example.onemorepassword

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import com.example.onemorepassword.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //setContentView(R.layout.activity_main)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main)

        supportActionBar?.hide()
    }
}

You need to add viewBinding true to buildFeatures in build.gradle (app) file in order to be able to use view binding.您需要将viewBinding true添加到buildFeatures (app) 文件中的 buildFeatures 才能使用视图绑定。

Just replace:只需更换:

buildFeatures {
    dataBinding true
}

with:和:

buildFeatures {
    dataBinding true
    viewBinding true
}

Found out what was happening.发现发生了什么。 Once you know it..!一旦你知道它..!

I have to XML files for same Fragment, Portrait and Landscape.对于相同的片段、纵向和横向,我必须 XML 文件。 Forgot to edit me Landscape XML file.... So in one I had <com.google.android.material.switchmaterial.SwitchMaterial> (or compat, does not matter)忘了编辑我景观 XML 文件....所以在一个我有 <com.google.android.material.switchmaterial.SwitchMaterial> (或兼容,没关系)

and in other XML I only had而在其他 XML 我只有

Maybe this will serve for someone to look into this before collapsing也许这将有助于某人在崩溃之前对此进行调查

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM