Tag: 安卓

一次性将整个android项目移植到kotlin是否安全?

我们有一个使用android sdk在原生android完全开发的移动项目。 我们希望完全将代码库迁移到kotlin。 所以想知道在移植到Kotlin的时候要记住什么是主要的东西。

a.run是否和kotlin中的.run一样?

如果一个variables在Kotlin中是空的,我们需要做一个安全调用,或者。 用于显式调用。 当我试图从可变空间variables中使用一些扩展(比如run或let)时,我注意到.run是好的,IDE没有抱怨,通常我会收到一个警告,提醒我这不是一个安全调用。 是否对kotlin中的.run {}和.run {}有所影响? 如果我使用.run {},它被认为是无效的安全吗? var a? = “…” a?.run{} a.run{}

如何使用Kotlin在Android中分享图像?

我想分享一个位于资产文件夹的图像使用’Kotlin’.How我可以实现类似的代码块在android: Intent intent = new Intent(Intent.ACTION_SEND); intent.setType(“image/jpeg”); intent.putExtra(Intent.EXTRA_STREAM, uri); startActivity(Intent.createChooser(intent, “Share Image”));

Android Studio中未解决的引用currentThread()。wait()与kotlin

我有同样的问题在这里问。 我想申请接受的答案。 但是我无法做到这一点,因为IDE说: Thread.currentThread().wait(); “等待”是一个未解决的参考。 我在Android Studio中使用kotlin,所以也许kotlin插件导致这个问题。 在首选项的插件部分中提到,我已经安装了1.0.6-release-Studio2.2-1 kotlin版本。 而在项目build.gradle: buildscript { ext.kotlin_version = ‘1.0.6’ repositories { jcenter() } dependencies { classpath ‘com.android.tools.build:gradle:2.2.3’ classpath “org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version” classpath “io.realm:realm-gradle-plugin:2.3.0” // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } 如果我将kotlin_version更改为1.0.6-release-Studio2.2-1 Ide指出Kotlin version that is used for building with […]

无法将硬编码的字符串值传递给Kotlin中的@BindingAdapter

将我的Android应用程序Java代码转换为Kotlin,并使用数据绑定将自定义字体设置为TextViews。 我曾经从XML传递字符串如下 app:customFont=”@{‘harmonia-semibold.ttf’}” 在将@BindingAdapter转换为kotlin之后,上面的代码行不能正常工作,并且抛出了expr或者lambdaexpression式,所以得到”’错误。 用getter方法取代硬编码的字符串值是完美的。 下面是我的绑定适配器,不知道为什么不采取硬编码的字符串 @JvmStatic @BindingAdapter(“app:customFont”) fun setCustomFont(textView: TextView, font: String) { textView.typeface = Typeface.createFromAsset(textView.context.assets, font) } 谢谢

项目不会与Kotlin 1.1.3一起构建

我有一个Android项目,我最近更新了1.1.3,每次我建立我得到以下错误: Error:Execution failed for task ‘:app:compileDebugJavaWithJavac’. > kotlin.jvm.internal.FunctionReference.(ILjava/lang/Object;)V 其他问题通常说,这意味着stdlib不包括在内,但我已经明确添加它。 使用1.1.2-5虽然工作,并且1.1.3已经为其他项目工作。 解 问题出在我与kapt混合的注释处理器。 对于像DbFlow和Glide这样的项目,annotationProcessor依赖可以直接与kapt一起使用。 回到原来的问题: 这是我的Travis日志,下面是我的依赖关系: dependencies { compile fileTree(dir: ‘libs’, include: [‘*.jar’]) androidTestCompile(‘com.android.support.test.espresso:espresso-core:2.3-alpha’, { exclude group: ‘com.android.support’, module: ‘support-annotations’ }) testCompile ‘junit:junit:4.12’ compile(“ca.allanwang:kau:${KAU}”) compile “org.jetbrains.kotlin:kotlin-stdlib-jre7:${KOTLIN}” testCompile “org.jetbrains.kotlin:kotlin-test-junit:${KOTLIN}” debugCompile “com.squareup.leakcanary:leakcanary-android:${LEAK_CANARY}” releaseTestCompile “com.squareup.leakcanary:leakcanary-android-no-op:${LEAK_CANARY}” releaseCompile “com.squareup.leakcanary:leakcanary-android-no-op:${LEAK_CANARY}” testCompile “com.squareup.leakcanary:leakcanary-android-no-op:${LEAK_CANARY}” compile “com.github.Raizlabs.DBFlow:dbflow:${DBFLOW}” compile “com.github.Raizlabs.DBFlow:dbflow-core:${DBFLOW}” annotationProcessor “com.github.Raizlabs.DBFlow:dbflow-processor:${DBFLOW}” kapt “com.github.Raizlabs.DBFlow:dbflow-processor:${DBFLOW}” compile “com.github.Raizlabs.DBFlow:dbflow-kotlinextensions:${DBFLOW}” […]

在android studio中使用kotlin共享意图文本

我想共享文本在我的CardView使用共享意图使用KOTLIN,但有一个问题,最后一行代码在KOTLIN代码 val shareIntent = Intent() shareIntent.action = Intent.ACTION_SEND shareIntent.putExtra(Intent.EXTRA_STREAM, “ali”) shareIntent.type = “text/plain” startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to))) 这是代码中的问题 startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to))) 请帮帮我 看图像来理解我 图片 https://ibb.co/jQwYXw https://ibb.co/id0tXw https://ibb.co/fbCU5G 适配器完整的代码 class MyAdapter(context: Context, listItem: ArrayList) : RecyclerView.Adapter() { var Context = context var movieList = listItem; var layoutInflator = LayoutInflater.from(context) override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): MyViewHolder { var inflateView […]

