在Android Studio中,如何使用Kotlin中的FAB将用户发送到搜索页面?
有人能帮我处理这个代码吗在Android Studio中,如何使用Kotlin中的FAB将用户发送到搜索页面?,android,kotlin,android-intent,floating-action-button,Android,Kotlin,Android Intent,Floating Action Button,有人能帮我处理这个代码吗 p0.btnPlay.setOnClickListener { val songName = song.songName val songArtist = song.songArtist Toast.makeText(mCtx, "You clicked this button!", Toast.LENGTH_SHORT).show() val webIntent = Intent(Intent.ACTION_WEB_SEARCH,
p0.btnPlay.setOnClickListener {
val songName = song.songName
val songArtist = song.songArtist
Toast.makeText(mCtx, "You clicked this button!", Toast.LENGTH_SHORT).show()
val webIntent = Intent(Intent.ACTION_WEB_SEARCH, Uri.parse("https://www.youtube.com/results?search_query=$songArtist+$songName"))
startActivity(webIntent)
}
我在StartActivityWebContent上遇到错误:类型不匹配:
推断类型为Intent,但应为上下文
如何将用户发送到这样的youtube页面?toast消息工作正常,因此按钮单击连接到XML 如果您的mCtx是活动上下文,则使用
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY, "https://www.youtube.com/results?search_query=$songArtist+$songName");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
try {
val webIntent = Intent(Intent.ACTION_VIEW, Uri.parse("https://www.youtube.com/results?search_query=$songArtist+$songName"))
mCtx.startActivity(webIntent)
} catch(ex: Exception) {
ex.stackTrace
}
如果您在片段中,请尝试使用
try {
val webIntent = Intent(Intent.ACTION_VIEW, Uri.parse("https://www.youtube.com/results?search_query=$songArtist+$songName"))
activity!!.startActivity(webIntent)
} catch(ex: Exception) {
ex.stackTrace
}
尝试使用mCtx.startactivityweintentc是否可以发布完整的错误?使用了第一个错误,错误消失,但按钮根本没有反应。但仍显示toast消息。请尝试使用Intent.ACTION\u视图而不是Intent.ACTION\u WEB\u搜索