Kotlin中是否有任何特定的语言实现,它与协同程序的另一种语言实现有所区别?
在这里,我将启动100000个协程,这段代码背后会发生什么?
for(i in 0..100000){
async(CommonPool){
//run long running operations
}
}
Run Code Online (Sandbox Code Playgroud) 我想让void enqueue(Callback<T> callback);方法调用代码块更具表现力,这是我通常所拥有的
request.enqueue(object : Callback<MyModel> {
override fun onFailure(call: Call<MyModel>?, t: Throwable?) {
//
}
override fun onResponse(call: Call<MyModel>?, response: Response<MyModel>?) {
//
}
})
Run Code Online (Sandbox Code Playgroud)
而我想要的意思是,更改此代码阻止更清晰的方式并删除那些覆盖,对象,回调关键字并执行类似的操作:
request.enqueue({throwable, response -> })
我认为可以使用扩展和高阶函数以某种方式进行改进.有谁知道怎么做?
我正在遵循面料的官方说明
https://www.fabric.io/kits/android/crashlytics/install
这是我的gradle.build文件
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'io.fabric'
buildscript {
ext.kotlin_version = '1.1.51'
repositories {
jcenter()
mavenCentral()
maven {
url 'https://maven.google.com'
}
}
dependencies {
...
classpath 'io.fabric.tools:gradle:1.24.3'
}
}
repositories {
jcenter()
mavenCentral()
maven { url "https://jitpack.io" }
maven {
url 'https://maven.google.com'
}
maven { url 'https://maven.fabric.io/public' }
}
android {
compileSdkVersion 22
buildToolsVersion '25.0.3'
defaultConfig {
....
}
}
dependencies {
......
compile('com.crashlytics.sdk.android:crashlytics:2.7.0@aar') {
transitive = true
}
}
apply plugin: 'com.google.gms.google-services'
Run Code Online (Sandbox Code Playgroud)
但是当我尝试同步我的项目时,它说找不到io.fabric.tools:gradle:1.24.3 …
我有一个自定义linearlayout类,当我想创建这个类的实例时,我得到一个错误lateinit属性尚未初始化 我正在使用最新版本的butterknife库
这是我的kotlin课程
class MenuItemView : LinearLayout {
@BindView(R.id.menu_title_text_view_id)
lateinit var menuTitleTextView : CTextBasic
constructor(ctx: Context) : super(ctx) {
}
init {
val view = LayoutInflater.from(context).inflate(R.layout.menu_item,this)
ButterKnife.bind(this,view)
}
constructor(ctx: Context, attrs: AttributeSet) : super(ctx, attrs) {
val menuAttrs = context.theme.obtainStyledAttributes(attrs, R.styleable.MenuItemView, 0, 0)
try {
val title: String = menuAttrs.getString(R.styleable.MenuItemView_menu_title)
menuTitleTextView.text = title
}catch (e : Exception){
e.printStackTrace()
}finally {
menuAttrs.recycle()
}
}
fun setTitle( title : String){
menuTitleTextView.text = title
}
}
Run Code Online (Sandbox Code Playgroud)
这是错误日志
kotlin.UninitializedPropertyAccessException: lateinit property menuTitleTextView has …Run Code Online (Sandbox Code Playgroud) 我已经将Android Studio更新到3.3,现在我无法构建我现有的项目之一,我已经尝试了invalidate/restart,还检查gradle-wrapper.properties了gradle版本是最新更新的地方:
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip
Run Code Online (Sandbox Code Playgroud)
这是我的 build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'realm-android'
apply plugin: 'kotlin-kapt'
buildscript {
ext.kotlin_version = '1.3.11'
repositories {
google()
jcenter()
mavenCentral()
maven {
url 'https://maven.google.com'
}
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.android.tools.build:gradle:3.3.0'
classpath 'com.google.gms:google-services:4.2.0'
classpath "io.realm:realm-gradle-plugin:3.1.3"
}
}
repositories {
google()
jcenter()
mavenCentral()
maven { url "https://jitpack.io" }
maven {
url 'https://maven.google.com'
}
}
androidExtensions {
experimental = true
}
android {
compileSdkVersion 22 …Run Code Online (Sandbox Code Playgroud) 例如,是否可以创建通用类型的调用
@POST("/service/")
fun<T> startRequest(@Body loginReq: Any): Call<T>
Run Code Online (Sandbox Code Playgroud)
然后就这样称呼它
val request = api.startRequest<MyModel_1>(loginReq)
Run Code Online (Sandbox Code Playgroud)
当我这样写并运行方法时,它说:
java.lang.IllegalArgumentException: Method return type must not include a type variable or wildcard: retrofit2.Call<T>
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Kotlin检查lateinit属性状态的新功能,但是得到了这个编译时错误 Unresolved reference: isInitialized
我已经使用kotlin版本kotlin_version = '1.2.0-beta-31'
(android studio版本为3.0)配置我的build.gradle文件,并且还使用相同版本更新了kotlin插件.这是我的代码片段,我正在使用isInitializedcheck.
还包括一个反射库
compile group: 'org.jetbrains.kotlin', name: 'kotlin-reflect', version: '1.2.0-beta-31'
Run Code Online (Sandbox Code Playgroud)
.
lateinit var k: SomeObjectType
fun instance(): SomeObjectType {
if (::k.isInitialized) {
k = SomeObjectType()
}
return k
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试在我的表中插入一些值,这些值是在执行此查询时创建的
public static final String tableName = "ACCOUNT_TABLE";
statement.executeUpdate("CREATE TABLE "+ tableName +" (" +
" ID INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY ("+
" START WITH 1, INCREMENT BY 1), username VARCHAR(15), password VARCHAR(100)" + ")");
Run Code Online (Sandbox Code Playgroud)
之后,当表成功创建时,我正在调用register方法将用户插入表中
public boolean registerAccount(final User user){
if (statement != null){
final String userName = user.getUserName();
final String password = user.getPassword();
try {
return statement.execute("INSERT INTO "+tableName +" VALUES (" + userName +"," + password +")");
} catch (SQLException e) { …Run Code Online (Sandbox Code Playgroud) 我在我的 Servlet 类中遇到此异常,当我尝试将文件从客户端应用程序上传到我的服务器时,这是我的 servlet doPost 代码片段
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
String userName = "admin";
String password = "123";
if (DBManager.getInstance().checkIfCanLogin(userName, password)) {
final Part part = (Part) request.getParts().toArray()[1];
new Thread(() -> {
// bla bla bla
}).start();
} else {
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
}
}catch (Exception e){
e.printStackTrace();
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
}
Run Code Online (Sandbox Code Playgroud)
这里该行final Part part = (Part) request.getParts().toArray()[1];抛出异常,我正在尝试解决这个问题,但没有找到,而且我是 JavaEE 新手,我的背景来自 android,而且我试图理解的是一切正常,但是之后重新启动(我认为没有其他事情可以做)这个 Servlet 停止工作,问题只有这个 servlet,我正在从客户端发送文件(使用改造的andorid )
这是错误日志
[2017-05-02T18:45:45.472+0400] [glassfish 4.1] [SEVERE] [] …Run Code Online (Sandbox Code Playgroud) 我有一个 KMM 项目并想使用SqlDelight库,但是当我构建项目数据库架构时,没有生成表实体。
actual class DatabaseDriverFactory(private val context: Context) {
actual fun createDriver(): SqlDriver {
//Unresolved reference: CoreDb
return AndroidSqliteDriver(CoreDb.Schema, context, "test.db")
}
}
Run Code Online (Sandbox Code Playgroud)
我在我的共享模块中定义了 sqldelight 文件夹,还为功能生成的 kotlin 类创建了文件夹,因为它是在gradle.build.kts其中配置的,并且*.sq在sqldelight文件夹中也有一个文件
sqldelight {
database("CoreDb") {
packageName = "com.example.app.core.database"
sourceFolders = listOf("sqldelight")
dialect = "sqlite:3.24"
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行generateSqlDelightInterface任务时,我只看到那些日志
> Task :core:generateAndroidDebugCoreDbInterface NO-SOURCE
> Task :core:generateAndroidReleaseCoreDbInterface NO-SOURCE
> Task :core:generateIosMainCoreDbInterface NO-SOURCE
> Task :core:generateMetadataCommonMainCoreDbInterface NO-SOURCE
> Task :core:generateMetadataMainCoreDbInterface NO-SOURCE
> Task :core:generateSqlDelightInterface UP-TO-DATE
can't …Run Code Online (Sandbox Code Playgroud)