Home > Mobile >  erorr when trying to reach view with findviewbyId
erorr when trying to reach view with findviewbyId

Time:01-13

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

    val viewPager = view.findViewById<ViewPager>(R.id.viewPager)
    viewPager.adapter = PageAdapter(childFragmentManager)

    val tabLayout = view.findViewById<TabLayout>(R.id.tabLayout)
    tabLayout.setupWithViewPager(viewPager)
}

}

i am getting an erorr Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type View?

can anyone help me?

CodePudding user response:

    class main_fragment : Fragment() {
   val view: View
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_main_fragment, container, false)

    val viewPager = view.findViewById<ViewPager>(R.id.viewPager)
    viewPager.adapter = PageAdapter(childFragmentManager)

    val tabLayout = view.findViewById<TabLayout>(R.id.tabLayout)
    tabLayout.setupWithViewPager(viewPager)
return view
}

}

CodePudding user response:

override onViewCreated method

@Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) and then use view parameter from this function

  • Related