0

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!

  • Questions: 1. What is `AppObject` ? is that an object you've created? 2. Are you sure that only `appPackageName` is not null? Perhaps logic for getting the `appPackageName` is causing the issue. – Sylwek845 Oct 11 '20 at 19:18
  • Hi, thank you @Sylwek good point. Here is 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 } }` – InjeffiniteIntegral Oct 11 '20 at 20:11
  • Looking at this seems like your `packageName` is nullable. and there maybe some issue with casting. Can you try setting default value = "" also possibly consider using data class. `data class AppObject(val packageName: String = "", val name: CharSequence, val image: Drawable)` This way no constructor is needed. – Sylwek845 Oct 11 '20 at 21:19
  • @Sylwek Ok, I am trying but I can't get past setting the image. I have no idea how to initialize an image and no online resource has been able to show me how `data class AppObject(val packageName:String, val name:CharSequence, val image:Drawable) { val appName:CharSequence = "" val appPackageName:String = "" val image:Drawable = ???? }` – InjeffiniteIntegral Oct 11 '20 at 22:29
  • Well it could be quite tricky to set default to a drawable as it contains resources. If you expect it to not be null you don't need to set default but just set `image` when creating the object. Data class in kotlin does not require body at all, so you could use it like. val t = AppObject("test","test1",resources.getDrawable(R.drawable.id)). If you take a look at my example above you'll see that I set default in constructor. – Sylwek845 Oct 12 '20 at 06:47

0 Answers0