首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Kotlin: AppCompatActivity片段

Kotlin: AppCompatActivity片段
EN

Stack Overflow用户
提问于 2021-01-07 15:01:22
回答 1查看 614关注 0票数 0

AppCompatActivity()转换为Fragment(),并根据主题进行了一些更改:如何将AppCompatActivity更改为片段并更新了Gradle has本机:不幸的是,应用程序已经停止

在我创建片段视图之前,carouselView就已经工作了。它汇编:

执行任务:app:assembleDebug in project /Users/jaskier/Documents/Android/GoodTogether配置项目:应用警告:不再推荐‘kotlin扩展’Gradle插件。请使用此迁移指南(https://goo.gle/kotlin-android-extensions-deprecation)开始使用视图绑定(https://developer.android.com/topic/libraries/view-binding)和kotlin插件。配置项目:选项卡视图警告:‘kotlin-android-扩展’Gradle插件是不推荐的。请使用此迁移指南(https://goo.gle/kotlin-android-extensions-deprecation)开始使用视图绑定(https://developer.android.com/topic/libraries/view-binding)和kotlin插件。任务:app:packageDebugRenderscript :app:preDebugBuild最新任务:tabview:预览:preDebugBuild最新任务:tabview :tabview:preDebugBuild最新任务:tabview:packageDebugRenderscript NO-SOURCE Task :app:generateDebugBuildConfig -TO日期任务:app:compileDebugRenderscript NO-SOURCE Task :tabview:compileDebugAidl NO-SOURCE Task :app:generateDebugResValues TO-DATE任务:app:generateDebugResValues TO日期任务:app:generateDebugResValues generateDebugResources任务:tabview:generateDebugResources最新任务:tabview:packageDebugResources最新任务:app:mergeDebugResources最新任务:app:createDebugCompatibleScreenManifests最新任务:app:extractDeepLinksDebug UP- Task任务:tabview:extractDeepLinksDebug UP-日期任务:tabview:processDebugManifest -TO-日期任务:app:processDebugManifest UP-TO-DATE任务:tabview:parseDebugLocalResources UP-DATE任务:tabview:extractDeepLinksDebug Task :DATE:DATE UP- Task任务任务:tabview:generateDebugBuildConfig最新任务:tabview:javaPreCompileDebug最新任务:tabview:compileDebugKotlin最新日期任务:tabview:compileDebugJavaWithJavac UP- Task任务:tabview:bundleLibCompileToJarDebug UP-TO-DATE Task :app:compileDebugKotlin UP-TO DATE Task :app:javaPreCompileDebug -TO-DATE Task :app:compileDebugJavaWithJavac TO DATE Task :app:compileDebugSources Task :app:mergeDebugShaders app:mergeDebugShaders DATE任务:选项卡视图:mergeDebugShaders最新任务:tabview:编译器mergeDebugShaders无源任务:tabview:generateDebugAssets任务:tabview:packageDebugAssets UP-TO任务:app:mergeDebugAssets TO任务:app:processDebugJavaRes NO-SOURCE Task :tabview:processDebugJavaRes NO-SOURCE Task :tabview:bundleLibResDebug UP-TO-DATE Task :app:mergeDebugJavaResource UP-TO DATE:DATE:bundleLibRuntimeToJarDebug UP-DATE- Task :tabview:dexBuilderDebug app:DATE:tabview:DATE:DATE任务-DATE任务:应用程序:mergeDexDebug最新任务:app:mergeDebugJniLibFolders最新任务:tabview:mergeDebugJniLibFolders最新任务:tabview:mergeDebugNativeLibs 在829 to中成功构建40个可操作的任务:40个最新的BUILD Analyzer结果可用

但是,当我将这个片段切换为active (带有片段的tabView)时,它会给出:

不幸的是,应用程序已经停止

在从AppCompatActivity()转换时,我会错过什么?

以前:

代码语言:javascript
复制
package com.myPackage.application
import android.annotation.SuppressLint
import android.app.PendingIntent.getActivity
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.core.content.res.ResourcesCompat
import kotlinx.android.synthetic.main.activity_start_carousel.*

class StartCarouselActivity : AppCompatActivity() {

    private val movies = arrayListOf(R.drawable.ic_action_tab1, R.drawable.ic_action_tab2, R.drawable.ic_action_tab3)
    private val moviesTitles = arrayListOf("Harry Potter", "Konosuba", "I Am Legend")
    private val trending = arrayListOf(R.drawable.ic_action_tab1, R.drawable.ic_action_tab2, R.drawable.ic_action_tab3)

    private val trendingTitles = arrayListOf("Lord of the Rings", "The Last Naruto the Movie", "Spirited Away")

    @SuppressLint("UseCompatLoadingForDrawables")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_start_carousel)

        carouselView1.apply {
            size = movies.size
            resource = R.layout.start_carousel_movies_item
            scaleOnScroll = true
            spacing = 50
            hideIndicator(true)
            setCarouselViewListener { view, position ->
                val imageView = view.findViewById<ImageView>(R.id.imageView)
                imageView.setImageDrawable(ResourcesCompat.getDrawable(resources, movies[position], null))
                val textView = view.findViewById<TextView>(R.id.textViewTitle)
                textView.text = moviesTitles[position]
            }
            show()
        }

        carouselView2.apply {
            val trendingMovies = trending + movies
            val trendingTitle = trendingTitles + moviesTitles

            size = trendingMovies.size
            resource = R.layout.start_carousel_trending_item
            spacing = 50
            hideIndicator(true)
            setCarouselViewListener { view, position ->
                val imageView = view.findViewById<ImageView>(R.id.imageView)
                imageView.setImageDrawable(ResourcesCompat.getDrawable(resources, trendingMovies[position], null))
                val textView = view.findViewById<TextView>(R.id.textViewTitle)
                textView.text = trendingTitle[position]
            }
            show()
        }

    }
}

