出于某种原因,一个DialogFragment
每当我试图从一个click事件启动它不会出现,我刚刚创建。 为什么对话框有一番景致,在我看来已经明确规定builder.setView(rootView)
Fragment does not have a view
点击发起活动DialogFragment
holder.myButton.setOnClickListener {
val dialog = MyDialog()
val manager = (holder.itemView.context as FragmentActivity).supportFragmentManager
dialog.show(manager, "example")
}
DialogFragment
class MyDialog : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
LayoutInflater.from(requireContext())
val builder = AlertDialog.Builder(activity)
val rootView = LayoutInflater.from(requireContext()).inflate(R.layout.dialog_sample, null, false)
lateinit var tabLayout: TabLayout
lateinit var viewPager: ViewPager
builder.setIconAttribute(R.attr.myImg)
builder.setTitle("My Program")
builder.setView(rootView)
builder.setMessage("My Message")
builder.setPositiveButton(getString(R.string.ok)) { dialog, _ -> dialog.dismiss() }
tabLayout = rootView.findViewById(R.id.tabLayout)
viewPager = rootView.findViewById(R.id.masterViewPager)
val adapter = CustomAdapter(childFragmentManager)
adapter.addFragment("Boy", CustomFragment.createInstance("John"))
adapter.addFragment("Girl", CustomFragment.createInstance("Stacy"))
adapter.addFragment("Robot", CustomFragment.createInstance("Aeon"))
viewPager.adapter = adapter
tabLayout.setupWithViewPager(viewPager)
// return builder.create()
return builder.show()
}
}
dialog_sample.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<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/masterViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
CustomFragment类
class CustomFragment : androidx.fragment.app.Fragment() {
private var mText = ""
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val v = inflater.inflate(R.layout.fragment_sample, container, false)
(v.findViewById(R.id.textView_sample) as TextView).text = mText
return v
}
companion object {
fun createInstance(txt: String): CustomFragment {
val fragment = CustomFragment()
fragment.mText = txt
return fragment
}
}
}
logcat的
java.lang.IllegalStateException: Fragment does not have a view
at androidx.fragment.app.Fragment$3.onFindViewById(Fragment.java:2396)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:869)
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1229)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1295)
at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:686)
at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2057)
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1847)
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1802)
at androidx.fragment.app.FragmentManagerImpl.execSingleAction(FragmentManagerImpl.java:1679)
at androidx.fragment.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:536)
at androidx.fragment.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:148)
at androidx.viewpager.widget.ViewPager.populate(ViewPager.java:1244)
at androidx.viewpager.widget.ViewPager.populate(ViewPager.java:1092)
at androidx.viewpager.widget.ViewPager.onMeasure(ViewPager.java:1622)
at android.view.View.measure(View.java:23169)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)
at android.view.View.measure(View.java:23169)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:23169)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:23169)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)
at com.android.internal.widget.AlertDialogLayout.onMeasure(AlertDialogLayout.java:71)
at android.view.View.measure(View.java:23169)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:23169)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:23169)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:716)
at android.view.View.measure(View.java:23169)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2718)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1545)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1855)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1460)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7183)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
at android.view.Choreographer.doCallbacks(Choreographer.java:761)
at android.view.Choreographer.doFrame(Choreographer.java:696)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
I/Process: Sending signal. PID: 4867 SIG: 9
Application terminated.
适配器类
class MyRVAdapter(private val itemsList: ArrayList<Item>) : RecyclerView.Adapter<MyRVAdapter.ViewHolder>() {
override fun getItemCount(): Int {
return itemsList.size
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
// Button click listener
holder.myButton.setOnClickListener {
val dialog = CustomFragment()
val manager = (holder.itemView.context as FragmentActivity).supportFragmentManager
dialog.show(manager, dialog.tag)
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.my_cv, parent, false)
return ViewHolder(v)
}
class ViewHolder (itemView : View):RecyclerView.ViewHolder(itemView) {
val mCV = itemView.findViewById<CardView>(R.id.cv_main)!!
val myButton = itemView.findViewById<Button>(R.id.btn_main)!!
}
class MyViewPagerAdapter(manager: androidx.fragment.app.FragmentManager) : androidx.fragment.app.FragmentPagerAdapter(manager){
private val fragmentList : MutableList<androidx.fragment.app.Fragment> = java.util.ArrayList()
private val titleList : MutableList<String> = java.util.ArrayList()
override fun getItem(position: Int): androidx.fragment.app.Fragment {
return fragmentList[position]
}
override fun getCount(): Int {
return fragmentList.size
}
fun addFragment(fragment: androidx.fragment.app.Fragment, title: String){
fragmentList.add(fragment)
titleList.add(title)
}
override fun getPageTitle(position: Int): CharSequence? {
return titleList[position]
}
}
}