Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – I can’t find fragment by its ID nor its tag

Posted on November 11, 2022 By

Solution 1 :

Few things:

1) No need to hold reference for supportFragmentManager, use it directly (because i am not sure if it will be null when Activity is initialized)

2) Try removing addToBackStack(null) and using findFragmentByTag("DEVICE_FRAGMENT")

3) Most importantly, Don’t try to access “things” of Fragment from Activity, do those Adapter initialization/fill in the Fragment itself. Because Fragment has its own lifecycle and you may try to access “things” at wrong lifecycle

Problem :

i am currently programming an app in Android Studio and i am having a big issue. The main problem is, that i want an activity with a fragment in it and this fragment has got a spinner. I wanted to find the spinner by id, but it always returned null and i read that i can’t use findViewById if it is not in the ContentView i just set. So i am currently trying to find the fragment that contains the spinner, but i also can’t find the fragment, i tried findFragmentById and findViewById from the FragmentManager. I always get a TypeCastException and if i try findFragmentById(…)!! it throws a NullPointer.

This is my MainActivity:

    class MainActivity : AppCompatActivity() {

        private val manager: FragmentManager? = supportFragmentManager

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            showDeviceFragment()

            val fragment = manager!!.findFragmentById(R.id.fragment_holder) as DeviceFragment
            val options = arrayOf("Wandhalterung (Arm)", "Gestellhalterung (Arm)", "Gestellhalterung")
            fragment.option.adapter = ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, options)
    }

    fun showDeviceFragment() {
        val transaction = manager!!.beginTransaction()
        val fragment = DeviceFragment()
        transaction.add(R.id.fragment_holder, fragment, "DEVICE_FRAGMENT")
        transaction.addToBackStack(null)
        transaction.commit()
    }
}

And this is the DeviceFragment:

class DeviceFragment : Fragment() {

lateinit var option : Spinner

companion object {
    fun newInstance(): DeviceFragment {
        return DeviceFragment()
    }
}

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    val view = inflater.inflate(R.layout.fragment_device, container, false)
    option = view.spinner

    return inflater?.inflate(R.layout.fragment_device, container, false)
}

}

The fragment_holder just is a FrameLayout.

READ  [FIXED] How to make reloading by swipe down for webview in Android studio?
Powered by Inline Related Posts

Thanks in advance

Comments

Comment posted by Illuyankas

If you need to use your spinner inside your activity, the best way is to made a getter inside your fragment and create varaible inside your actitivy to save your fragment in the oncreate. Inside the fragment you can find your spinner in the onViewCreated with view.findViewById.

Comment posted by EpicPandaForce

FragmentTransactions are executed deferred and not immediately. But you should be initializing Fragment views only inside the Fragment anyway.

Android Tags:android, android-fragments, android-studio, kotlin

Post navigation

Previous Post: [FIXED] android – how to change notification background color in nougat?
Next Post: [FIXED] android – How to make width ratio equal in XML GridView

Related Posts

[FIXED] java – ExecuterService’s execute() method not executing sometimes even though when it’s not shutdown or terminated – Android Android
[FIXED] android – How to display Dialog Fragment on FullScreen? Android
[FIXED] android – How to implement WebVeiw in app “kotlin”? Android
[FIXED] android – When select two item at the same time in recycle view application crash,how can i solve this? Android
[FIXED] android – FATAL EXCEPTION; SQLiteException: no such column: Android
[FIXED] ANDROID Navigation Component + BottomNavigationView with backstack Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What color are dead flea eggs?
  • What is Indiana vine?
  • What’s the downside of a Chromebook?
  • Is phosphide the same as phosphorus?
  • Why do you need an S bend?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme