关于 Kotlin 中的 packageManager 和 getLaunchIntentForPackage 的字符串不匹配错误
String mismatch error regarding packageManager and getLaunchIntentForPackage in Kotlin
我正在使用 Kotlin 创建一个 android 启动器应用程序,我正在 运行 下面的代码来激活一个应用程序。我有一个显示我的用户应用程序的应用程序抽屉,当我点击其中一个时,我希望它使用以下代码激活该应用程序:
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)
{
val launchAppIntent = context.packageManager.getLaunchIntentForPackage(app.appPackageName)
if (launchAppIntent != null)
{
context.startActivity(launchAppIntent)
}
}
})
return v
}
我得到的错误在行中:
val launchAppIntent = context.packageManager.getLaunchIntentForPackage(app.appPackageName)
appPackageName 是类型 String? 但 getLaunchIntentForPackage() 需要 字符串。我试过在 app.appPackageName 的末尾添加 toString() 但这不起作用。
我认为我遇到的问题在于我对 Kotlin 的 null 回避烦躁无知,而且我还不知道如何解决它,因为我是 Kotlin 和应用程序开发的新手。
非常感谢任何帮助。谢谢!
您收到的错误是因为当您访问 app.appPackageName
时,应用程序的包名称可能为空。这是因为此方法最初是在 Java 中编写的,并且 Kotlin 在值为 null 的情况下保护自己。这就是为什么它的类型是字符串?
要解决此问题,您需要在访问此值之前添加可空性检查。这样,Kotlin 会将其类型解释为字符串。
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)
{
val applicationPackageName : String? = app.appPackageName
if (applicationPackageName != null) {
val launchAppIntent = context.packageManager.getLaunchIntentForPackage(applicationPackageName)
if (launchAppIntent != null)
{
context.startActivity(launchAppIntent)
}
}
} else {
//YOUR LOGIC
}
})
return v
}
我正在使用 Kotlin 创建一个 android 启动器应用程序,我正在 运行 下面的代码来激活一个应用程序。我有一个显示我的用户应用程序的应用程序抽屉,当我点击其中一个时,我希望它使用以下代码激活该应用程序:
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)
{
val launchAppIntent = context.packageManager.getLaunchIntentForPackage(app.appPackageName)
if (launchAppIntent != null)
{
context.startActivity(launchAppIntent)
}
}
})
return v
}
我得到的错误在行中:
val launchAppIntent = context.packageManager.getLaunchIntentForPackage(app.appPackageName)
appPackageName 是类型 String? 但 getLaunchIntentForPackage() 需要 字符串。我试过在 app.appPackageName 的末尾添加 toString() 但这不起作用。
我认为我遇到的问题在于我对 Kotlin 的 null 回避烦躁无知,而且我还不知道如何解决它,因为我是 Kotlin 和应用程序开发的新手。
非常感谢任何帮助。谢谢!
您收到的错误是因为当您访问 app.appPackageName
时,应用程序的包名称可能为空。这是因为此方法最初是在 Java 中编写的,并且 Kotlin 在值为 null 的情况下保护自己。这就是为什么它的类型是字符串?
要解决此问题,您需要在访问此值之前添加可空性检查。这样,Kotlin 会将其类型解释为字符串。
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)
{
val applicationPackageName : String? = app.appPackageName
if (applicationPackageName != null) {
val launchAppIntent = context.packageManager.getLaunchIntentForPackage(applicationPackageName)
if (launchAppIntent != null)
{
context.startActivity(launchAppIntent)
}
}
} else {
//YOUR LOGIC
}
})
return v
}