Tag: 安卓

Kotlin在variables声明之后分配委托

所以,基本上我有一个class级: class App : Application() { lateinit var prefs: SharedPreferences } 现在,我想添加一个委托属性: var isInitialized: Boolean by prefs.boolean() 问题是这个, isInitialized属性必须被懒惰地初始化,因为我使用的是Android Dagger2框架,它在App创建之后执行注入(在调用onCreate()方法期间): class App : Application() { lateinit var prefs: SharedPreferences var isInitialized: Boolean = false override fun onCreate() { super.onCreate() // how can I assign a delegate to isInitialized? } } 我希望通过以下方式来完成: 在声明中的懒惰初始化(这是委托在委托 – 怀疑这是否可能?) 在赋值期间延迟初始化 […]

如何访问kotlin中的数据列表数据

我有: var itemOrder = ArrayList() 文件模型Order.kt: private var itemName:String? = null private var note:String? = null private var price: CharSequence? = null private var all: Int? = null private var quantity: Int? = null fun Order(itemName: String, price: CharSequence, all: Int, note: String, quantity: Int) { this.itemName = itemName this.price = price this.all = all […]

如何更改alertdialog上的正面按钮的颜色?

我想改变正面按钮的颜色。 我怎样才能做到这一点? AlerDialog: fun onSomeClicked(view: View) { val dialog: AlertDialog.Builder = AlertDialog.Builder(view.context) dialog.setTitle(“SomeText”) .setMessage(“SomeMessage”) .setPositiveButton(view.context.getString(R.string.ok_dialog)) { p0, p1 -> onCheckPassword(view) } .setNegativeButton(context.getString(R.string.cancel_dialog)) { p0, p1 -> } .create() .show() } 谢谢! (我的问题是在科特林,在科特林做。)

当使用Kotlin :: class.java.canonicalName时OutOfMemoryexception

运行这行代码时,出现Out Of Memoryexception private val TAG = KotlinFilePicker()::class.java.canonicalName 并禁用时,它工作正常 Throwing OutOfMemoryError “Failed to allocate a 170 byte allocation with 68 free bytes and 68B until OOM” (recursive case) 01-04 00:14:55.044 20584-20584/com.androidbuffer.kotlinfilepickersample E/art: “main” prio=5 tid=1 Runnable 01-04 00:14:55.044 20584-20584/com.androidbuffer.kotlinfilepickersample E/art: | group=”main” sCount=0 dsCount=0 obj=0x7581c250 self=0xb4f07800 01-04 00:14:55.044 20584-20584/com.androidbuffer.kotlinfilepickersample E/art: | sysTid=20584 nice=-11 cgrp=apps sched=0/0 […]

Kotlin:val,或者可以设置一次的var

只是好奇:在Kotlin,我很想得到一些可以被懒惰初始化的val,但是有一个参数。 那是因为我需要一些很晚才创建的东西来初始化它。 具体来说,我希望我有: private lateinit val controlObj:SomeView 要么: private val controlObj:SomeView by lazy { view:View->view.findViewById(…)} 接着: override fun onCreateView(….) { val view = inflate(….) controlObj = view.findViewById(…) 或者在controlObj.initWith(view)情况下controlObj.initWith(view)或类似的东西: return view 我不能by lazy使用by lazy因为by lazy不会接受初始化时要使用的外部参数。 在这个例子中 – 包含的view 。 当然,我有lateinit var但如果我能确保它只是在设置后才能读取,我可以在一行中完成。 是否有一个非常干净的方法来创建一个只读的variables,只初始化一次,但只有当其他一些variables出生? 任何init once关键字? 在init之后,编译器知道它是不可变的? 我意识到潜在的并发问题,但如果我敢于在init之前访问它,我当然应该被抛出 。

Java活动不会在布局中显示任何视图

我用Android studio 3.0创建了新的Kotlin项目。之后,我创建了一个Java活动,并将其更改为Manifest中的主要活动。启动活动后,我没有看到任何视图。但是,当我尝试加载kotlin活动时,视图是可见的。任何建议可以欣赏 Kotlin活动 class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) } } Java活动 public class HomeActivity extends AppCompatActivity { @Override public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) { super.onCreate(savedInstanceState, persistentState); setContentView(R.layout.activity_main); } } 清单文件

在kotlin中写入可分配的值

后端返回可空Int? 我应该如何编写可以为空的值? date class Foo (var value: Int?){ constructor(source: Parcel) : this( source.readInt() ) override fun writeToParcel(dest: Parcel, flags: Int) { dest.writeInt(value) // doesn’t compile – writeInt expect no null value } } 现在我解决了: dest.writeInt(value?: -1) 然后检查到-1 或者像Int字符串写入Int然后使用…的值… 但我认为这是丑陋的错误。 解决! 我的答案: source.readValue(Int::class.java.classLoader) as Int?, dest.writeValue(value)

android studio 3.0.1和kotlin v1.2.10-release-Studio3.0-1有问题

我正在使用Android工作室3.0.1,我打算使用Kotlin。 我写了一些简单的代码,当应用程序运行android工作室显示我下面的错误: 错误:摇篮: 配置根项目“Firstapp1”时出现问题。 无法解析配置’:classpath’的所有文件。 无法解析com.android.tools.build:gradle:3.0.1。 要求: 项目: 无法解析com.android.tools.build:gradle:3.0.1。 无法解析POM https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/3.0.1/gradle-3.0.1.pom 已经看过doctype。 Gradle版本:4.1 Android插件版本:3.0.1 请帮我解决这个问题!

DialogFragment中的Kotlin合成和自定义布局

比方说,我有这样的布局: 我在DialogFragment中使用它: class DepartmentChoiceDialog : DialogFragment() { override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { val builder = AlertDialog.Builder(activity) builder.setTitle(R.string.choose_or_create_dep) .setView(R.layout.department_chooser_dialog) .setNegativeButton(android.R.string.cancel, { d, i -> d.cancel() }) return builder.create() } } 如果我参考使用合成的小部件: override fun onActivityCreated(savedInstanceState: Bundle?) { super.onActivityCreated(savedInstanceState) dep_list.layoutManager = LinearLayoutManager(activity) dep_list.itemAnimator = DefaultItemAnimator() dep_list.setHasFixedSize(true) } 我在运行时遇到这个错误: java.lang.NullPointerException:尝试在MyDialog的空对象引用上调用虚拟方法’android.view.View android.view.View.findViewById(int)’._ $ _ findCachedViewById(DepartmentChoiceDialog.kt:0) 我不明白如何在DialogFragment的情况下使用合成。 它在Fragment和Activity中工作正常。

Kotlin属性与getter。 我可以不指定初始值吗?

我想创建一个单例类,但不幸的是,Android几乎所有东西都需要Context,所以我需要它创建一个实例。 所以我只是假设用户名为init() ,然后返回实例。 正如你在下面看到的那样,如果_instance为null,则会抛出exception,所以get方法不能返回null。 但Kotlin说我必须初始化instance 。 事情是,MyClass不能没有上下文创建。 所以我不想指定一个初始值。 我怎样才能做到这一点? companion object { protected var _instance:MyClass? = null; fun init(context:Context) { _instance = MyClass(context) } var instance:MyClass //<—This causes a compile error. get() { if(_instance==null) throw RuntimeException("Call init() first."); return _instance!!; } }