0

I'm building an app like WhatsApp using 3 tabs and every tab have a Fragment. In my first Fragment I have a RecyclerView.

Win a set the SectionsPagerAdapter to start width the RecyclerView Fragment like this:

inner class SectionsPagerAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {

        override fun getItem(position: Int): Fragment {
            // getItem is called to instantiate the fragment for the given page.
            // Return a PlaceholderFragment (defined as a static inner class below).
            //return PlaceholderFragment.newInstance(position + 1)
            when(position){
                0 ->  return Fragmenttask.newInstance()
                1 ->  return FragmentChat.newInstance()
                2 ->  return FragmentMaps.newInstance()
                else  -> {
                    return null!!
                }
            }
        }

        override fun getCount(): Int {
            // Show 3 total pages.
            return 3
        }
    }

It give me this error:

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.deraah.mohamed.deraahpro, PID: 21993 java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. at android.view.ViewGroup.addViewInner(ViewGroup.java:4937) at android.view.ViewGroup.addView(ViewGroup.java:4768) at android.support.v4.view.ViewPager.addView(ViewPager.java:1477) at android.view.ViewGroup.addView(ViewGroup.java:4708) at android.view.ViewGroup.addView(ViewGroup.java:4681) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1425) at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740) at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:794) at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580) at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367) at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322) at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:2199) at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:651) at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:145) at android.support.v4.view.ViewPager.populate(ViewPager.java:1236) at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:662) at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:624) at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:605) at android.support.design.widget.TabLayout$ViewPagerOnTabSelectedListener.onTabSelected(TabLayout.java:2170) at android.support.design.widget.TabLayout.dispatchTabSelected(TabLayout.java:1165) at android.support.design.widget.TabLayout.selectTab(TabLayout.java:1158) at android.support.design.widget.TabLayout.selectTab(TabLayout.java:1128) at android.support.design.widget.TabLayout$Tab.select(TabLayout.java:1427) at android.support.design.widget.TabLayout$TabView.performClick(TabLayout.java:1537) at android.view.View$PerformClick.run(View.java:24770) at android.os.Handler.handleCallback(Handler.java:790) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

UPDATE

My fragments:

FragmentChat:

/**
 * A simple [Fragment] subclass.
 * Activities that contain this fragment must implement the
 * [FragmentChat.OnFragmentInteractionListener] interface
 * to handle interaction events.
 * Use the [FragmentChat.newInstance] factory method to
 * create an instance of this fragment.
 */
class FragmentChat : Fragment() {

    private var mListener: OnFragmentInteractionListener? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

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

    // TODO: Rename method, update argument and hook method into UI event
    fun onButtonPressed(uri: Uri) {
        if (mListener != null) {
            mListener!!.onFragmentInteraction(uri)
        }
    }

    override fun onAttach(context: Context?) {
        super.onAttach(context)
    }

    override fun onDetach() {
        super.onDetach()
        mListener = null
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     *
     *
     * See the Android Training lesson [Communicating with Other Fragments](http://developer.android.com/training/basics/fragments/communicating.html) for more information.
     */
    interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        fun onFragmentInteraction(uri: Uri)
    }

    companion object {

        // TODO: Rename and change types and number of parameters
        fun newInstance(): FragmentChat {
            val fragment = FragmentChat()
            val args = Bundle()
            fragment.arguments = args
            return fragment
        }
    }
}// Required empty public constructor

FragmentMaps:

/**
 * A simple [Fragment] subclass.
 * Activities that contain this fragment must implement the
 * [FragmentMaps.OnFragmentInteractionListener] interface
 * to handle interaction events.
 * Use the [FragmentMaps.newInstance] factory method to
 * create an instance of this fragment.
 */
class FragmentMaps : Fragment() {

    private var mListener: OnFragmentInteractionListener? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

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

    // TODO: Rename method, update argument and hook method into UI event
    fun onButtonPressed(uri: Uri) {
        if (mListener != null) {
            mListener!!.onFragmentInteraction(uri)
        }
    }

    override fun onDetach() {
        super.onDetach()
        mListener = null
    }


    interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        fun onFragmentInteraction(uri: Uri)
    }

    companion object {
        // TODO: Rename and change types and number of parameters
        fun newInstance(): FragmentMaps {
            val fragment = FragmentMaps()
            val args = Bundle()
            fragment.arguments = args
            return fragment
        }
    }
}// Required empty public constructor

Fragmenttask :

class Fragmenttask : Fragment() {

    private var mListener: OnFragmentInteractionListener? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        val CustumViewtask = inflater!!.inflate(R.layout.fragment_fragmenttask, container, false)
        val taskMainRV = CustumViewtask.findViewById(R.id.recyclerView_main) as RecyclerView
        //taskMainRV.setBackgroundColor(Color.BLUE)

