【问题标题】:What goes into the context parameter of YouTubeStandalonePlayer.createVideoIntent (*context*, YTapiKey, videoID)?YouTubeStandalonePlayer.createVideoIntent (*context*, YTapiKey, videoID) 的上下文参数是什么?
【发布时间】:2019-08-01 09:26:53
【问题描述】:

我正在编写一个应用程序,并希望该应用程序使用 YouTube API 播放 YouTube 视频。我在 YouTubeStandalonePlayer.createVideoIntent (context, YTapiKey, videoID) 的 context 参数中放了什么?

文档说上下文应该是一个活动。我试过 MainActivity、activity_main 和 findViewById(R.layout.activity_main)。它们都不起作用。

MainActivity.kt 文件:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val intent = YouTubeStandalonePlayer.createVideoIntent(context, YTapiKey, videoID)
        startActivity(intent)
    }
}

activity_main.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              tools:context="com.vcsw.explicacao.MainActivity">

    <com.google.android.youtube.player.YouTubePlayerView
            android:id="@+id/youtubePlayerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

</LinearLayout>

我希望简单地将活动的名称放在意图的上下文参数中 YouTubeStandalonePlayer.createVideoIntent(context, YTapiKey, videoID) 会起作用,但它没有。

【问题讨论】:

  • 上下文使用this

标签: android kotlin youtube-api


【解决方案1】:

您的代码在活动中。要引用活动实例,请使用 this 关键字作为活动上下文。

【讨论】:

    猜你喜欢
    • 2011-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多