Tag: 安卓

在KOTLIN中使用Replace函数

我尝试使用拉丁替换cyrlic字符下面的代码工作 val kztext83:String = kztext82.replace(’ə’,’ä’) 但实际翻译不是ä,它是一个’ 由于一个’是两个字符我越来越errror val kztext83:String = kztext82.replace(’ə’,’a”) 错误是 – 字符文字太多了? 我尝试了很多方法,包括创建字符串,没有解决问题

在Android Java / Kotlin中,是否实现了一个接口直接访问嵌套类的本地variables

在Android Java / Kotlin中,是否实现了一个接口直接访问嵌套类的本地variables。 更具体地说,当访问嵌套类的局部variables时,它们通过值或引用传递给接口。

Android Kotlin Dagger2提供了gson:指定为非null的参数为null

我想提供一个单一的gson实例,所以我提供它在我的NetworkModule,当我创建改造api,gson是非null,但是当我在改造类中使用gson实例,它是空的.. @Module class NetworkModule { @Provides @Singleton fun provideGson(): Gson { return Gson() } @Provides @Singleton fun provideMyEnterpriseApi(gson: Gson): MyEnterpriseApi { //HERE GSON IS NOT NULL return RetrofitMyEnterpriseApi(BuildConfig.API_MYENTERPRISE_URL, BuildConfig.API_MYENTERPRISE_CONNECTION_TIMEOUT, BuildConfig.API_MYENTERPRISE_READ_TIMEOUT, gson) } } 我的翻新API类: class RetrofitMyEnterpriseApi(baseUrl: String, connectTimeout: Long, readTimeout: Long, private val gson: Gson) : RetrofitApi(baseUrl, connectTimeout, readTimeout, RetrofitMyEnterpriseCalls::class.java) { override fun buildRetrofit(baseUrl: String, […]

gRPC protobuf不能用kotlin编译(Android Studio 3.0)

我创建了一个gRPC项目,与https://github.com/grpc/grpc-java/tree/master/examples/android/helloworld中用于android项目的示例gRPC非常相似 我第一次得到我自己的项目与我自己的简单的.proto文件编译没有任何错误。 我只是复制和粘贴所有重要的build.gradle部分。 下面的版本改变了我的MainActivity Kotlin代码。 但现在,我已经添加了一些更多的我的.proto文件,Gradle无法建立我的项目。 .proto文件是正确的,我已经尝试添加它作为依赖compileDebugKotlin {}和compileReleaseKotlin {}但没有任何工作。 我得到以下错误消息,任务:generateDebugProto未定义。 但我很困惑,因为当我添加一个换行符到我的build.gradle并单击在Android Studio中立即同步时,Gradle同步成功完成,并在gradle日志中看到以下几行: :app:extractDebugProto :app:extractIncludeDebugProto :app:extractProto :app:generateDebugProto 我做错了什么? 我的项目build.gradle // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { ext.kotlin_version = ‘1.2.0’ repositories { google() jcenter() } dependencies { classpath ‘com.android.tools.build:gradle:3.0.1’ classpath “org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version” classpath “com.google.protobuf:protobuf-gradle-plugin:0.8.3” // NOTE: Do not place […]

Kotlin的Android扩展和variables

在Kotlin之前,Android开发人员应该在这样一个variables中保存对Activity的Views的引用: Button fooBtn = (Button) findViewById(R.id.btn_foo) 以减少锅炉代码的数量和findViewById调用的数量。 通过引入Kotlin的Android扩展,我们可以通过简单地使用以下参考来引用相同的Button: btn_foo 问题: btn_foo是否保存了Button的引用,还是每次调用findViewById ? 开发者是否btn_foo使用variables来存储btn_foo来提高应用程序的性能,或者直接在代码中使用呢? 编辑:有一个解释如何扩展工作,但它仍然有点不清楚。

用Kotlin在Android中线程安全的全局资源

我是新来的Android应用程序和活动lifecyle似乎强制对全局的依赖。 (即使有Dagger,也有执行dependency injection的有状态的全局Dagger对象。) 我想创建一个名为GlobalMode的全局可用模式对象。 该模式应该在运行时改变。 这是我迄今为止: interface Mode { fun doSomething(a: Int); } class NormalMode : Mode { override fun doSomething(a: Int) { // … } } class DebugMode : Mode { override fun doSomething(a: Int) { // … } } object GlobalMode : Mode { private var mode: Mode? = null; private fun getMode() […]

Android Studio构建失败,Kotlin

:app:mergeDebugAssets :app:processDebugJavaRes UP-TO-DATE :app:transformResourcesWithMergeJavaResForDebug FAILED FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ‘:app:transformResourcesWithMergeJavaResForDebug’. > com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK kotlin/internal/internal.kotlin_builtins File1: /Users/KD/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-compiler-embeddable/1.0.4/172b43fbc03b521fed141484b212d6725fa671a9/kotlin-compiler-embeddable-1.0.4.jar File2: /Users/KD/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-runtime/1.0.4/8e25da5e31669f5acf514bdd99b94ff5c7003b3b/kotlin-runtime-1.0.4.jar 我的build.gradle: apply plugin: ‘com.android.application’ apply plugin: ‘kotlin-android’ apply plugin: ‘kotlin-android-extensions’ android { compileSdkVersion 23 buildToolsVersion “23.0.2” defaultConfig { applicationId “com.app2par.ctime” minSdkVersion 16 […]

Kotlin:解决genericsinheritance

阅读同样的文章后,我仍然不能解决generics的问题: 我有BaseActivity: abstract class BaseActivity : MvpAppCompatActivity(), BaseView { abstract fun getPresenter():BasePresenter } BaseView界面 interface BaseView : MvpView 并确保BasePresenter open class BasePresenter : MvpPresenter() 然后我创建BaseConnectionView interface BaseConnectionView : BaseView 和BaseConnectionPresenter class BaseConnectionPresenter : BasePresenter() 所以当我创建BaseConnectionActivity abstract class BaseConnectionActivity : BaseActivity(),BaseConnectionView { override abstract fun getPresenter(): BaseConnectionPresenter } 我有错误: Return type is BaseConnectionPresenter, which is not […]

燃料,Kotlin,Gson,预计BEGIN_ARRAY,但在BEGIN_OBJECT第1行

我试图解析一个JSON字符串,如下所示: { “count”: 1, “items”: [ { “organization_id”: 6972979, “organization_name”: “Lorem ipsum dolor sit amet, consectetur adipisicing elit”, } ] } 和Kotlin课堂: class LoremModel { var count: Int? = null var items: List? = null class Lorem { var organization_id: Int? = null var organization_name: String? = null constructor(organization_id: Int?, organization_name: String?) { this.organization_id = […]

如何使用Kotlin在共享首选项中存储数据类对象?

如何使用Kotlin在SharedPreferences存储数据类实例。 我写这个代码,但它不工作,每次检索数据时,它会产生崩溃。 我必须在本地保存用户预订,并且每次都从首选项中获取 ,并将这些值放在单例中 。 崩溃是: java.lang.IllegalStateException: gson.fromJson(json, BookingResponse::class.java) must not be null. BookingResponse是我的数据类 fun saveResponse(context: Context,bookingResponse: BookingResponse) { val prefs = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE) val prefsEditor = prefs.edit() val gson = Gson() val json = gson.toJson(bookingResponse) prefsEditor.putString(Constants.BOOKING, json) prefsEditor.commit() } /** * Get bookingResponse from the shared preference * * @return Saved BookingResponse */ fun […]