草庐IT

android - 在 kotlin 中连接两个字节数组的简单方法?

在Kotlin中连接两个字节数组的最简单方法是什么?考虑一下,valx=ByteArray(a);valy=ByteArray(b);帮我把两个字节数组x,y拼接起来,存入另一个ByteArray? 最佳答案 有一个运算符函数plus对于ByteArray(以及所有其他数组)operatorfunByteArray.plus(elements:ByteArray):ByteArrayReturnsanarraycontainingallelementsoftheoriginalarrayandthenallelementsofthe

android - Kotlin 协程处理错误和实现

第一次使用协程。需要帮助。这是我的流程:Presenter想要登录所以调用RepositoryInterface。Repository实现RepositoryInterface。所以Repository调用APIInterface。APIInterface由APIInterfaceImpl实现。APIInterfaceImpl最后调用MyRetrofitInterface。这是流程图:Presenter->Repository->APIInterfaceImpl->MyRetrofitInterface一旦我收到登录响应:APIInterfaceImpl->Repository->将数

带有 Kotlin 的 Android - 如何使用 HttpUrlConnection

我试图从AsyncTask中的url获取数据,但在创建HttpUrlConnection的新实例时出现错误。Java上类似这样的东西URLurl=newURL("http://www.android.com/");HttpURLConnectionurlConnection=(HttpURLConnection)url.openConnection();try{InputStreamin=newBufferedInputStream(urlConnection.getInputStream());readStream(in);finally{urlConnection.disconne

Android - 在 Kotlin 中声明和使用 View

我正在尝试将kotlin用于android并尝试像这样声明Linearlayout:internalvarlinlay_SuccessfulPayment:LinearLayout=null!!internalvarlinlay_failPayment:LinearLayoutlinlay_SuccessfulPayment=findViewById(R.id.linlay_SuccessfulPayment)asLinearLayoutlinlay_failPayment=findViewById(R.id.linlay_failPayment)asLinearLayout但是在日志

java - 将 android hashmap 转换为 kotlin

我有一个JavaHashMap填充为HashMapmyMMap=newHashMap();for(inti=0;i我正在尝试将其转换为Kotlin。我尝试了下面的方法,但我得到的是空值。varmyMap:HashMap?=nullfor(iinobjects){//myMap?.put(i,objects.indexOf(i))myMap?.put("sample",3)System.out.println("myMapInForLoop"+myMap)}它打印I/System.out:myMapInForLoopnull。我试过使用hashMapOf函数,但它只允许1个值,所以我不能

android - 如何在 Kotlin for Android 上使用 "setTextColor(hexaValue)",

背景在Java中,我可以直接更改TextView的文本颜色,使用它的标准十六进制值:textView.setTextColor(0xffffffff);//whitetextView.setTextColor(0x00000000);//transparenttextView.setTextColor(0xff000000);//blacktextView.setTextColor(0xff0000ff);//blue//etc...非常简单...问题在Kotlin上,如果我尝试编写这样的东西,我会遇到一个奇怪的构建错误:Error:(15,18)Noneofthefollowingf

android - 在 kotlin 的 Elvis 运算符中使用多行

我在AndroidStudio中有这段代码:valnewUser=!intent.hasExtra("newUser")valuserData=intent.getParcelableExtra("newUser")?:UserData()这段代码有问题。如果extra不是UserData存在于intent中并且它的键是“newUser”,则newUser变为false但userData成为UserData的新实例。我正在寻找这样的东西:valuserData=intent.getParcelableExtra("newUser")?:{newUser=trueUserData()}我

android - 使用 Kotlin 在 fragment 中引用 RecyclerView 时出现空指针错误

当我遇到一个问题时,我正在尝试kotlin及其在android中令人敬畏的新功能。kotlin中有一个很酷的东西,您可以直接使用xml中的id,而无需使用findViewById()。但是当我将它应用于回收器View并尝试使其工作时,我得到了一个空指针异常。我检查了xml,我确实有一个带有我使用的ID的回收站View。你们能帮我弄清楚为什么会出现空指针异常吗?这是我引用回收器View并对其应用方法的代码。with(rv_my_device){setHasFixedSize(true)layoutManager=LinearLayoutManager(activity)listAdapt

java - 如何在 Kotlin 上绕过 NetworkOnMainThreadException

您好,我从kotlin开始,现在AndroidStudio3.0支持它,但我不知道如何在另一个线程中执行简单的网络请求...在java中很容易newThread(newRunnable(){@Overridepublicvoidrun(){//DodomeNetworkRequestrunOnUiThread(newRunnable(){@Overridepublicvoidrun(){//UpdateUI}});}}).start();我知道我可以执行AsyncTask和blablabla...但我不想那样。我想要一个简单的解决方案,而不需要创建额外的类和复杂的用例这在Kotlin中

android - Kotlin + Room : java. lang.IllegalArgumentException:void 无法转换为 Element

我正在尝试在我的Java+Kotlin项目中添加Room,但是当我尝试编译该项目时,它在:app:kaptDebugKotlin上失败并出现以下错误:e:java.lang.IllegalStateException:failedtoanalyze:java.lang.IllegalArgumentException:voidcannotbeconvertedtoanElementatorg.jetbrains.kotlin.analyzer.AnalysisResult.throwIfError(AnalysisResult.kt:57)atorg.jetbrains.kotlin.