我的密码
ViewPagerAdapter:
package io.lionpswn.kodular.notepad
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.PagerAdapter
import io.lionpswn.kodular.notepad.presenter.viewNotes.ViewNotes
class PagerAdapter(val fm: FragmentManager) :
FragmentPagerAdapter(fm) {
override fun getItem(position: Int): Fragment{
return when(position){
0 -> addToDo()
1 -> ViewToDo()
else -> ViewToDo()
}
}
override fun getCount(): Int {
return 2
}
override fun getPageTitle(position: Int): CharSequence? {
return when(position){
0 -> "ViewNotes()1"
1 -> "ViewToDo()2"
else -> "ViewToDo()3"
}
}
}
字符串
我的碎片:
package io.lionpswn.kodular.notepad.presenter.viewNotes
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import dagger.hilt.android.AndroidEntryPoint
import io.lionpswn.kodular.notepad.R
import io.lionpswn.kodular.notepad.databinding.FragmentViewnotesBinding
import io.lionpswn.kodular.notepad.domain.model.Note
@AndroidEntryPoint
class ViewNotes : Fragment() {
var isAdNotShown:Boolean=true
val viewModel: ViewNotesViewModel by viewModels()
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val binding: FragmentViewnotesBinding =DataBindingUtil.setContentView(this.requireActivity(), R.layout.fragment_viewnotes)
var context=this
val recyclerView=binding.recyclerview
var list:List\<Note\> = mutableListOf()
viewModel.onEvent(event.loadAd(this.requireContext(), binding.adView))
var adapter = RecyclerViewAdapter(list)
recyclerView.setHasFixedSize(true)
recyclerView.adapter = adapter
recyclerView.layoutManager = LinearLayoutManager(this.requireContext())
viewModel.list.observe(viewLifecycleOwner,{
Log.d("check",it.toString())
list=it
var adapter = RecyclerViewAdapter(list)
recyclerView.adapter = adapter
adapter.notifyDataSetChanged()
})
binding.fab.setOnClickListener {
viewModel.onEvent(event.newNoteFab(this.requireContext()))
}
return binding.root
}}
型
显示错误:
FATAL EXCEPTION: main
Process: io.lionpswn.kodular.notepad, PID: 24071
java.lang.IllegalArgumentException: No view found for id 0x7f08021a (io.lionpswn.kodular.notepad:id/viewPager) for fragment ViewToDo{df73341} (911e855d-2d18-427e-b76e-f31ad6ae5083 id=0x7f08021a tag=android:switcher:2131231258:1)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:547)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:272)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1943)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1839)
at androidx.fragment.app.FragmentManager.execSingleAction(FragmentManager.java:1751)
at androidx.fragment.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:323)
at androidx.fragment.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:249)
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:26010)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7105)
at androidx.coordinatorlayout.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:760)
at androidx.coordinatorlayout.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:833)
at android.view.View.measure(View.java:26010)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7105)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:145)
at android.view.View.measure(View.java:26010)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7105)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1552)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:842)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:721)
at android.view.View.measure(View.java:26010)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7105)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:26010)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7105)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1552)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:842)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:721)
at android.view.View.measure(View.java:26010)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7105)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:767)
at android.view.View.measure(View.java:26010)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:3629)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:2407)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2673)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2129)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8625)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1034)
at android.view.Choreographer.doCallbacks(Choreographer.java:858)
at android.view.Choreographer.doFrame(Choreographer.java:789)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1019)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:236)
at android.app.ActivityThread.main(ActivityThread.java:7912)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:620)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1011)
型
我希望在运行时正确地填充我的viewNotes布局。即使有我正确的布局文件,它也没有显示id的视图。我对ViewPager不太了解。但看代码是正确的。有人帮我解决这个问题。我可以填充其他空白片段。但这似乎是我的viewNNotes片段的问题。
2条答案
按热度按时间r1wp621o1#
getCount()方法返回可用视图的数量。您应该总是返回要在viewPager中显示的片段列表的大小。对于您的情况,尝试从getCount()返回3。
字符串
ldxq2e6h2#
您使用的是哪个视图分页器ViewPager还是ViewPager2?如果您使用的是ViewPager,请不要使用布局管理器。这将是很好的,如果你张贴xml代码也。