Kotlin和Android Studio非法状态exception

突然之间,我无法从AS 3.0启动应用程序。 我有Kotlin 1.2.10,Gradle同步是做的不错,但是当我启动时,我得到这个错误。 我已经尝试清理项目,使AS缓存无效…但没有什么好的。 e: java.lang.IllegalStateException: declared descriptor is not resolved to declaration: public open fun (): [ERROR : ] defined in root package[ErrorSimpleFunctionDescriptorImpl@231377cb] at org.jetbrains.kotlin.resolve.OverrideResolver.checkOverrideForMember(OverrideResolver.kt:260) at org.jetbrains.kotlin.resolve.OverrideResolver.checkOverridesInAClass(OverrideResolver.kt:66) at org.jetbrains.kotlin.resolve.OverrideResolver.checkOverrides(OverrideResolver.kt:59) at org.jetbrains.kotlin.resolve.OverrideResolver.check(OverrideResolver.kt:52) at org.jetbrains.kotlin.resolve.LazyTopDownAnalyzer.analyzeDeclarations(LazyTopDownAnalyzer.kt:207) at org.jetbrains.kotlin.resolve.LazyTopDownAnalyzer.analyzeDeclarations$default(LazyTopDownAnalyzer.kt:57) at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration(TopDownAnalyzerFacadeForJVM.kt:112) at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration$default(TopDownAnalyzerFacadeForJVM.kt:84) at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$analyze$1.invoke(KotlinToJVMBytecodeCompiler.kt:376) at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$analyze$1.invoke(KotlinToJVMBytecodeCompiler.kt:67) at org.jetbrains.kotlin.cli.common.messages.AnalyzerWithCompilerReport.analyzeAndReport(AnalyzerWithCompilerReport.kt:96) at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.analyze(KotlinToJVMBytecodeCompiler.kt:367) at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules(KotlinToJVMBytecodeCompiler.kt:132) at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:162) at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:63) […]

Android Studio 3.0 gradle 3.0.0-beta2,打破Kotlinunit testing覆盖?

我有一个简单的Kotlin类,如下所示 class MyClass { fun justSayHello(yes: Boolean): String { if (yes) { return “Hello” } else { return “Sorry” } } } 我有我的测试(用Java写的,也可以在Kotlin上) public class MyClassTest { private MyClass myClass = new MyClass(); @Test public void testFirst() { myClass.justSayHello(true); } @Test public void testSecond() { myClass.justSayHello(false); } } 当我使用classpath ‘com.android.tools.build:gradle:3.0.0-beta2’在Android Studio 3.0 Beta-2中使用Coverage运行测试时,不会报告其覆盖范围。 但是当我使用classpath ‘com.android.tools.build:gradle:2.3.3’运行测试时,报告了100%的覆盖率。 […]

Android活动重新崩溃的应用程序“碎片不再存在”Kotlin

里面onResume()我重新创建活动如果条件匹配, this@MainActivity.recreate() 这在onCreate()内执行下面的代码, val infoFragment = ListPagerFragment() val fragmentManager = this@MainActivity.getSupportFragmentManager() fragmentManager.beginTransaction().replace(R.id.container_body, infoFragment).addToBackStack(“infoFragment”).commit() 这个片段事务在第一个Activity创建时运行良好。 但是,重新创建时,我收到错误 – java.lang.IllegalStateException: Fragment no longer exists for key f0: index 0 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2429) at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3971) at android.app.ActivityThread.access$900(ActivityThread.java:151) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1348) at android.os.Handler.dispatchMessage(Handler.java:110) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:5333) 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:824) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640) at dalvik.system.NativeStart.main(Native Method) *这个机制在java中运行良好我猜是因为我把代码改成了kotlin而不是逻辑 […]