Passing data from db_helper Activity to Fragment in kotlin - android

for Sending Data:
val u = Users()
val b = Bundle()
b.putStringArrayList("name01", name01)
u.arguments=b
for Receiving Data:
override fun onCreateView(inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?): View? {
strtext = arguments!!.getStringArrayList("name01")
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_users, container, false)
}
output: kotlin.KotlinNullPointerException
How should I get the data?
Is there is another Method?

Related

erorr when trying to reach view with findviewbyId

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?
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
}
}
override onViewCreated method
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState)
and then use view parameter from this function

Trying to sent data from activity to fragment

In Main
override fun pastDada(name: String) {
val bundle = Bundle()
bundle.putString("USER_NAME", "DDDDDDDD")
val transction = this.supportFragmentManager.beginTransaction()
val profileFragmant = fragment_profile()
profileFragmant.arguments = bundle
transction.replace(R.id.container,profileFragmant)
}
In Frafment:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
var view = inflater.inflate(R.layout.fragment_profile2, container, false)
name = arguments?.getString("USER_NAME")
Log.e("name", arguments?.getString("USER_NAME").toString())
Log.e("name", name.toString())
view.tv_name.text = name
return view
}
the result of Log.e("name", name.toString()) is always null and I dont know why
maby its becuse my arguments is empty?
and if it is,how am I supose to slove it?
Thanks
change your code like this
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
var view = inflater.inflate(R.layout.fragment_profile2, container, false)
val bundle = arguments
val name = bundle!!.getString("USER_NAME")
Log.e("name", name.toString())
view.tv_name.text = name
return view
}

Difference between onCreateView and onViewCreated in Fragment - Kotlin

I know this question has been asked many times but just wanted to have some more clarity.
So I wanted to create a simple fragment with a button that should change the fragment when clicked - very simple and basic one.
hence I create a function, and called it on onCreateView. Nothing happeded.
Then I created onViewCreated after onCreateView and called the same function in it.
It Worked.
My Question is What exactly made it work ?
here is the code
class homeFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
hello()
}
fun hello()
{
val button = view?.findViewById<Button>(R.id.button_login)
button?.setOnClickListener{
val action = homeFragmentDirections.actionHomeFragmentToLoginFragment()
findNavController().navigate(action)
Toast.makeText(context,"wao",Toast.LENGTH_LONG).show()
}
}
}
As per your comments, you did something like this:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_home, container, false)
hello() // the method has already returned a View, so this call is never reached.
}
So, when you call hello() in onCreateView after the return statement,
the method has no effect because of the return statement.
Since onViewCreated is called after the onCreateView,
the underlying View is no more null in your hello function.
If you still want to use onCreateView, you can do something like this:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val fragmentView = inflater.inflate(R.layout.fragment_home, container, false)
hello(fragmentView)
return fragmentView
}
fun hello(buttonHolderView: View?) {
val button = buttonHolderView?.findViewById<Button>(R.id.button_login)
button?.setOnClickListener {
val action = homeFragmentDirections.actionHomeFragmentToLoginFragment()
findNavController().navigate(action)
Toast.makeText(context, "wao", Toast.LENGTH_LONG).show()
}
}
You need to inflate the view before calling the function:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val fragmentHome = inflater.inflate(R.layout.fragment_home, container, false)
hello(fragmentHome)
return fragmentHome;
}
Change your hello function:
fun hello(view: View)
{
....

How to return multiple values VIEW in Kotlin?

I was trying to change ImageView in one Fragment from another Fragment. But since this cannot be done without container, I have to create another view value with the inflater method. Then I need to return these view values with the inflater methods. How do I return two views?
code is written in a fragment:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.firstfragment, container, false)
val view2 = inflater.inflate(R.layout.secondfragment, container, false)
val imgPF: ImageView = view2.findViewById(R.id.imageView)
imgPF.setImageResource(R.drawable.image_29)
view.ButtonFromFirstFragment.setOnClickListener{
//Unimportant code
}
setHasOptionsMenu(true)
return view //Approximately here i need to return two views, but how?
//And can it be done somehow differently?
}
Basically you should not have 2 fragments within a fragment. Please double check that you are not confusing fragments with views.
If you want to have those 2 fragments in an activity and you are showing both of your fragments at the same time then you should have 2 separate fragments:
Fragment A:
(...)
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.firstfragment, container, false)
view.ButtonFromFirstFragment.setOnClickListener{
//Unimportant code
}
return view
}
(...)
Fragment B:
(...)
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.secondfragment, container, false)
val imgPF: ImageView = view.findViewById(R.id.imageView)
imgPF.setImageResource(R.drawable.image_29)
return view
}

Numberpicker send data to HomeFragment (Fragment to Fragment)

I have app who have viewpagers and in inside is numberpicker. I try to send this data to my HomeFragment but crash my app. This is my source code. What i do wrong?
Create Interface for communication
interface Communicator {
fun passData(numberpicker: NumberPicker) {
}
This is my fragment with numberpicker
lateinit var comm: Communicator
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_period_length, container, false)
view.number_picker_period?.minValue = 21
view.number_picker_period?.maxValue = 35
view.number_picker_period?.wrapSelectorWheel = true
val viewPager = activity?.findViewById<ViewPager2>(R.id.viewPager)
view.next_txt_period.setOnClickListener {
viewPager?.currentItem = 1
comm.passData(view.number_picker_period)
}
return view
}
This is MainActivity
val fragment1 = DaysLength()
supportFragmentManager.beginTransaction().replace(R.id.content_id, fragment1).commit()
}
override fun passData(numberpicker: NumberPicker) {
val bundle = Bundle()
bundle.putString("input_txt", number_picker_period.toString())
val transaction = this.supportFragmentManager.beginTransaction()
val fragment2 = HomeFragment()
fragment2.arguments = bundle
}
My HomeFragment
var inputtext: String? = ""
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_home, container, false)
inputtext = arguments?.getString("input_txt")
view.output_numberpicker.text = inputtext
return view
What i do wrong?
Error
Unable to start activity ComponentInfo{com.example.oapp/com.example.oapp.MainActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f080067 (com.example.oapp:id/content_id) for fragment DaysLength{aa77410}
your main activity is crashing on this line as it doesn't know what the view "number_picker_period" is
bundle.putString("input_txt", number_picker_period.toString())
try accessing the number picker through the parameter in the function called "numberPicker", Or alternatively, change your passData interface function to return the string value instead of the entire numberPicker

Categories

Resources