【发布时间】:2019-07-23 23:46:35
【问题描述】:
我正在尝试在警报对话框中显示选项卡布局。 我收到关于找不到视图的错误 我尝试了接受的答案here。 它仍然不起作用。 我有两个简单的片段,其余的都在这里:
适配器:
class MyPagerAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm, FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
override fun getItem(position: Int): Fragment {
return when (position) {
0 -> {
RegistrationFragment()
}
else -> {
return LoginFragment()
}
}
}
override fun getCount(): Int {
return 2
}
override fun getPageTitle(position: Int): CharSequence {
return when (position) {
0 -> "Registration"
else -> {
return "Login"
}
}
}
}
类扩展DialogueFragment:
class MeDialog : DialogFragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.login_registration, container)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
val mTabLayout = view.findViewById<View>(R.id.tabs_main) as TabLayout
val mViewPager = view.findViewById<View>(R.id.viewpager_main) as ViewPager
val mThemePagerAdapter = MyPagerAdapter(childFragmentManager)
mViewPager.adapter = mThemePagerAdapter
mViewPager.currentItem = 0
mTabLayout.setupWithViewPager(mViewPager)
}
}
我这样称呼它:
MeDialog().show(supportFragmentManager, MeDialog::class.java.simpleName)
我得到的错误是:
No view found for id 0x7f09015d (app.com:id/viewpager_main) for fragment
如何让它发挥作用。
登录注册 xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabs_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:tabMode="fixed" />
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewpager_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tabs_main" />
</androidx.constraintlayout.widget.ConstraintLayout>
【问题讨论】:
-
login_registration是否确实包含 ID 为viewpager_main的视图? -
是的,一切都很好
-
我加了......
标签: android android-alertdialog android-tablayout