Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android Studio中,如何使用Kotlin中的FAB将用户发送到搜索页面?_Android_Kotlin_Android Intent_Floating Action Button - Fatal编程技术网

在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搜索