I am having a fragment and I'm sending data(Params i.e url as string from activity) to it from an activity, but in onCreateView it's(Params) showing null and also view as null I tried the bundle method also it's also not working and the code is like this.
private const val ARG_PARAM1 = "url"
private var Arg2 = ""
var mView: View? = null
class FragmentDisplayImage : Fragment() {
private var param1: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString("url")
Arg2 = param1.toString()
// Toast.makeText(context, param1.toString() , Toast.LENGTH_SHORT).show()
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
super.onCreateView(inflater, container, savedInstanceState);
var a =this.getView()
var image= view?.findViewById<ImageView>(R.id.Fragimage)
Picasso.get().load(Arg2).resize(700, 700).centerCrop().into(image);
return inflater.inflate(R.layout.fragment_display_image, container, false)
}
how to get param1 value in oncreateView and how to solve this image/view which is giving null
CodePudding user response:
The onCreateView
method is supposed to return the view. You are getting null there because using getView
before the view is created cant give you the view. If you change that for requireView
it wont be null but it will throw an exception.
Since you are using findViewById
and not bindings what you have to do is to override onViewCreated
that method includes an argument view
.
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_display_image, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
var image= view.findViewById<ImageView>(R.id.Fragimage)
Picasso.get().load(Arg2).resize(700, 700).centerCrop().into(image);
}