0

Here is my code on the sender fragment

 override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_list_card,container,false)
        rcvAdapter = CardAdapter(cardList,this)
        view.cardRecyclerView.layoutManager = LinearLayoutManager(container?.context,LinearLayoutManager.VERTICAL,false)
        view.cardRecyclerView.adapter = rcvAdapter
        return view
    }
    override fun onClick(card: Card) {
        val cardBundle  = Bundle()
        cardBundle.putSerializable("Card",card)
        val infoFrag = CardInfoFrag()
        infoFrag.arguments = cardBundle
        val transaction = activity?.supportFragmentManager?.beginTransaction()
        transaction?.replace(R.id.fragmentContainer,infoFrag)
        transaction?.commit()
    }

Here is the code on the receiver

class CardInfoFrag : Fragment() {
    private val card : Card? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val bundle = Bundle(arguments)
        val card : Card = bundle.getSerializable("Card") as Card
        Log.d("CARDINFO: ",card.name+" "+ card.cardDate+" "+card.cardNum)
    }
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        cardNameInfo.text = card?.name
        cardExDateInfo.text = card?.cardDate
        cardNumInfo.text = card?.cardNum
        return inflater.inflate(R.layout.fragment_card_info,container,false)
    }
}

The cardList is declared as ArrayList<Card>

The Card object

class Card (val cardNum : String, val cardDate : String, val name : String) :Serializable{

}

The logcat, when I attempt to create a new fragment with data from the passed object, says my cardNameInfo.text can't be null while the log command clearly says I received the object and its data. I can't work out what may cause the problem

Any tip is appreciated

Long Doan
  • 91
  • 8

2 Answers2

1

As its name implies, onCreateView get called before the fragment view is created; So within the onCreateView, you need to explicitly use the view that will be returned by it to access any underlying views.

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    val view = inflater.inflate(R.layout.fragment_card_info,container,false)
    cardNameInfo = view.findViewById<TextView>(R.id.foo1)
    cardExDateInfo= view.findViewById<TextView>(R.id.foo2)
    cardNumInfo= view.findViewById<TextView>(R.id.foo3)

    cardNameInfo.text = card?.name
    cardExDateInfo.text = card?.cardDate
    cardNumInfo.text = card?.cardNum
    return view
}

Change the foo Ids to the corresponding ids in the fragment layout

Zain
  • 37,492
  • 7
  • 60
  • 84
  • NVM my previous comment, I got i going. Just a quick question, normally I can set text to the textfield by doing ```textFieldId.text = ....``` but in this case, I have to declare a new val, cast it into texField like you did above to make it work. Can you elaborate this part, please ? – Long Doan Jul 20 '21 at 05:41
  • 1
    Probably you were usinf synthetic view binding... Which allows you to access views without needing to call findViewById. But it is deprecated now – Zain Jul 20 '21 at 06:07
  • Another thing that you were already defining them as fragment/activity class fields and used findViewById earlier in another context than when you call.text property.. So it is already the same case like in this question – Zain Jul 20 '21 at 06:20
0

Try this once, when you are getting the Bundle, the arguments is already a Bundle object. Maybe if you do something like this it might work: ( My answer is based off this answer :) )

class CardInfoFrag : Fragment() {

    ...

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val bundle: Bundle? = arguments

        bundle?.let{

          val card : Card = it.getSerializable("Card") as Card
          Log.d("CARDINFO: ",card.name+" "+ card.cardDate+" "+card.cardNum) 
        }
        
    }

    ...
}
mehul bisht
  • 682
  • 5
  • 15