后:

代码语言:javascript
复制
package com.myPackage.application
import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.res.ResourcesCompat
import androidx.fragment.app.Fragment
import kotlinx.android.synthetic.main.activity_start_carousel.*
import kotlinx.android.synthetic.main.fragment_tab2.view.*

class TabFragment2 : Fragment() {
    private val movies = arrayListOf(
        R.drawable.ic_action_tab1,
        R.drawable.ic_action_tab2,
        R.drawable.ic_action_tab3
    )
    private val moviesTitles = arrayListOf("Harry Potter", "Konosuba", "I Am Legend")
    private val trending = arrayListOf(
        R.drawable.ic_action_tab1,
        R.drawable.ic_action_tab2,
        R.drawable.ic_action_tab3
    )

    private val trendingTitles = arrayListOf(
        "Lord of the Rings",
        "The Last Naruto the Movie",
        "Spirited Away"
    )

    @SuppressLint("UseCompatLoadingForDrawables")
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        super.onCreate(savedInstanceState)
        val v: View = inflater.inflate(R.layout.activity_start_carousel, container, false)

        carouselView1.apply {
            size = movies.size
            resource = R.layout.start_carousel_movies_item
            scaleOnScroll = true
            spacing = 50
            hideIndicator(true)
            setCarouselViewListener { view, position ->
                val imageView = view.findViewById<ImageView>(R.id.imageView)
                imageView.setImageDrawable(
                    ResourcesCompat.getDrawable(
                        resources,
                        movies[position],
                        null
                    )
                )
                val textView = view.findViewById<TextView>(R.id.textViewTitle)
                textView.text = moviesTitles[position]
            }
            show()
        }

        carouselView2.apply {
            val trendingMovies = trending + movies
            val trendingTitle = trendingTitles + moviesTitles

            size = trendingMovies.size
            resource = R.layout.start_carousel_trending_item
            spacing = 50
            hideIndicator(true)
            setCarouselViewListener { view, position ->
                val imageView = view.findViewById<ImageView>(R.id.imageView)
                imageView.setImageDrawable(
                    ResourcesCompat.getDrawable(
                        resources,
                        trendingMovies[position],
                        null
                    )
                )
                val textView = view.findViewById<TextView>(R.id.textViewTitle)
                textView.text = trendingTitle[position]
            }
            show()
        }
        return  v
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-07 16:46:32

假设您的旋转木马视图ID是carouselView1和carouselView2,则应该使用

代码语言:javascript
复制
v.findViewById<CarouselView>(R.id.carouselView1).apply { ... }

代码语言:javascript
复制
v.findViewById<CarouselView>(R.id.carouselView2).apply { ... }

onCreateView中实例化您的旋转木马视图。

kotlin中的应用函数是一个范围函数,它引用上下文对象(在本例中是两个CarouselView)并返回它。

因此,在您的示例中,v.findViewById<CarouselView>(R.id.carouselView1).apply { ... }v.findViewById<CarouselView>(R.id.carouselView2).apply { ... }都返回一个CarouselView

如果您需要在Fragment中全局使用您的视图,则此行为可能很有用。

代码语言:javascript
复制
    private var carouselView1: CarouselView? = null

    @SuppressLint("UseCompatLoadingForDrawables")
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        super.onCreate(savedInstanceState)
        val v: View = inflater.inflate(R.layout.activity_start_carousel, container, false)

        carouselView1 = v.findViewById<CarouselView>(R.id.carouselView1).apply { ... }
    }

    fun exampleFunction() {
        // Example operation to explain the global use of this variable
        carouselView1?.spacing = 10
    }

有关Kotlin的作用域函数的更多信息,请参见这里

但是,片段代码的主要问题是没有在充气视图(v)中搜索您的v。实际上,在您的AppCompactActivity中,您不需要在findViewById之前引用特定的视图,因为onCreate中的setContentView方法会自动膨胀布局。

这就是代码语法v.findViewById<ViewType>(<view-id>)的原因。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65614819

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档