        taskMainRV.layoutManager = LinearLayoutManager(context)
        //recyclerView_main.adapter = MainAdapter()

        fetchJson(taskMainRV)

        return taskMainRV
    }

    fun fetchJson(RSV: RecyclerView) {

        //SharedPreferences
        val MY_APP_INFO: String = "UserInfo"
        val prefs = activity.getSharedPreferences(MY_APP_INFO, AppCompatActivity.MODE_PRIVATE)
        val LoggedUserId = prefs.getString("UserId", null)
        println("your code is : $LoggedUserId")

        println("Attempting to Fetch JSON")

        val url = "http://Restful.com/get_tasks.php"

        val client = OkHttpClient()

        val formBody = FormBody.Builder().add("UserId", LoggedUserId).build()

        val request = Request.Builder().url(url)
                .post(formBody)
                .build()

        client.newCall(request).enqueue(object: Callback {
            override fun onResponse(call: Call?, response: Response?) {
                val body = response?.body()?.string()
                println("mohamed : $body")

                val gson = GsonBuilder().create()

                val tasksfeed = gson.fromJson(body, M_tasksFeed::class.java)

                activity.runOnUiThread {
                    RSV.adapter = MainAdaptertasks(tasksfeed)
                }

            }

            override fun onFailure(call: Call?, e: IOException?) {
                println("Failed to execute request")
            }
        })
    }

    // TODO: Rename method, update argument and hook method into UI event
    fun onButtonPressed(uri: Uri) {
        if (mListener != null) {
            mListener!!.onFragmentInteraction(uri)
        }
    }

    override fun onDetach() {
        super.onDetach()
        mListener = null
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     *
     *
     * See the Android Training lesson [Communicating with Other Fragments](http://developer.android.com/training/basics/fragments/communicating.html) for more information.
     */
    interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        fun onFragmentInteraction(uri: Uri)
    }

    companion object {
        fun newInstance(): Fragmenttask {
            val fragment = Fragmenttask()
            val args = Bundle()
            fragment.arguments = args
            return fragment
        }
    }
}// Required empty public constructor
Benjamin
  • 7,055
  • 6
  • 40
  • 60
unpitted.com
  • 54
  • 1
  • 9

3 Answers3

2

You are returning wrong view check in your Fragmenttask Fragment

Use this

 return CustumViewtask

Instead of this

return taskMainRV

Change your code like this

override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        val CustumViewtask = inflater!!.inflate(R.layout.fragment_fragmenttask, container, false)
        val taskMainRV = CustumViewtask.findViewById(R.id.recyclerView_main) as RecyclerView
        //taskMainRV.setBackgroundColor(Color.BLUE)

        taskMainRV.layoutManager = LinearLayoutManager(context)
        //recyclerView_main.adapter = MainAdapter()

        fetchJson(taskMainRV)

        return CustumViewtask
    }
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
0

Problem coming from

       else  -> {
                return null!!
            }

Try this way

override fun getItem(position: Int): android.support.v4.app.Fragment {

        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
        //return PlaceholderFragment.newInstance(position + 1)
       var fragment: android.support.v4.app.Fragment? = null
       when (position) {
           0 -> fragment = Fragmenttask.newInstance()
           1 -> fragment = FragmentChat.newInstance()
           2 -> fragment = FragmentMaps.newInstance()
       }

       return fragment!!
   }

   override fun getCount(): Int {
       return 3           //no of cases/fragments
   }

}

FYI

You must call removeView() .

Each Fragment section

 View viewOBJ = inflater.inflate(R.layout.layout_child, parent_layout, false);

  return viewOBJ ;
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
  • wen i click the last one FragmentMaps.newInstance() and i click to the first Fragmenttask.newInstance() they give me the same error java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() – unpitted.com Mar 19 '18 at 07:47
  • @unpitted.com In your fragment class, you should return VIEW – IntelliJ Amiya Mar 19 '18 at 07:49
  • @unpitted.com Kindly return proper view `Fragmenttask ` fragment section – IntelliJ Amiya Mar 19 '18 at 14:10
0

Are you sure while inflating fragment's view you have set the attachRootView to false?

inflater.inflate(R.layout.fragment, container, false)
Samuel Robert
  • 10,106
  • 7
  • 39
  • 60
  • val CustumViewtask = inflater!!.inflate(R.layout.fragment_fragmenttask, container, false) val taskMainRV = CustumViewtask.findViewById(R.id.recyclerView_main) as RecyclerView – unpitted.com Mar 19 '18 at 07:48