如何在Kotlin中使用AndroidAnnotation @FragmentArg?

我如何使用Kotlin片段中的@FragmentArg的AndroidAnnotations ? @EFragment(R.layout.debug_empty_fragment) open class EmptyFragment : Fragment(){ @FragmentArg("debugIndex") var debugIndex:Int = 0 } 这给我下面的gradle错误: error: org.androidannotations.annotations.FragmentArg cannot be used on a private element 我试图使debugIndex open (公共) ,但它不工作。 有任何想法吗? 这甚至有可能吗? 我使用Android Annotations 4.3.1和kotlin 1.1.2-5

Android性能的Kotlin枚举类

在Java中,我们被告知要严格避免在Android上使用枚举,因为它占用了两倍的内存。 这是否也适用于Kotlin中的enum class ? 将Kotlin enum编译成Java enum ?

我如何创建一个使用Gradle构建的Kotlin项目?

我正在尝试使用IntelliJ IDEA(Ubuntu 16.04上的2016.2.5)创建一个使用Gradle构建的新Kotlin项目。 当我这样做,我马上得到一个错误消息。 这就是我想要的: 从欢迎屏幕中选择“创建新项目”。 从左侧窗格中选择“Gradle”,从右侧选择“Kotlin(Java)”。 点击下一步”。 输入“hello-world”作为ArtifactId。 点击下一步。 确保选择“从源集创建单独的模块”和“使用默认的Gradle包装器”,没有别的。 点击下一步”。 使用项目名称和位置的默认值。 点击“完成”。 然后我马上得到这个错误: Gradle 'hello-world' project refresh failed Error: Could not find org.jetbrains.kotlin:kotlin-gradle-plugin:1.1-M02-12. Searched in the following locations: https://repo1.maven.org/maven2/org/jetbrains/kotlin/kotlin-gradle-plugin/1.1-M02-12/kotlin-gradle-plugin-1.1-M02-12.pom https://repo1.maven.org/maven2/org/jetbrains/kotlin/kotlin-gradle-plugin/1.1-M02-12/kotlin-gradle-plugin-1.1-M02-12.jar Required by: :hello-world:unspecified 生成的build.gradle看起来像这样: version '1.0-SNAPSHOT' buildscript { ext.kotlin_version = '1.1-M02-12' repositories { mavenCentral() } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } apply plugin: […]

Kotlin班级代表团的特点

这就是我知道如何在Kotlin创建班级代表团: class CustomList<T>(private val data: ArrayList<T> = ArrayList<T>()) : List<T> by data 但是,我不想把数据放在构造函数中,因为我想在创建CustomList时列表为空: class CustomList<T> : List<T> by data { private val data = ArrayList<T>() } 这似乎并没有工作。 我怎样才能让data成为一个内部字段,并且将List委托给它的方法,而不必编写所有的委托方法?

Kotlin:跳过协程

我在演示Android应用程序中玩Kotlin和Coroutines。 这是我有: fun testCoroutine3() = runBlocking { var num = 0 val jobs = List(10_000) { // create a lot of coroutines and list their jobs. launch(CommonPool) { delay(1000L) println(num++) } } for(job in jobs) { job.join() //wait for all jobs to finish } println("FINAL RESULT $num") } 基本上,我创建了1万个等待1秒的协程的列表,然后打印一个数字然后递增。 然后,当所有的工作完成后,我打印最终的结果。 (这个演示取自GitHub文档 ) 现在我的大部分测试运行良好,所有的协程几乎同时运行,最终结果是10000 但在一些罕见的情况下,我得到的最终结果是9,999 例如,当我把这个数字增加到5万时,这变得更加明显: […]

在Kotlin大量转换为Byte

为什么65555转换为byte在Kotlin中产生了19的结果?

如何在Kotlin中注入var?

我尝试将Dagger2实现到我的Kotlin项目中,但是@Inject注释存在问题。 在Java中看起来像这样,这工作正常: public class FooActivity extends Activity { @Inject @Named("accessTokenObservable") public Flowable<Optional<AccessToken>> accessTokenObservable; @Override protected void onCreate(@Nullable final Bundle savedInstanceState) { super.onCreate(savedInstanceState); App.getGraph().inject(this); } } 但是,我如何写Kotlin中的@Inject ? 当我使用这个: @Inject @Named("accessTokenObservable") var accessTokenObservable: Flowable<Optional<AccessToken>>? = null 我得到这个错误消息: Error:Dagger does not support injection into private fields 如果我使用lateinit : @Inject @Named("accessTokenObservable") lateinit var accessTokenObservable: Flowable<Optional<AccessToken>> 我得到这个错误消息: Error:Flowable<Optional<AccessToken>> cannot […]

无法编译Kotlin代码

我想实现Facebook登录写在kotlin。 下面的代码工作正常: 包social.social import android.content.Intent import android.support.v7.app.AppCompatActivity import android.os.Bundle import com.facebook.* import com.facebook.login.LoginResult import com.facebook.login.LoginManager import com.facebook.login.widget.LoginButton import org.json.JSONObject import java.util.* class MainActivity : AppCompatActivity() { private var callbackManager: CallbackManager? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) callbackManager = CallbackManager.Factory.create() var loginBtn: LoginButton = findViewById(R.id.login_button) as LoginButton loginBtn.setReadPermissions(Arrays.asList("public_profile","email","user_photos")) loginBtn.registerCallback(callbackManager, object : FacebookCallback<LoginResult> { […]

Json给kotlin数据类

有没有现成的服务从json模型生成kotlin数据类的方式http://www.jsonschema2pojo.org/ site? 我的新项目已经超过了复杂的API响应,所以它会节省我的时间。

使用Kotlin自定义Android视图

我正在尝试在我的Android项目中使用Kotlin。 我需要创建自定义视图类。 每个自定义视图有两个重要的构造函数: public class MyView extends View { public MyView(Context context) { super(context); } public MyView(Context context, AttributeSet attrs) { super(context, attrs); } } MyView(Context)用于在代码中实例化视图,当从XML扩充布局时, MyView(Context, AttributeSet)由布局inflater调用。 对这个问题的回答建议我使用默认值或工厂方法的构造函数。 但是,这里是我们所拥有的: 工厂方法: fun MyView(c: Context) = MyView(c, attrs) //attrs is nowhere to get class MyView(c: Context, attrs: AttributeSet) : View(c, attrs) { … } 要么 fun […]