Tag: android

使用Kotlin for Android进行数据绑定问题

我正在尝试使用Kotlin为我的Android项目启用数据绑定。 我有Kotlin插件启用,但我不能启用数据绑定我不断收到以下错误 Error:(66, 0) Could not find method kapt() for arguments [com.android.databinding:compiler:2.0.0-beta6] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler 我有我的gradle文件中的数据绑定以下依赖关系 dependencies { … kapt 'com.android.databinding:compiler:2.0.0-beta6' } kapt { generateStubs = true }

Kotlin访问Java类

为什么在Kotlin中,你需要访问他们使用的对象的Java类 MyActivity::class.java 我被告知与Kotlin与JS的互操作性…有什么想法?

在Kotlin中创建未绑定的服务

我试图在kotlin中创建一个简单的未绑定服务,但是我不能。 当我在Java中重写onBind()方法时,我可以返回null ,但是在kotlin中它说我只允许返回IBinder而不是IBinder? 这意味着它不能为null 。 任何想法如何解决,除了重写MyService类到Java? [已解决]谢谢,伙计们! 我可以真的把IBinder IBinder? 。 有用!!

与Kotlin的Mockito:AbstractMethodError

我正在努力使谷歌的测试实验室代码示例与kotlin一起工作。 但转换后,我想嘲笑kotlin接口我得到一个AbstractMethodError ,我无法解决。 的build.gradle: testCompile "org.hamcrest:hamcrest-all:1.3" testCompile "junit:junit:4.12" testCompile "org.mockito:mockito-core:2.8.47" testCompile "org.powermock:powermock-module-junit4:1.6.2" testCompile "org.powermock:powermock-api-mockito:1.6.2" NotesPresenterTest.kt package com.example.android.testing.notes.notes import org.mockito.Matchers.any /** * Unit tests for the implementation of [NotesPresenter] */ class NotesPresenterTest { @Mock private val mNotesView: NotesContract.View? = null @Before fun setupNotesPresenter() { MockitoAnnotations.initMocks(this) // Get a reference to the class under test mNotesPresenter = […]

在kotlin广播Reciver

如何在Kotlin中使用注册并在Android中创建Broadcast Receiver。 任何建议….在Java中,您可以通过声明它为广播接收器来创建它。但是在Kotlin中没有广播接收器功能…以及如果有,那么我无法找到它或如何使用它。

“kotlin-noarg”插件在Realm中不起作用

“kotlin-allopen”插件工作,但“kotlin-noarg”插件不工作。 我能怎么做? 下面是代码。 的build.gradle buildscript { ext.kotlin_version = '1.1.3-2' repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.0.0-beta2' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version" classpath "org.jetbrains.kotlin:kotlin-noarg:$kotlin_version" classpath "io.realm:realm-gradle-plugin:3.5.0" } } apply plugin: "kotlin-allopen" apply plugin: "kotlin-noarg" allOpen { annotation("sample.AllOpen") } noArg { annotation("sample.NoArg") invokeInitializers = true } 应用程序/的build.gradle apply plugin: 'realm-android' NoArg.kt @Target(AnnotationTarget.CLASS) @Retention(AnnotationRetention.SOURCE) annotation class NoArg […]

将GsonBuilder转换成Kotlin

有谁知道如何将此代码转换为kotlin GsonBuilder builder = new GsonBuilder(); builder.setLenient(); builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { @Override public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (json.getAsJsonPrimitive().isNumber()) { return new Date(json.getAsJsonPrimitive().getAsLong() * 1000); } else { return null; } } }); return builder.create(); 然后,我试了一下 val builder = GsonBuilder() builder.setLenient() builder.registerTypeAdapter(Date::class.java,………) return builder.create() …..我不知道如何转换代码

在ViewHolder中使用kotlin-android-extensions

class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { fun bindata(text: SingleText){ itemView.title.text = text.title itemView.desc.text = text.desc } } 像这样的代码,Kotlin在android扩展中有任何缓存? 当我反编译Kotlin字节码 public final void bindata(@NotNull SingleText text) { Intrinsics.checkParameterIsNotNull(text, "text"); ((AppCompatTextView)this.itemView.findViewById(id.title)).setText((CharSequence)text.getTitle()); ((AppCompatTextView)this.itemView.findViewById(id.desc)).setText((CharSequence)text.getDesc()); } 这意味着当我在Adapter.onBindViewHolder()中调用binData时,它会每次调用findViewById 这大大增加了性能的损失,并没有达到布局重用的目的 Kotlin在ViewHolder的android扩展中有任何缓存逻辑?

Android可以在Kotlin下载

我有这样的Java parcelable: @SuppressWarnings("rawtypes") public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { public KwerendaGraficzna createFromParcel(Parcel in) { return new KwerendaGraficzna(in); } public KwerendaGraficzna[] newArray(int size) { return new KwerendaGraficzna[size]; } }; 现在试图在Kotlin中实现它: companion object { @SuppressWarnings("rawtypes") val CREATOR: Parcelable.Creator<KwerendaGraficzna!> = object : Parcelable.Creator<KwerendaGraficzna!> { override fun createFromParcel(`in`: Parcel): KwerendaGraficzna { return KwerendaGraficzna(`in`) } override fun […]

在kotlin注释错误?

这里有两个代码示例 Java的: public class Q { @Retention(RetentionPolicy.SOURCE) @IntDef({LOL.one, LOL.two}) @interface Lol{} public final class LOL{ public final static int one = 1; public final static int two = 2; } public Q(){ q(1); } void q (@Lol int q){ } } 科特林: class Q { @Retention(AnnotationRetention.SOURCE) @IntDef(LOL.one, LOL.two) internal annotation class Lol object LOL { […]