有人可以用这个代码帮助我吗?
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)
}
我在
startActivity(webIntent)
遇到错误:类型不匹配:推断的类型为Intent,但需要上下文]
如何将用户发送到这样的youtube页面? Toast消息运行正常,因此单击按钮已连接到XML。
如果您的mCtx
是活动context
,请使用
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
}
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);
如果您的mCtx
是活动context
,则
使用
mCtx.startActivity(webIntent)
代替
startActivity(webIntent)