草庐IT

kotlin-compiler-embeddable

全部标签

android - 无法使用 Kotlin 访问 EditText 或其他 UI 组件

我正在使用AndroidStudio3.0RC2和Kotlin。当我尝试访问UI组件时,除非我先编写findViewById,否则应用程序会崩溃。我认为Kotlin应该摆脱必须编写findViewById行?UI是一个fragment,我正在尝试从相同的fragment代码进行访问。有没有办法不必写findViewById?这些行有效:varuserNameField=view?.findViewById(R.id.userNameTextField)asEditTextuserNameField.setText("helloworld")如果没有findViewById行,该行将无法

android - 如何在 Android 中模拟 Kotlin 的 kotlinx.android.synthetic View

我有一个用Kotlin编写的fragment。我使用导入布局Viewimportkotlinx.android.synthetic.main.my_fragment_layout.在我的一种方法中,我设置了TextView的文本:funsetViews(){myTextView.text="Hello"//InJavaIwouldhaveused://(getView().findViewById(R.id.myTextView)).setText("Hello");}在我的纯JVM单元测试中,我想使用Mockito测试此方法。例如,如果上面的方法是用java编写的,我可以这样做:pu

android - Kotlin Dagger2 找不到符号 ApplicationModule_ProvideApplicationFactory

我正在尝试将Dagger2与Kotlin一起使用,但今天尝试编译时遇到此错误:Error:Executionfailedfortask':app:compileDebugJavaWithJavac'.Error:(5,43)error:cannotfindsymbolclassApplicationModule_ProvideApplicationFactory(应用程序)Build.gradleapplyplugin:'com.android.application'applyplugin:'kotlin-android'applyplugin:'kotlin-android-ext

android - Kotlin object/singleton 的方法是否同步?

将@Synchronized注释添加到对象的方法以使它们成为原子方法是否有意义,或者不需要它?例如:objectSingleton{varfield1:Int=0privatesetvarfield2:Int=0privateset@SynchronizedfunupdateFields(f1:Int,f2:Int){field1=f1field2=f2}}我想确保当我从其他对象读取它们时所有字段都被更新。我应该保留@Synchronized注释吗? 最佳答案 如果你想实现@Synchronized您需要明确添加语义-如您的示例所示

java - Kotlin androidTest : Tests ran to completion. 空测试套件

我正在尝试将我的测试从java转换为kotlin。简单的单元测试翻译成功,像这样:classBindingUtilsTest{@Test@Throws(Exception::class)funtestConvertBooleanToVisibility_visible(){assertEquals(BindingUtils.convertBooleanToVisibility(true),View.VISIBLE)}}但是当我尝试运行androidTest时它失败并显示消息:“未找到测试”和TestrunningstartedTestsrantocompletion.Emptytest

android - Kotlin 启动应用程序崩溃

我的应用程序是用kotlin构建的,这是我的build.gradle文件:compile"org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"和我工作区的build.gradle文件:ext.kotlin_version='1.1.3-2'ext.kotlin_plugin_version='1.1.1'dependencies{classpath"io.realm:realm-gradle-plugin:3.4.0"classpath"org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_

android - 具有 Room 持久性库的 Kotlin 委托(delegate)

我目前正在使用Kotlin开发一个新的Android应用程序。我尝试实现Room来存储数据,但我没有让它与Kotlin委托(delegate)一起工作。我创建了一个Identifier委托(delegate),以确保id在初始化后不会更改。委托(delegate)看起来像这样:classIdentifier:ReadWriteProperty{privatevarcurrentValue=-1LoverridefungetValue(thisRef:Any?,property:KProperty):Long{if(currentValue==-1L)throwIllegalStateE

android - Kotlin 中的通用类类型用法

我正在尝试编写一个通用的基础Activity,指定它是ViewModel类型的一个通用参数:abstractclassBaseActivity:AppCompatActivity()现在我正在尝试为我的ViewModel编写一个惰性初始化属性:valviewModelbylazy{ViewModelProviders.of(this,getFactory()).get(T)}显示的错误是TypeParameterTisnotanexpression也使用::class或::class.java没有帮助。谁能解释一下这个问题?编辑:我尝试使用像这样的具体化内联函数:inlinefunAp

android - lateinit 属性适配器尚未在 Kotlin4Android 中初始化

我在activity内的fragment中创建了RecyclerView,一切正常,但是当我执行notifyDataSetChanged()通过interface从activity到adapter,我得到一个错误“lateinitpropertyadapterhasnotbeen已初始化”,但我已经初始化了适配器classBuildingListFragment:Fragment(),MainActivity.EditInterface{lateinitvaradapter:BuildingListAdapterprivatevarmListener:OnFragmentInterac

android - 列出 BLE 设备 Android - Kotlin

我正在尝试使用Kotlin(Java版本也不起作用)列出Android设备上的所有BLE设备,但我没有收到任何设备或任何回电,除了“扫描”已经开始了”我在list中拥有正确的使用权限。这是我正在尝试的当前最少代码。但即使是来自Google的示例代码也列出了所有设备。我在运行Android版本8.1.0的Pixel上运行。我让它在iOS上运行,带有基本的BLE设备列表(Swift)!privatevalbleScanner=object:ScanCallback(){overridefunonScanResult(callbackType:Int,result:ScanResult?){