简体   繁体   English

Android Studio 不渲染布局

[英]Android Studio does not render layout

I'm currently developing an android app and I've encountered a trouble that I'm not able to resolve myself.我目前正在开发一个 android 应用程序,但遇到了一个我自己无法解决的问题。

Introduction介绍

I've created a new project and picked "Tabbed Activity" as a template for the project.我创建了一个新项目并选择了“选项卡式活动”作为该项目的模板。 As you may know, the project created with this template, has 2 .xml files: activity_main.xml (that contains AppBarLayout and ViewPager2 ) and fragment_main.xml .您可能知道,使用此模板创建的项目有 2 个.xml文件: activity_main.xml (包含AppBarLayoutViewPager2 )和fragment_main.xml It also has MainActivity.kt set up to make tabs work, and ui.main package with 3 .kt files in it that are responsible for displaying tabs content eg "Fragment #1" etc.它还设置了MainActivity.kt以使选项卡工作,以及ui.main包,其中包含 3 个.kt文件,负责显示选项卡内容,例如“Fragment #1”等。

What do I want to have我想要什么

I need the application to have top action bar with title, logo and tabs navigation.我需要应用程序具有带有标题、徽标和选项卡导航的顶部操作栏。 In total, I need to have 3 different tabs (fragments) with their own layout and logic.总的来说,我需要有 3 个不同的选项卡(片段),它们具有自己的布局和逻辑。

What did I do and what happend我做了什么,发生了什么

So, I've customized the activity_main.xml layout, then created a new layout fragment_dashboard.xml for the one of the fragments that I want to have in the application.所以,我已经自定义了activity_main.xml布局,然后为我想要在应用程序中拥有的片段之一创建了一个新的布局fragment_dashboard.xml
I've deleted auto generated code and wrote my own.我删除了自动生成的代码并编写了自己的代码。 Since I'm mostly like as a beginner in android development, I've used google to learn how to bind tabs , fragments and main activity together.由于我最喜欢 Android 开发的初学者,因此我使用 google 学习如何将tabsfragment和 main Activity绑定在一起。 I found several articles that I considered suitable for me.我找到了几篇我认为适合我的文章。

After I finished the code, I wanted to check how my customized action bar with tabs and the half-finished fragment_dashboard.xml layout look together.完成代码后,我想检查一下我的带有选项卡的自定义操作栏和半完成的fragment_dashboard.xml布局如何组合在一起。

So I tried to run the app and encoutered the problem: when app starts in the emulated phone there is just a white screen and nothing else... (before deleting the auto-generated code for tabs it run without any problems)所以我尝试运行该应用程序并遇到了问题:当应用程序在模拟手机中启动时,只有一个白屏,没有别的......(在删除自动生成的标签代码之前,它运行没有任何问题)

What I tried to do我试图做的

First of all I tried to debug MainActivity.kt .首先,我尝试调试MainActivity.kt I put a breakpoint at the first line of the function onCreate() :我在函数onCreate()的第一行放置了一个断点:

super.onCreate(savedInstanceState, persistentState)

But when I run app in debug mode, debuger does not stop at this breakpoint.但是当我在调试模式下运行应用程序时,调试器不会在这个断点处停止。 Thus, I came to a conclusion: execution does not even get to the function onCreate() .因此,我得出了一个结论:执行甚至没有到达函数onCreate()

So, the question is: what am I doing wrong and how can I fix it to be able to see tabs and their fragments?所以,问题是:我做错了什么,我该如何修复它才能看到标签及其片段?

Code代码

DashboardFragment.kt DashboardFragment.kt

class DashboardFragment() : Fragment() {
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_dashboard, container, false);
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        Log.i("DashboardFragment","onViewCreated")
        super.onViewCreated(view, savedInstanceState)
    }
}

ViewPagerFragmentStateAdapter.kt ViewPagerFragmentStateAdapter.kt

class ViewPagerFragmentStateAdapter(fa: FragmentActivity) : FragmentStateAdapter(fa) {
    var positionToPageName = mapOf(
        0 to "Dashboard"
    )

    private var _pageNameToFragment = mapOf<String, Fragment>(
        "Dashboard" to DashboardFragment()
    )


    override fun getItemCount(): Int = _pageNameToFragment.size

    override fun createFragment(position: Int): Fragment {
        val pageName = positionToPageName[position]
        val page = _pageNameToFragment[pageName]

        return page ?: DashboardFragment() as Fragment
    }
}

ViewPagerFragmentStateAdapter.kt ViewPagerFragmentStateAdapter.kt

class MainActivity : AppCompatActivity() {
    private lateinit var tabLayout: TabLayout
    private lateinit var viewPager: ViewPager2

    override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        super.onCreate(savedInstanceState, persistentState)

        setContentView(R.layout.activity_main)

        val adapter = ViewPagerFragmentStateAdapter(this)

        viewPager = findViewById(R.id.view_pager)
        viewPager.adapter = adapter

        tabLayout = findViewById(R.id.tabs)
        TabLayoutMediator(tabLayout, viewPager) {
            tab, position -> tab.text = adapter.positionToPageName[position]
        }.attach()
    }
}

I've tried to change some things in the MainActivity.kt .我试图改变MainActivity.kt 中的一些东西。 It seems that the problem was just in the definition of the onCreate() function:问题似乎只是在onCreate()函数的定义中:

override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?)   {
    super.onCreate(savedInstanceState, persistentState)

I've created a test project with "Tabbed Activity" template again and noticed that in the new project the persistentState argument is missing.我再次使用“选项卡式活动”模板创建了一个测试项目,并注意到在新项目中缺少persistentState参数。 So, I just removed it from definition in my project and the project started working well.所以,我只是从我的项目中的定义中删除了它,并且该项目开始运行良好。

Now the definition of the onCreate() looks like this:现在onCreate()的定义如下所示:

override fun onCreate(savedInstanceState: Bundle?)   {
    super.onCreate(savedInstanceState)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM