简体   繁体   English

由于 Kotlin 中未解析的引用,无法设置文本或可绘制对象

[英]Can't set text or drawable because of unresolved references in Kotlin

I'm new to Kotlin and I try to learn it by writing simple app launcher on it using this Java guide: https://parallelcodes.com/create-android-launcher-program/ So far so good, but I'm stuck with some "Unresolved references" errors, and I know I do something wrong, but can't figure out what exactly, as I don't understand Kotlin syntax very well.我是 Kotlin 的新手,我尝试通过使用此 Java 指南在其上编写简单的应用程序启动器来学习它: Z5E056C500A1C4B6A7110B50D807BADE-5Z://laller-program/so farcreate-5Z://laller-program/so farcreate-5Z://laller-program/so farcreate有一些“未解决的引用”错误,我知道我做错了什么,但无法弄清楚究竟是什么,因为我不太了解 Kotlin 语法。 I've tried probably everything I found in Google, as well as rewriting code here and there.我可能已经尝试了我在 Google 中找到的所有内容,以及到处重写代码。 Seems like newbies often face my problem and there is a lot of that kinda questions on StackOverflow.似乎新手经常遇到我的问题,而且 StackOverflow 上有很多类似的问题。

So here are errors:所以这里有错误:

viewHolder.icon.setImageDrawable(appInfo.icon) // Unresolved reference: icon
viewHolder.label.text = appInfo.label // Unresolved reference: label
viewHolder.name.text = appInfo.name // Unresolved reference: name

Here is entire MainActivity class:这是整个 MainActivity class:

class MainActivity : AppCompatActivity() {

    var apps: List<AppInfo>? = null
    private var gridView: GridView? = null
    private var adapter: ArrayAdapter<AppInfo>? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        loadApps()
        loadListView()
        addGridListeners()
    }

    private fun loadApps() {
        val packageManager = packageManager
        if (apps == null) {
            apps = ArrayList()
            val i = Intent(Intent.ACTION_MAIN, null)
            i.addCategory(Intent.CATEGORY_LAUNCHER)
            val availableApps: List<ResolveInfo> = packageManager.queryIntentActivities(i, 0)
            for (ri in availableApps) {
                val appinfo = AppInfo()
                appinfo.label = ri.loadLabel(packageManager)
                appinfo.name = ri.activityInfo.packageName
                appinfo.icon = ri.activityInfo.loadIcon(packageManager)
                (apps as ArrayList<AppInfo>).add(appinfo)
            }
        }
    }

    private fun addGridListeners() {
        val packageManager = packageManager
        gridView!!.onItemClickListener = OnItemClickListener { adapterView, view, i, l ->
            val intent: Intent? = packageManager.getLaunchIntentForPackage(apps!![i].name.toString())
            this@MainActivity.startActivity(intent)
        }
    }

    private fun loadListView() {
        gridView = findViewById(R.id.gridview)
        if (adapter == null) {
            adapter = object : ArrayAdapter<AppInfo>(this, R.layout.apps_view, apps!!) {
                override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
                    var convertView = convertView
                    val viewHolder: Any?
                    if (convertView == null) {
                        convertView = layoutInflater.inflate(R.layout.apps_view, parent, false)
                        viewHolder = ViewHolderItem()
                        viewHolder.icon = convertView.findViewById(R.id.img_icon)
                        viewHolder.name = convertView.findViewById(R.id.txt_name)
                        viewHolder.label = convertView.findViewById(R.id.txt_label)
                        convertView.tag = viewHolder
                    } else {
                        viewHolder = convertView.tag
                    }
                    val appInfo = apps!![position]
                    viewHolder.icon.setImageDrawable(appInfo.icon)
                    viewHolder.label.text = appInfo.label
                    viewHolder.name.text = appInfo.name
                    return convertView!!
                }

                inner class ViewHolderItem {
                    var icon: ImageView? = null
                    var label: TextView? = null
                    var name: TextView? = null
                }
            }
        }
        gridView!!.adapter = adapter
    }
}

Here is activity_main.xml in case it's needed:这是 activity_main.xml 以防需要:

<?xml version="1.0" encoding="utf-8"?>
<GridView android:id="@+id/gridview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="5dp"
    android:layout_weight="5"
    android:background="#000"
    android:horizontalSpacing="1dp"
    android:verticalSpacing="1dp"
    android:numColumns="4"
    xmlns:android="http://schemas.android.com/apk/res/android" />

Here is AppInfo class:这是 AppInfo class:

class AppInfo {
    var label: CharSequence? = null
    var name: CharSequence? = null
    var icon: Drawable? = null
}

And here is apps_view.xml layout file:这是apps_view.xml布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff"
    android:orientation="vertical"
    android:padding="10dp">

    <ImageView
        android:id="@+id/img_icon"
        android:layout_width="70dp"
        android:layout_height="50dp"
        android:gravity="center_horizontal" />

    <TextView
        android:id="@+id/txt_label"
        android:layout_width="70dp"
        android:layout_height="40dp"
        android:gravity="center_horizontal"
        android:textColor="#361b1b"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/txt_name"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:textColor="#fff"
        android:textSize="0sp"
        android:textStyle="bold" />
</LinearLayout>

Hope it's clear.希望很清楚。 What have I misunderstood?我误解了什么?

You have an Unresolved reference error because of this line:由于这一行,您有一个未解决的参考错误:

val viewHolder: Any?

Here viewHolder has type Any?这里viewHolder有类型Any? and when you are referencing, for example, to label property viewHolder.label it shows the error because Any doesn't have such property.例如,当您引用label属性viewHolder.label时,它会显示错误,因为Any没有此类属性。

To solve the problem use ViewHolderItem type for viewHolder :要解决此问题,请使用ViewHolderItem类型的viewHolder

val viewHolder: ViewHolderItem

The problem is that ViewHolderItem is an inner class, which it's fields cannot be access from the outer class.问题是 ViewHolderItem 是一个内部 class,它的字段不能从外部 class 访问。 for creating a model class in kotlin use data class like this:用于在 kotlin 中创建 model class 使用数据 class

data class ViewHolderItem(val p1 : String, val p2 : Int, ...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM