Tag: kotlin android

为什么试图访问常量值的未解决的参考?

该应用程序在Kotlin单例对象中定义常量: @file:JvmName("APIConstants") package com.myapp.api object APIConstants { const val HTTP_RESPONSE_CODE_NOT_AUTHORIZED = 401 etc…. } 然后他们被用于另一个班级: import com.myapp.api.APIConstants.HTTP_RESPONSE_CODE_NOT_AUTHORIZED etc … class API { private fun returnBadResponse(response: Response<*>, callback: ApiAuthListener<*>) { if (response.code() == HTTP_RESPONSE_CODE_NOT_AUTHORIZED) { callback.onBadAuthToken() } else { callback.onFailure(response.message(), getServerError(response)) } } 在这个类中,Android Studio(3.0 beta)提供了为常量添加导入的提示,并没有给出任何问题的指示(没有红色下划线等,方法中的常量引用以紫色斜体文本显示它已经解决了)但是当我建立这个项目时,我得到了这个: Error: Unresolved reference: HTTP_RESPONSE_CODE_NOT_AUTHORIZED 我已经尝试清除IDE缓存并重新启动它,并做一个干净的构建,这没有什么区别。 我已经尝试删除@JvmName注释,甚至将常量值放在没有包含对象的文件的根目录中,但是不允许构建。 为什么类没有引用这个常量,特别是当IDE强烈建议它可以解决这个问题的时候呢?

Kotlin Android扩展:如何获得对包含在另一个布局中的布局的视图的引用?

我有一个包含另一个布局的布局: activity_main.xml中: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView"/> <include layout="@layout/included_layout" /> </LinearLayout> included_layout.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/includedTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Included TextView"/> </LinearLayout> 我如何获得对包含的布局中的TextView的引用? 它不支持(还)吗? 主要活动: import android.app.Activity import android.os.Bundle import android.support.v7.app.AppCompatActivity import kotlinx.android.synthetic.activity_main.* class MainActivity : AppCompatActivity(){ override fun onCreate(savedInstanceState: Bundle?) { […]

Kotlin构建不能获得由SquidDatabase创建的类

在我的项目中,我几乎在任何地方都使用SquidDatabase来访问数据库。 我想把我的项目转换成Kotlin,但是当我建立的时候有很多“Unresolved reference:DepartmentInfo”错误。 那个DepartmentInfo类是由SquidDatabase创建的,所以我觉得这个问题是由Kotlin在其他库之前建立的。 我没有找到任何方法把Kotlin改成buildlist的结尾。 我该如何解决这个问题?

在库模块中使用Kotlin,而不在app模块中使用它

我试图在库模块中使用Kotlin,而不是在应用程序模块中使用它。 应用程序模块只使用Java,不使用库中的任何Kotlin类。 Gradle不会编译: Error:(2, 1) A problem occurred evaluating project ':<Library>'. > Plugin with id 'kotlin-android' not found. 我所做的包括Kotlin的更改: {库根} / build.gradle buildscript { ext.kotlin_version = '1.1.3-2' repositories { jcenter() } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" … } allprojects { repositories { jcenter() } } {库根} / {库模块} / build.gradle apply plugin: 'com.android.library' apply plugin: 'kotlin-android' […]

Koltin MyClass.class.java未解决的参考:java

在Java Android中,我们使用MyActivity.class来开始一个新的活动。 我正在玩Android Studio 3 beta1,并试图将其转换为kotlin。 它被转换为MyActivity::class.java但不能编译。 它给了我Unresolved reference: java 。 什么是正确的等值? 项目Build.gradle: buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.0.0-beta1' classpath 'com.google.gms:google-services:3.0.0' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { jcenter() maven { url "https://jitpack.io" } […]

DataBinding错误在android微调

我正在尝试设置双向绑定与Android的微调,但我得到以下错误。 引起:android.databinding.tool.util.LoggedErrorException:发现数据绑定错误。 **** /数据绑定错误****味精:无法找到属性“绑定:selectedValue”的值类型java.lang.String android.widget.Spinner **** \数据绑定错误的getter * 这是我的SpinnerBindingUtils public class SpinnerBindingUtils { @BindingAdapter(value = {"bind:selectedValue", "bind:selectedValueAttrChanged"}, requireAll = false) public static void bindSpinnerData(AppCompatSpinner pAppCompatSpinner, String newSelectedValue, final InverseBindingListener newTextAttrChanged) { pAppCompatSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { newTextAttrChanged.onChange(); } @Override public void onNothingSelected(AdapterView<?> parent) { } […]

在添加/替换片段中的任何代码改进

我已经开始研究Kotlin,不知道这门语言的所有功能。 该函数用于在FrameLayout中显示片段。 逻辑是这样的,它第一次应该总是add()片段,下次它会replace() 。 另外在某些情况下,我需要使用addToBackStack() ,并在相同的情况下禁用左侧菜单。 fun showFragment(fragment : Fragment, isReplace: Boolean = true, backStackTag: String? = null, isEnabled: Boolean = true) { /* Defining fragment transaction */ val fragmentTransaction = supportFragmentManager .beginTransaction() /* Select if to replace or add a fragment */ if(isReplace) fragmentTransaction.replace(R.id.frameLayoutContent, fragment, backStackTag) else fragmentTransaction.add(R.id.frameLayoutContent, fragment) /* Select if to add […]

导入kotlinx.android.synthetic。<xml_name>显示“包无法导入”?

我已经分别在应用程序级build.gradle和project level build.gradle中添加了kotlin插件和扩展,仍然显示“package can not be imported”消息。以下是两个build.gradle文件的代码 1-build.gradle(应用程序模块) – apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' android { compileSdkVersion 26 buildToolsVersion "26.0.0" defaultConfig { applicationId "com.example.paruld.learning_kotlin_project" minSdkVersion 15 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard android.txt'), 'proguard-rules.pro' } } } dependencies { compile […]

如何从KClass反射Kotlin类型推断?

在下面的代码中,我无法正确地安排输入/输出,以至于()和()都按预期工作。 我已经尝试在属性和函数参数中切换进/出,但总是以不正确的输入错误结束。 class KStateMachine(private val states: List<StateHandler>) { var currentState: KClass<out StateHandler> private set private val handlers:HashMap<KClass<in StateHandler>, Int> = HashMap() init { currentState = states[0]::class for (i in 0..states.size – 1) { handlers.put(states[i]::class, i) } } fun goto(toState: KClass<in StateHandler>) { var index = handlers.get(toState) if (index != null) { var oldState = currentState […]

无法访问Kotlin中的val

我正在创建一个CustomView,如下所示。 但是,当我创建一个名为paintObject的val,并尝试稍后使用它时,我得到错误“预期的成员声明”,即我不能使用该val声明。 class CustomView: View { constructor(context: Context?) : super(context) constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) val paintObject = Paint() //unable to use this paintOnbject below paintObject.isAntiAlias = true override fun […]