Tag: 安卓

将Kotlin添加到现有Java项目会打破Android Studio gradle消息错误

我是Gradle和Android的新手,但是因为我已经把Kotlin添加到了我的项目中,所以我在Android Studio中遇到一个错误,我需要通过Gradle控制台,默认情况下不会给我任何好的堆栈跟踪或路径。 在现在出现错误的Gradle消息视图中,我只能得到 错误:执行任务’:app:kaptDebugKotlin’失败。 内部编译器错误。 查看日志了解更多详情 这是预期的function,因为它似乎在jetbrains示例项目中工作正常。 我的项目Gradle文件 buildscript { ext.kotlin_version = ‘1.1.4-2’ repositories { jcenter() } dependencies { classpath ‘com.android.tools.build:gradle:2.3.3’ classpath “org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version” // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { jcenter() } } task clean(type: Delete) { delete […]

在Kotlin按钮上点击HTTP请求

我想在按钮被点击时在我的android应用程序中发出请求。 在Python中,我可以这样做: import requests params = { ‘param1’:some_string, ‘param2’:some_int, ‘param3’:another_string } requests.post(“https://some.api.com/method/some.method”, params=params) 当我按下按钮时,我想在Kotlin中做同样的事情。 我试图用燃料和khhtp这样做,但没有成功很多 – 应用程序崩溃,只要我按下按钮,负责发送请求。 UPD:我用什么: AndroidManifest.xml中 … … 的build.gradle dependencies { … compile ‘com.github.jkcclemens:khttp:0.1.0’ … } MainActivity.kt fun request(){ var message = “message” var uid = “123456” //I wanted to use it as int, but mapOf didn’t allow me var token = […]

通过kotlin中的匿名内部对象修改外部类

我正在Kotlin写我的第一个Android应用程序。 我想要做的是发出一个HTTP请求(排空),以获取一些数据,应写入对象的属性。 直到Volley响应监听器被保留为止,这一切都工作的很好。 之后,属性返回null。 这是来电者: class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val haiku = Haiku(applicationContext) haiku.load() // populates 3 properties (author, body, title) haiku.show() // author, body, title are back to null here } } 被调用者: class Haiku(var context: Context? = null, var title: String? = null, var body: String? […]

如何创建Kotlin DSL – DSL语法Kotlin

和anko一样,你可以这样编写回调函数: alert { title = “” message = “” yesButton { toast(“Yes”) } noButton { toast(“No”) } } 我怎样才能创建一个这样的嵌套function? 我试图创建它像下面,但似乎并没有工作。 class Test { fun f1(function: () -> Unit) {} fun f2(function: () -> Unit) {} } 现在,如果我用这个扩展function, fun Context.temp(function: Test.() -> Unit) { function.onSuccess() // doesn’t work } 从Activity调用它: temp { onSuccess { toast(“Hello”) } […]

在Kotlin Realm Extention中如何解决“function不属于这个领域的模式的一部分”

我想用Kotlin使用Realm使用这个Kotlin Realm Extention库。 我已经添加mavenCentral()存储库并compile “com.github.vicpinm:krealmextensions:1.2.0″到app.gradle。 当我运行我的应用程序,我得到了这个错误: java.lang.IllegalArgumentException: Feature is not part of the schema for this Realm. Did you added realm-android plugin in your build.gradle java.lang.IllegalArgumentException: Feature is not part of the schema for this Realm. Did you added realm-android plugin in your build.gradle 我试图在app.gradle中添加apply: realm-android ,但它不工作(旁边我认为它应该从库内部调用)。 我还在我的Activity中添加了Realm.init(baseContext) ,因为查询操作需要它。

types推断失败。 请尝试指定types参数显式:Kotlin

class SetContentView( @LayoutRes private val layoutRes: Int) { private var value : T? = null operator fun getValue(thisRef: Activity, property: KProperty): T { value = value ?: DataBindingUtil.setContentView(thisRef, layoutRes) return value } } val binding: ActivityDogBinding by contentView(R.layout.activity_dog) DogActivity.kt fun contentView(@LayoutRes layoutRes: Int): SetContentView { return SetContentView(layoutRes) } 当我尝试打电话给活动 val binding: ActivityLoginBinding by contentView(layoutRes = […]

Android / Kotlin使用Java.io.File读取文件:路径有问题

我正在尝试使用Kotlin在Android上开发我的第一个应用程序。 目前我尝试读取位于我的Android项目中的文件。 我有我的“Java”文件夹下面的架构: /java/example.first.com.monapp/controller/FirstActivity.kt /java/example.first.com.monapp/controller/WelcomeActivity.kt /java/example.first.com.monapp/model/words.kt /java/example.first.com.monapp/model/wordsData wordsData是一个带有原始数据的文本文件(每行3个词,用“;”分隔) word.kt中的代码是: package example.first.com.monapp.model import java.io.File data class Word(val wordFr:String, val wordRu:String, val active:Boolean) fun readFileKotlin(): List { val fileToRead=”wordsData” val wordList = mutableListOf() var reader= File(fileToRead).readLines() var wrdLst:List for (line in reader) { val wrdProperties = line.split(“;”) wordList.add(Word(wrdProperties[0], wrdProperties[1], wrdProperties[2].toBoolean())) } wrdLst=wordList return wrdLst } 我创建我的活动期间调用readFileKotlin函数,但我不断得到“java.io.FileNotFoundException”没有这样的文件或目录错误。 我尝试了几个变体的路径,如 […]

Dagger 2 + Kotlin无法将Presenter插入到View中

我正在尝试使用Dagger 2创建简单的MVP构建应用程序。我正在试图获得与本教程中相同的结果,但使用Kotlin。 这是我的代码到目前为止。 主持人: class MainPresenter @Inject constructor(var view: IMainView): IMainPresenter{ override fun beginMessuring() { view.toastMessage(“Measuring started”) } override fun stopMessuring() { view.toastMessage(“Measuring stopped”) } } 视图: class MainActivity : AppCompatActivity(), IMainView { @Inject lateinit var presenter : MainPresenter val component: IMainComponent by lazy { DaggerIMainComponent .builder() .mainPresenterModule(MainPresenterModule(this)) .build() } override fun onCreate(savedInstanceState: Bundle?) { […]

kotlin中URLEncodedUtils.format(params,“utf-8”)的替代方法是什么?

我必须将键值对转换为Url查询参数有一个在Java中可用的函数: URLEncodedUtils.format(params, “utf-8”)它在kotlin中的替代方法是什么?

在Android Studio中使用Kotlin捕获和保存图像

我需要帮助惠普kotlin,我需要捕获和保存图像在我的媒体商店我的代码: class MainActivity : AppCompatActivity() { var ListadeProductos= ArrayList() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) camera.setOnClickListener{ val intentCamera= Intent(“android.media.action.IMAGE_CAPTURE”) startActivity(intentCamera) } } }