【问题标题】:Kotlin does not allow multiple inheritance. How can I do something equivalent? [duplicate]Kotlin 不允许多重继承。我怎样才能做等效的事情? [复制]
【发布时间】:2018-09-21 19:47:23
【问题描述】:
class MainActivity : YouTubeBaseActivity() ,AppCompatActivity() (error:multiple inheritance)  {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    // attaching layout xml
    setContentView(R.layout.activity_main)
    val adapter = PagerAdapter(supportFragmentManager)
    val pager = findViewById<View>(R.id.pager) as ViewPager

    pager.adapter = adapter

    // Initializing YouTube player view
    val youTubePlayerView = findViewById(R.id.youtube_player_view) as YouTubePlayerView
    youTubePlayerView.initialize("YOUR API KEY",
            object : YouTubePlayer.OnInitializedListener {
                override fun onInitializationSuccess(provider: YouTubePlayer.Provider,
                                                     youTubePlayer: YouTubePlayer, b: Boolean) {

                    // do any work here to cue video, play video, etc.
                    youTubePlayer.cueVideo("5xVh-7ywKpE")
                }

                override fun onInitializationFailure(provider: YouTubePlayer.Provider,
                                                     youTubeInitializationResult: YouTubeInitializationResult) {

                }
            })

}

我在这个应用程序中使用YoutubeViewViewPager。我收到错误消息,因为我无法使用 AppCompatActivity().supporFragmentManager(error)

Kotlin 不支持多重继承。我该怎么做?

【问题讨论】:

  • 您不能在 Kotlin 或 Java 中进行多重继承。我不明白你想做什么。你能再解释一下并发布一些代码吗?
  • 你不能。使 YouTubeBaseActivity() 扩展 AppCompatActivity()。

标签: android inheritance kotlin


【解决方案1】:

您不应在 Avtivity 中扩展 FragmentManager。 Activity 已经包含 fragmentManager 的实例,因此您可以像 getFragmentManager() 一样获取它,而在 Kotlin 中它只是 fragmentManager

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-13
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-20
    • 1970-01-01
    • 2021-09-23
    相关资源
    最近更新 更多