有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我是Kotlin的新手,我试图通过使用以下Java指南在Kotlin上编写简单的应用程序启动器来学习它:https://parallelcodes.com/create-安卓-launcher-program/ 到目前为止还不错,但我遇到了一些“未解决的引用”错误,我知道我做错了什么,但无法弄清楚到底是什么,因为我不太理解Kotlin语法。 我可能已经尝试了我在谷歌找到的所有东西,以及在这里和那里重写代码。似乎新手经常面对我的问题,在StackOverflow上有很多类似的问题

以下是错误:

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

下面是整个MainActivity类:

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
    }
}

这里是主要活动。xml,以防需要:

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

以下是AppInfo类:

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

这是你的看法。xml布局文件:

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

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

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

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

希望一切都清楚。我误解了什么


共 (2) 个答案

  1. # 1 楼答案

    由于此行,您有一个未解决的引用错误:

    val viewHolder: Any?
    

    这里viewHolder有类型Any?,例如,当您引用label属性viewHolder.label时,它会显示错误,因为Any没有这样的属性

    要解决此问题,请为viewHolder使用ViewHolderItem类型:

    val viewHolder: ViewHolderItem
    
  2. # 2 楼答案

    问题在于ViewHolderItem是一个内部类,它的字段不能从外部类访问。 要在kotlin中创建模型类,请使用如下数据类:

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