在片段中打开 ViewPager 使应用程序崩溃

Open ViewPager In Fragment Make App Crash

我正在处理在片段上实现 View-pager 的布局,当我在片段上实现 View-pager 布局时出现问题。当我从底部导航打开 View-pager Main Fragment 时,应用程序崩溃了,并且没有显示任何内容。 这是我实现 View-pager 的 Xml 文件主要片段。 此布局是在线性布局之后实现的

<com.google.android.material.tabs.TabLayout
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />

这是 Kotlin 文件的主要片段

class ViewPagerFragmentMain : Fragment() {

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

    val viewPager: ViewPager = view!!.findViewById(R.id.viewpager)
    val tabLayout: TabLayout = view!!.findViewById(R.id.tablayout)

    // attach tablayout with viewpager

    tabLayout.setupWithViewPager(viewPager)

    val adapter = ViewPagerAdapter(childFragmentManager)

    // add your fragments

    // add your fragments
    adapter.addFrag(FragmentViewPagerHistory(), "Tab1")
    adapter.addFrag(FragmentViewPagerHistorySecond(), "Tab2")
    // set adapter on viewpager

    // set adapter on viewpager
    viewPager.adapter = adapter


}

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_view_pager_main, container, false)
}

来自 Main Activity 我正在调用此方法打开 Fragment

 private fun ShowFragmentViewPager() {
    val newFragment: Fragment = ViewPagerFragmentMain()
    val transaction1: FragmentTransaction = supportFragmentManager.beginTransaction();
    transaction1.replace(R.id.frameLayout, newFragment);
    transaction1.addToBackStack(null);
    transaction1.commit();
}

调试器的结果是这样的

在 Logcat 中显示 Nothing

我无法理解我做错了什么...

[

实施更改代码后如下所示

您需要了解 lifecycle of Fragment。您在 onCreate() 中编写的代码必须在 onViewCreated() 中。或在 onCreateView() 中设置视图后。所以基本上

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

    val viewPager: ViewPager = view!!.findViewById(R.id.viewpager)
    val tabLayout: TabLayout = view!!.findViewById(R.id.tablayout)

    // attach tablayout with viewpager

    tabLayout.setupWithViewPager(viewPager)

    val adapter = ViewPagerAdapter(childFragmentManager)

    // add your fragments

    // add your fragments
    adapter.addFrag(FragmentViewPagerHistory(), "Tab1")
    adapter.addFrag(FragmentViewPagerHistorySecond(), "Tab2")
    // set adapter on viewpager

    // set adapter on viewpager
    viewPager.adapter = adapter
} 

以上代码导致空指针异常。因为在 onCreate() 中,视图还没有准备好,而您正在尝试访问该视图。


Update after onCreateView() mess

您需要在 onCreateView() 中编写代码,如下所示

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    val rootView = inflater.inflate(R.layout.fragment_view_pager_main, container, false)

    val viewPager: ViewPager = rootView.findViewById(R.id.viewpager)
    val tabLayout: TabLayout = rootView.findViewById(R.id.tablayout)

    // attach tablayout with viewpager

    tabLayout.setupWithViewPager(viewPager)

    val adapter = ViewPagerAdapter(childFragmentManager)

    // add your fragments

    // add your fragments
    adapter.addFrag(FragmentViewPagerHistory(), "Tab1")
    adapter.addFrag(FragmentViewPagerHistorySecond(), "Tab2")
    // set adapter on viewpager

    // set adapter on viewpager
    viewPager.adapter = adapter
    
    return rootView
}