简体   繁体   中英

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. I've tried probably everything I found in Google, as well as rewriting code here and there. Seems like newbies often face my problem and there is a lot of that kinda questions on 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:

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:

<?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:

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

And here is apps_view.xml layout file:

<?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? and when you are referencing, for example, to label property viewHolder.label it shows the error because Any doesn't have such property.

To solve the problem use ViewHolderItem type for viewHolder :

val viewHolder: ViewHolderItem

The problem is that ViewHolderItem is an inner class, which it's fields cannot be access from the outer class. for creating a model class in kotlin use data class like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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