小编eva*_*als的帖子

从android kotlin中的文件夹中获取图像列表

我正在尝试使用此功能从文件夹中获取图像列表

var gpath:String = Environment.getExternalStorageDirectory().absolutePath
var spath = "testfolder"
var fullpath = File(gpath + File.separator + spath)
var list = imageReader(fullpath)

fun imageReader(root : File):ArrayList<File>{
    val a : ArrayList<File> ? = null
    val files = root.listFiles()
    for (i in 0..files.size){
        if (files[i].name.endsWith(".jpg")){
            a?.add(files[i])
        }
    }
    return a!!
}
Run Code Online (Sandbox Code Playgroud)

但我有这些例外:

java.lang.ArrayIndexOutOfBoundsException:length=3;index=3

kotin.kotlinNullPointerException

我读过这个问题,但我不知道如何解决它,

有什么帮助吗?

android arraylist nullpointerexception indexoutofboundsexception kotlin

6
推荐指数
2
解决办法
8087
查看次数

分类器没有伴随对象

我想在我的应用程序中使用 BottomNavigationView 并且我正面临着 kotlin 的这个问题(以前从未在 Java 中遇到过)我看到这条消息:分类器“listFragment”没有伴随对象,因此必须在此处初始化

这是我的代码:

private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
    when (item.itemId) {
        R.id.listNav -> {
//the problem is here in listFragment word below
            setFragment(listFragment)
            return@OnNavigationItemSelectedListener true
        }
        R.id.accountNav -> {
//the problem is here also in accountFragment word below
            setFragment(accountFragment)
            return@OnNavigationItemSelectedListener true
        }
false 
}
private fun setFragment(fragment: Fragment) {
    supportFragmentManager.beginTransaction().replace(R.id.mainFrame , fragment).commit()
}
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏:)

fragment kotlin companion-object android-studio

6
推荐指数
2
解决办法
1万
查看次数

如何从 Firebase 中的实时数据库获取数组

我想从这个 firebase-database(下图)获取数据到一个 ArrayListOf< Product> 当Productclass 是:

data class Product(
val title:String,
val photoURL:String,
val description:String,
val price:Double
)
//and i want to make an array like this 
val r = arrayListOf<Product>()
Run Code Online (Sandbox Code Playgroud)

所以基本上我想制作 Firebase_Database_products 的数组列表,感谢任何帮助:)

火力数据库

android arraylist kotlin firebase firebase-realtime-database

2
推荐指数
1
解决办法
5011
查看次数