我有以下数据类
data class Person (val id: Int? = null, val name: String, val active: Boolean)
Run Code Online (Sandbox Code Playgroud)
我需要通过反射来调用它的构造函数.我尝试了以下代码
private fun <T> createEntity(constructor: Constructor<*>, vararg args: T) : Any {
return constructor.newInstance(args)
}
Run Code Online (Sandbox Code Playgroud)
并使用args参数的数组调用它.
val fields = entity.declaredFields
var elements = Array<Any>(getFieldsCount(fields), { i ->
val index = cursor.getColumnIndex(fields[i].name.toUpperCase())
when (fields[i].type) {
kotlin.Int::class.java -> cursor.getInt(index)
kotlin.Boolean::class.java -> if (cursor.getInt(index) == 1) true else false
else -> cursor.getString(index)
}
})
val test = createEntity(entity.constructors.first(), *elements)
Run Code Online (Sandbox Code Playgroud)
随着entity: Class<T>并cursor: Cursor从本地数据库文件科特林说: …
我有这个日期字符串:2016-04-26T09:14:10.477Z这是UTC时区.我想将它转换为用户本地时区,所以如果它是GMT +02:00,我想要一个值为的日期2016-04-26T11:14:10.477Z(注意9变为11).
到目前为止我一直在使用它:
val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.getDefault())
val now = Date()
val limit = sdf.parse(coupon.usedAt)
Run Code Online (Sandbox Code Playgroud)
下面limit = Tue Apr 26 09:14:10 GMT+02:00 2016这是不正确的,我想.
那么我怎样才能正确转换它?谢谢!
编辑:我的问题不同于这一个,因为我需要使用的对象SimpleDateFormat,而不是Date一个
我有这个简单的界面:
interface ValidationBehavior {
fun onValidated()
}
Run Code Online (Sandbox Code Playgroud)
此接口用于类的一个函数:
private enum class BehaviorEnum {
IDLE,
NAVIGATEBACK
}
private fun getBehavior(payloadBehavior: String) : ValidationBehavior {
when(BehaviorEnum.valueOf(payloadBehavior)) {
BehaviorEnum.IDLE -> return object: ValidationBehavior {
override fun onValidated() {
// do some stuff
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:有没有办法用lambda简化return语句?我尝试这样的东西,但它不起作用:
return ValidationBehavior{ () -> //do some stuff }
Run Code Online (Sandbox Code Playgroud) 我需要从我的Windows Phone 8.1应用程序发送一封包含日志文件的电子邮件.我发现了这种方式:
var mailto = new Uri("mailto:?to=recipient@example.com&subject=The subject of an email&body=Hello from a Windows 8 Metro app.");
await Windows.System.Launcher.LaunchUriAsync(mailto);
Run Code Online (Sandbox Code Playgroud)
是否有一个特殊的参数来指定附件文件或另一种完全不同的方式?
因此,出于某种原因,我需要像这样执行我的调用:
val result = manager.call.execute()
if (result.isSuccess) {
//do stuff
} else {
//handle exeption
}
Run Code Online (Sandbox Code Playgroud)
当我的代码到达括号时,else它的缓冲区内容result.body()为result.errorBody()0,但长度不是 0。
但是,这是我从改造中得到的日志:
<-- 400 Bad Request https://cubus-friends-
...
D/OkHttp: {"error":"validation_error","error_description":"No user with that phone number"}
D/OkHttp: <-- END HTTP (81-byte body)
Run Code Online (Sandbox Code Playgroud)
如何将 json 错误字符串放入else代码中的括号中?
当我直接编译我的应用程序时,它适用于每个设备.但是,当我通过Fabric进行构建并从Fabric Beta应用程序安装我的应用程序时,我在启动应用程序后立即崩溃:
java.lang.NoClassDefFoundError: com.android.tools.fd.runtime.AppInfo
at com.android.tools.fd.runtime.BootstrapApplication.attachBaseContext(BootstrapApplication.java:229)
at android.app.Application.attach(Application.java:201)
at android.app.Instrumentation.newApplication(Instrumentation.java:998)
at android.app.Instrumentation.newApplication(Instrumentation.java:982)
at android.app.LoadedApk.makeApplication(LoadedApk.java:502)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4720)
at android.app.ActivityThread.access$1500(ActivityThread.java:166)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1343)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5584)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
Run Code Online (Sandbox Code Playgroud)
这仅在4.4及更低版本的设备上发生.我也有过相关的麻烦此.你认为我得到的崩溃有关系吗?
这是我的项目build.gradle:
buildscript {
repositories {
jcenter()
maven { url 'https://maven.fabric.io/public' }
}
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0'
classpath 'io.fabric.tools:gradle:1.+'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects …Run Code Online (Sandbox Code Playgroud)