appPackageName returning null even when AppObject is not null

117 views Asked by At

I am a brand new app developer creating a launcher app using Kotlin. I am running the following code in order to launch apps from the app drawer I've made:

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
}

}

And AppObject is

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
}

}

Paying special attention to the

println(app)

In my AppAdapter class, I have no issues getting results when I click on the app layouts in my drawer with this statement. For example here's my run results when I click on three different apps:

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

But when I change the print statement to include the package name:

println(app.appPackageName)

I only get: I/System.out: null I/System.out: null I/System.out: null

What am I doing wrong?

I have a feeling it's not to blame, but I'd like to note I'm getting an emulator error: Emulator: emulator: INFO: QtLogger.cpp:68: Critical: Uncaught TypeError: Cannot read property 'update' of undefined from time to time. And I've already done a cold reboot of my AVD (Nexus 5X API 29)

Any help or suggestions highly appreciated. Thank you!

0

There are 0 answers