即使AppObject不为null,appPackageName仍返回null

问题描述

我是一名全新的应用程序开发人员,使用Kotlin创建了启动器应用程序。我正在运行以下代码,以便从我制作的应用程序抽屉中启动应用程序:

class AppAdapter(context:Context,appList:List<AppObject>):BaseAdapter() {
private val context:Context
private val appList:List<AppObject>
private val inflater: LayoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
init{
    this.context = context
    this.appList = appList
}

override fun getCount(): Int {
    return appList.size
}

override fun getItem(position:Int): Any {
    return appList[position]
}

override fun getItemId(position:Int):Long {
    return position.toLong()
}

override fun getView(position:Int,convertView:View?,parent:ViewGroup):View
{
    val v:View
    if (convertView == null)
    {
        v = inflater.inflate(R.layout.item_app,parent,false)
    }
    else
    {
        v = convertView
    }

    val myLayoutView = v.findViewById(R.id.layout) as LinearLayout
    val myImageView = v.findViewById(R.id.image) as ImageView
    val myLabelView =v.findViewById(R.id.label) as TextView

    val app = getItem(position) as AppObject
    myLabelView.text = app.appName
    myImageView.setimageDrawable(app.appImage)

    myLayoutView.setonClickListener(object : View.OnClickListener {
        override fun onClick(v: View) {
            println(app)
            val applicationPackageName: String? = app.appPackageName
            if (applicationPackageName != null) {
                val launchAppIntent = context.packageManager.getLaunchIntentForPackage(app.appPackageName)
                if (launchAppIntent != null) {
                    context.startActivity(launchAppIntent)
                }
            }
        }
    })
    return v
}

}

AppObject是

class AppObject(packageName:String?,name:CharSequence,image:Drawable) {
val appName:CharSequence
val appPackageName:String?
var appImage:Drawable
init{
    this.appName = name
    this.appPackageName = packageName
    this.appImage = image
}

}

特别注意

println(app)

在我的AppAdapter类中,使用此语句单击抽屉中的应用程序布局时,获取结果没有问题。例如,这是当我单击三个不同的应用程序时的运行结果:

I/System.out: com.example.gotime_aparentalcontrolandproductivityapp.AppObject@84ad308 I/System.out: com.example.gotime_aparentalcontrolandproductivityapp.AppObject@4587aa1 I/System.out: com.example.gotime_aparentalcontrolandproductivityapp.AppObject@88f74c6

但是当我更改打印语句以包括程序包名称时:

println(app.appPackageName)

我只会得到: I/System.out: null I/System.out: null I/System.out: null

我在做什么错了?

我觉得不应该受到指责,但是我想指出我遇到了模拟器错误Emulator: emulator: INFO: QtLogger.cpp:68: Critical: Uncaught TypeError: Cannot read property 'update' of undefined 时。而且我已经完成了AVD(Nexus 5X API 29)的冷重启

任何帮助或建议均深表感谢。谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)