Is it okay to delete a CompanionObject on a fragment?

141 Views Asked by At

So I'm working with Android Studio, and I found a video on youtube on how to set tab layout. I'm trying to create a fragment with new>Fragment>Fragment(Blank) and I'm following youtube instructions to remove everything else except onCreate and onCreateView in Fragment class. But when i try to put everything together, i get this error:

Classifier 'PemasukanFragment' does not have a companion object, and thus must be initialized here

I use AddActivity class to hold my fragment, the code look like this:

class AddActicity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_add_acticity)

        setUpTabs()
    }
    private fun setUpTabs(){
        val adapter= Adapter_AddActivity(supportFragmentManager)
        adapter.addFragment(PemasukanFragment,"Pemasukan")

    }
}

I have 2 fragment pages. The code look like this:

class PemasukanFragment : Fragment() {

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        arguments?.let {

        }
    }

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

The other fragment page have a similar code to this. I also have adapter that look like this:

class Adapter_AddActivity(supportFragmentManager: FragmentManager) :
    FragmentPagerAdapter(supportFragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
    private val mFragmentList = ArrayList<Fragment>()
    private val mFragmentListTitle = ArrayList<String>()

    override fun getCount(): Int {
        return mFragmentList.size
    }

    override fun getItem(position: Int): Fragment {
        return  mFragmentList[position]
    }

    override fun getPageTitle(position: Int): CharSequence? {
        return mFragmentListTitle[position]
    }
    fun addFragment(fragment: Fragment, title: String){
        mFragmentList.add(fragment)
        mFragmentListTitle.add(title)
    }
}

I don't know if companion objects can be removed. But if it can, how to solve this error? Any help is appreciated

0

There are 0 best solutions below