[如何在Android Studio中使用Kotlin中的FAB将用户重定向到搜索页?

问题描述 投票:0回答:2

有人可以用这个代码帮助我吗?

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。

android kotlin android-intent floating-action-button
2个回答
1
投票

如果您的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
}

0
投票
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);

0
投票

如果您的mCtx是活动context,则

使用

mCtx.startActivity(webIntent)

代替

startActivity(webIntent)
© www.soinside.com 2019 - 2024. All rights reserved.