Tag: android

如何使用kotlin使用片段

我无法找到如何使用kotlin使用片段。 我在onCreateView方法中遇到错误,请帮助我。 ListaFragment.kt: class ListaFragment : Fragment() { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle): View? { val view = inflater.inflate(R.layout.fragment_lista, container, false) return view } } fragment_lista.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAlignment="center" android:text="Hello"/> </LinearLayout> MainActivity.kt: class MainActivity : AppCompatActivity() { private var listaFragment: ListaFragment? = […]

Kotlin – 在android中使用Realm模块

我有一个与Realm和一些Realm模块的Android应用程序,帮助我隔离同步Realm,我决定从Java迁移到Kotlin。 @RealmModule(classes = arrayOf(Category::class, Product::class)) private class ShopModule fun getShop(user: SyncUser, path: String): RealmConfiguration { return SyncConfiguration.Builder(user, path) .modules(ShopModule()) .build() } 函数getShop返回RealmConfiguration以在Realm.getInstance()中使用。 经过一些转换后,发生build()时发生了一些错误: io.realm.exceptions.RealmException: Could not find io.realm.ShopModuleMediator 请帮助我继续我的迁移。 我不明白什么是错的,因为这个方法在java中适用于我。 更新: 在评论和许多改善问答后,如此多的改变和尝试在我的项目中,当我的项目中使用kotlin时,发现ShopModuleMediator没有生成,当从Java应用程序项目转换到Kotlin项目已经工作干净的项目。

Kotlin:安全的lambdas(没有内存泄漏)?

在阅读了关于内存泄漏的这篇文章之后,我想知道在Kotlin Android项目中使用lambdas是否安全。 确实,lambda语法使得我的程序更加轻松,但是内存泄漏呢? 作为一个有问题的例子,我从我的一个项目中取得了一段代码,在那里构建了一个AlertDialog。 这段代码在我的项目的MainActivity类中。 fun deleteItemOnConfirmation(id: Long) : Unit { val item = explorerAdapter.getItemAt(id.toInt()) val stringId = if (item.isDirectory) R.string.about_to_delete_folder else R.string.about_to_delete_file val dialog = AlertDialog.Builder(this). setMessage(String.format(getString(stringId), item.name)).setPositiveButton( R.string.ok, {dialog: DialogInterface, id: Int -> val success = if (item.isDirectory) ExplorerFileManager.deleteFolderRecursively(item.name) else ExplorerFileManager.deleteFile(item.name) if (success) { explorerAdapter.deleteItem(item) explorerRecyclerView.invalidate() } else Toast.makeText(this@MainActivity, R.string.file_deletion_error, Toast.LENGTH_SHORT).show() }).setNegativeButton( R.string.cancel, […]

在kotlin中创建一个parcelable对象

在kotlin中创建一个parcelable对象在活动之间传递数据的最佳方法?

在java代码中使用kotlin库

我有一个kotlin库的FlowLayout库为aar , 并希望在我的java代码(android)中使用它,但不能解决kotlin.Pair 。 FlowLayoutManager layoutManager=new FlowLayoutManager(3, RecyclerView.HORIZONTAL, new FlowLayoutManager.Interface() { @Override public kotlin.Pair<Integer, Integer> getProportionalSizeForChild(int i) { return null; } }); 我已经尝试android.support.v4.util.Pair但不兼容。 如何在Java代码中使用kotlin库及其对象依赖关系?

Mocktito ArgumentCaptor Kotlin lambda与参数

我试图在Kotlin上测试这个: verify(myInterface).doSomething(argumentCaptor.capture()) capture.value.invoke(0L) 哪里做什么: doSomething((Long) -> Unit) 我怎样才能为此创建一个ArgumentCaptor? 现在我正在做这个 inline fun <reified T : Any> argumentCaptor() = ArgumentCaptor.forClass(T::class.java)!! val captor = argumentCaptor<(Long) -> Unit>() verify(mainApiInterface!!).downloadUserProfilePicture(captor.capture()) captor.value.invoke(0L) 但是我得到java.lang.IllegalStateException:captor.capture()不能为空 我也尝试整合mockito-kotlin,但我得到一个PowerMockito错误: 在org.mockito.internal.MockitoCore的类层次结构中找不到名为“reported”的实例字段。

Kotlin Coroutines是Android的正确方法

我正在尝试使用异步更新适配器内的列表,我可以看到有太多的样板。 这是使用Kotlin协同程序的正确方法吗? 这可以优化更多? fun loadListOfMediaInAsync() = async(CommonPool) { try { //Long running task adapter.listOfMediaItems.addAll(resources.getAllTracks()) runOnUiThread { adapter.notifyDataSetChanged() progress.dismiss() } } catch (e: Exception) { e.printStackTrace() runOnUiThread {progress.dismiss()} } catch (o: OutOfMemoryError) { o.printStackTrace() runOnUiThread {progress.dismiss()} } }

Kotlin版本1.1.4-eap-77不起作用

我在Kotlin开始了一个新的应用程序,但是当Android Studio完成了新的项目的构建等。是说cand下载正确版本的Kotlin: 这里是构建gradle: // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { ext.kotlin_version = '1.1.4-eap-77' repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.0.0-beta2' 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 { […]

Kotlin是否与Android上的ART 100%兼容?

ART是Android上的“新”Dalvik运行时,自从Android 5.0以来已经取代了它。 我非常喜欢在JVM v8上运行的Kotlin和JavaFX。 我不确定Kotlin编译器生成的字节码版本,或者一旦达到v1.0版本状态就会生成字节码版本。 所以问题是:ART现在是否能够运行“Kotlin代码”,并且将来能够运行Kotlin代码,例如当Kotlin编译器要发射Java8字节码时? 或者ART不依赖于Kotlin编译器发出的字节码?

“Kotlin”编程中的语言更改语言环境由于语言环境适用于Java Fine,但Kotlin语言不起作用

我在应用程序中使用kotlin语言,并在更改语言环境时遇到一些困难。 正如我已经做了,我已经创建了一个全球性的类,即在Java和这个类上,我已经写了语言改变语言环境代码的代码和语言代码函数我打电话给基础应用程序类在我的项目是在Kotlin。 这是我的源代码: 全局类(Java代码) public void changelanguage(Context context) { Locale locale = Locale.getDefault(); Locale.setDefault(locale); System.out.println("GlobalLocale" +locale.toString()); Configuration config = new Configuration(); config.locale = locale; context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics()); } 基本申请分类代码:( Kotlin) class CrashApplicationClass : Application() { val TAG = CrashApplicationClass::class.java.simpleName lateinit var sDefSystemLanguage: String val gc = GlobalClass.getInstance() override fun onCreate() { // TODO Auto-generated method stub […]