Tag: 安卓

Kotlin的Android扩展和变量

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

如何创建像anko – DSL语法Kotlin嵌套函数回调

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

在浓缩咖啡测试超时,测试不运行

有一个辅助类DialogIdlingResource,我试着运行我的测试。 测试发生,除了这一个。 按下按钮后,一切都停止。 AcceptanceHelper.clickOnButtonInLayout(R.id.mainSignButton, R.string.common_signin_button_text, R.id.inputLayout) 带有两个辅助方法的代码片段: @Test fun signInUserWithInvalidEmail() { goToSignIn() AcceptanceHelper.updateValidationTextView(R.string.ui_data_attribute_email, "kokojambo@mail.ru") AcceptanceHelper.updateValidationTextView(R.string.ui_data_attribute_password, VALID_PASSWORD) AcceptanceHelper.clickOnButtonInLayout(R.id.mainSignButton, R.string.common_signin_button_text, R.id.inputLayout) val idlingResource = DialogIdlingResource() registerDialogIdlingResource() unregisterDialogIdlingResource() } private fun registerDialogIdlingResource() { val instrumentation = InstrumentationRegistry.getInstrumentation() idlingResource = DialogIdlingResource() Espresso.registerIdlingResources(idlingResource) } private fun unregisterDialogIdlingResource() { Espresso.unregisterIdlingResources(idlingResource) } 可能由于regester和unregister(idlingResource)两种方法导致的错误, 但实际上它应该可以工作,但也可能是某个可能犯了错误的地方,即辅助类代码: class DialogIdlingResource(private val waitTimeSeconds: Int = 5) : […]

ImageButton OnClickListener不工作

所以我有一个片段中的以下ImageButton: <ImageButton android:id="@+id/moneyBtn" style="@android:style/Widget.Holo.Light.ImageButton" android:layout_width="250dp" android:layout_height="250dp" android:layout_centerHorizontal="true" android:layout_marginTop="40dp" android:scaleType="fitCenter" android:adjustViewBounds="true" android:src="@drawable/monkey" android:background="@null"/> 和下面的片段Activity.txt class Home : Fragment() { override public fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { val view: View? = inflater.inflate(R.layout.fragment_home, container, false) val moneyButton: ImageButton = view?.findViewById(R.id.moneyBtn) as ImageButton val result = MyAppApplication() var money = result.money moneyButton.setOnClickListener(View.OnClickListener { Toast.makeText(activity, "TESTING […]

在条件符合的情况下更改可观察 – RxJava2

使用RxJava2 RxKotlin和Room ,我需要查询数据库中的一个开放的狩猎。 这意味着我搜索一个包含一个名为closed的值为false的属性。 一旦找到猎物,就需要将查询切换到特定的猎物。 对于这些查询我有2个方法: getOpenHunt(teamId:String): Flowable<List<Hunt>> getHunt(huntId:String): Flowable<List<Hunt>> 他们都返回一个List因为否则查询卡住没有找到狩猎时。 我的想法是类似的 fun queryHunt(teamId:String):Flowable<Optional<Hunt>>{ getOpenHunt(teamId) .map<Optional<Hunt>> { Optional.create(it.firstOrNull()) } .switchToFlowableIf ( it is Optional.Some, getHunt(it.element().id) } //With switchToFlowableIf's being fun <E:Any> switchToFlowableIf(condition: (E)->Boolean, newFlowable: Flowable<E>): Flowable<E> //It should unsubscribe from getOpenHunt and subscribe to newFlowable 作为参考,这里是我的Optional类 sealed class Optional<out T> { class Some<out T>(val element: […]

图像存在,不为空,但我得到一个NullPointerException异常

简短的介绍 我目前正在使用的应用程序从JSON中获取图像,如果正确使用它,并将其保存为下次使用的加载图像。 问题 突然之间,应用程序开始崩溃加载和进一步检查, file.isFile && file.canRead()检查我已经到位返回正面,但它仍然在BitmapFactory.decodeStream崩溃。 该文件的字节数为8k,与其他设备相比,其中相同的图像实际上有43k字节。 图像json是一个更大的json消息(约500kb)的一部分,它不是在开始或结束。 在发生这种情况之前的最后一次使用中,加载或签名没有问题,表明下载可能已经停止了一半。 即使如此,图像仍然有8kb,所以我想应该只是用它的部分创建的东西,而不是抛出NullPointerException 我正在使用Android Studio 3.0 Beta 4,代码位于Kotlin。 我必须在这个设备上运行100次,甚至在发生这种情况之后,它在其他测试设备上运行得非常好,加上在启动时获得完全相同的json的iOS应用程序 我在找什么 关于如何处理这个问题的建议,或者是Android或Kotlin中已知的错误的信息。 我可以重新安装在设备上的应用程序,它会再次确定,但不会希望这一发生后,应用程序被释放,如果我可以阻止它。 LaterEdit :有了Xaviers的建议,我已经把文件复制到桌面上了,当它加载的时候它肯定是格式不正确的,它实际上显示了Mac上运行的东西的一些严重畸形的图像。 我试图上传它,但它只是显示为一个空的白色图像。 它仍然有8kb,所以它包含一些数据。 这怎么可能,可以预防? 码 加载应用程序,我检查图像是否存在,并将其加载到imageView中 try { if (DesignSupport().imageExists("logo", this)) { if (DesignSupport().loadImage("logo", this) != null) { imageView.setImageBitmap(DesignSupport().loadImage("logo", this)) } } } catch (error: Error) { Log.i(tag, error.stackTrace.toString()) } 检查图像是否存在 fun imageExists(string: String, […]

Android Studio 3.0 beta2

当我建立我的项目时,我得到了以下错误: 错误:com.android.builder.merge.DuplicateRelativeFileException:发现多个文件与操作系统无关的路径“META-INF / app_debug.kotlin_module” 这里是我的build.gradle文件 buildscript { repositories { maven { url 'https://maven.fabric.io/public' } google() } dependencies { classpath 'io.fabric.tools:gradle:1.+' } } apply plugin: 'com.android.application' apply plugin: 'io.fabric' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' android { compileSdkVersion 26 buildToolsVersion "26.0.1" defaultConfig { applicationId "com.e9innovation.infrastructure" multiDexEnabled true minSdkVersion 19 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner […]

Kotlin kotlinClass.class.getName()不能返回包名,而只能返回简单的类名

AClass.class.getName(); 如果AClass是一个java类,这个方法将返回包名和类名。 但是当我将AClass java文件转换为Kotlin文件时,它只会返回一个类名。 所以系统找不到这个类的路径 上面的代码

如何设置android应用程序的自定义字体?

我似乎无法将标准的android字体更改为我的应用程序中的另一个。 我正在Kotlin写我的应用程序,我正在使用Anko进行布局。 我试过了: typeface = Typeface.create() typeface = Typface.createFromAsset(assets, "font/font_name") setTypeface(Typeface.createFromAsset(assets, "font/font_name")) 谢谢您的帮助。

在Android Studio 3.0 Canary中,Gradle Sync失败

这是我的build.gradle(Project: MyProjectName)文件: // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { ext.kotlin_version = "1.1.1" ext.supportLibVersion = "25.3.0" repositories { jcenter() } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version" classpath 'com.android.tools.build:gradle:3.0.0-alpha5' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } […]