Error message: No view found for id 0x7f08015b (com.example.gmerto:id/nav_host_fragment_container) for fragment Library2Fragment{aaf1679} (e698b7d5-d0dd-4123-932a-9a037673054b id=0x7f08015b)
First fragment:
adapter.setOnItemClickListener(object: RecyclerViewAdapter.onItemClickListener{
override fun onItemClick(position: Int) {
Navigation.findNavController(view).navigate(R.id.action_secondFragment_to_library2Fragment)
val fragment = Library2Fragment()
val bundle = Bundle()
val editText : EditText = view.findViewById(R.id.editTextTextPersonName2)
val input = editText.text.toString()
bundle.putString("data", input)
fragment.arguments = bundle
fragmentManager?.beginTransaction()?.replace(nav_host_fragment_container, fragment)?.commit()
Toast.makeText(context, "gmertooo $position", Toast.LENGTH_SHORT).show()
}
})
Second fragment:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_library2, container, false)
val textView: TextView = view.findViewById(R.id.abaraImushavebs)
// val args = this.arguments
// val inputData = args?.get("data")
val inputData = this.arguments?.getString("data").toString()
textView.text = inputData.toString()
return view
}
Related
i want to know how pass data from one fragment to other using LiveData
i have this code and do somethings that i know but it doesnt work pls help ...
this is my first fragmetn
enter code here
class ItemDetailsfragment : Fragment() {
lateinit var viewmodel: ViewModelPassItem
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.details_items_page, container, false)
val title = arguments?.getString("title")
view.title_details_item.text = title
val price = arguments?.getString("price")
view.price_item_details.text = price
val image = arguments?.getString("image")
Picasso.get().load(image).into(view.image_items_detail)
return view
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewmodel = ViewModelProviders.of(activity!!).get(ViewModelPassItem::class.java)
val title: TextView = view.findViewById(R.id.title_details_item)
view.plus_icon_item_details.setOnClickListener {
viewmodel.passdata(title.text.toString())
val myfragment = Cart_Fragment()
}
}
}
and this is second fragment
enter code here
class Cart_Fragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val viewitem = inflater.inflate(R.layout.cart_fragment, container, false)
val choosebtn: Button = viewitem.findViewById(R.id.choose_titme_and_submit_order_btn)
choosebtn.visibility = View.GONE
val rl_cart = viewitem.findViewById<RecyclerView>(R.id.recycler_view_cart)
rl_cart.visibility = View.GONE
val navbar: BottomNavigationView = activity!!.findViewById(R.id.nav_view_bottom_main)
navbar.visibility = View.GONE
val homeicon: ImageButton = viewitem.home_icon_cart
homeicon.setOnClickListener {
activity?.let {
val intent = Intent(requireContext(), HomeActivity::class.java)
it.startActivity(intent)
}
}
viewitem.start_shopping_in_cart.setOnClickListener {
activity?.let {
val intent = Intent(requireContext(), HomeActivity::class.java)
it.startActivity(intent)
}
}
return viewitem
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val viewmodel = ViewModelProviders.of(activity!!).get(ViewModelPassItem::class.java)
viewmodel.pass.observe(activity!!, Observer {
d("main", "${it.toString()}")
})
}
}
and this is my ViewModel Class
enter code here
class ViewModelPassItem:ViewModel() {
val pass = MutableLiveData<String>()
fun passdata (title : String) {
pass.postValue(title)
}
}
ok
when i click on btn to get string that i pass nothing happend ...
In the Cart Fragment instead of passign activity lifecycleowner pass your fragments lifecycle owner. So instead of :
viewmodel.pass.observe(activity!!, Observer {
Log.d("main", "${it.toString()}")
})
Do this :
viewmodel.pass.observe(this.viewLifecycleOwner, Observer {
Log.d("main", "${it.toString()}")
})
Fragment A:-
val pager: ViewPager = v.findViewById(R.id.viewpager)
val tabs: TabLayout = v.findViewById(R.id.tabs)
setupViewPager(pager)
tabs.setupWithViewPager(pager)
private fun setupViewPager(pager: ViewPager) {
val adapter = activity?.supportFragmentManager?.let { Adapter(it) }
adapter?.addFragment(FragmentA(), getString(R.string.FragmentA))
adapter?.addFragment(FragmentB(), getString(R.string.FragmentB))
pager.adapter = adapter
}
Fragment B:-
override fun onCreateView(inflater: LayoutInflater,
container: ViewGroup?,savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
val rootView = inflater.inflate(R.layout.layout_view, container, false)
listView = rootView.findViewById(R.id.listView)
return rootView
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
myAdapter = MyAdapter(list)
listView?.itemAnimator = DefaultItemAnimator()
listView?.adapter = myAdapter
}
Code using kotlin,
first,this is my error code.
this is my used extends BaseFragment
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val cacheView = mView == null
if (cacheView) {
mView = inflater.inflate(getFragLayoutResID(), container, false)
initView(mView!!)
}
return mView
}
//When used in an activity
fun addFragment(context: FragmentActivity, fragmentContainer: Int) {
val fragmentManager = context.supportFragmentManager
val ft = fragmentManager.beginTransaction()
ft.replace(fragmentContainer, this, javaClass.simpleName).addToBackStack(javaClass.simpleName)
.commitAllowingStateLoss()
ft.setMaxLifecycle(this, Lifecycle.State.RESUMED)
}
this is my HomeActivity
homeVideoFragment.addFragment(mContext as FragmentActivity, mainFrameLayout.id)
this is my HomeFragment
//HomeFragment
override fun initView(view: View) {//this is my homeFragment
initAdapter()
//init()
}
private var homeVideoAdapter: HomeVideoAdapter? = null
private fun initAdapter() {//this is my init adapter
val homeVideoManager = LinearLayoutManager(mContext, RecyclerView.VERTICAL, false)
homeVideoAdapter = HomeVideoAdapter(mContext!!)
home_video_rv.layoutManager = homeVideoManager
home_video_rv.adapter = homeVideoAdapter
}
private fun init() = runBlocking {
GlobalScope.launch(Dispatchers.Main) {
initAdapter()
//GlobalScope.async(Dispatchers.IO) {
//presenter!!.getVideoClassPost(AllPort.POST_GET_VIDEO_HOME)//network requests
//}.await()
}
}
When the code is run like this, it will report an error: home_video_rv must not be null.
But when I use init(), it works fine.
Although successful, but can not make network requests.
Why is this problem occurring?
Use onViewCreated method
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val cacheView = mView == null
if (cacheView) {
mView = inflater.inflate(getFragLayoutResID(), container, false)
}
return mView
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
initView(view)
}
I noticed that after creating a fragment an element is expected. In Java, all that was needed was getActivity() but it seems more difficult in Kotlin. < and > in mTwoPane = <FragmentActivity>(activity).findViewById(R.id.master_container) != null become red underlined and return this error:
Expecting an element
class MyFragment() : Fragment() {
private var mAdapter: MyListAdapter? = null
internal lateinit var mRecyclerView: RecyclerView
var mTwoPane: Boolean = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.md, container, false)
mTwoPane = <FragmentActivity>(activity).findViewById(R.id.master_container) != null
mRecyclerView = view.findViewById(R.id.recyclerView_list)
mRecyclerView.setHasFixedSize(true)
mRecyclerView.layoutManager = LinearLayoutManager(this.activity)
mRecyclerView.addItemDecoration(DividerItemDecoration(Objects.requireNonNull<Context>(context), LinearLayout.VERTICAL))
val myList = ArrayList<Product>()
val items = resources.getStringArray(R.array.product_names)
val itemDescriptions = resources.getStringArray(R.array.product_descriptions)
for (n in items.indices) {
val product = Product(items[n], itemDescriptions[n])
myList.add(product)
}
mAdapter = MyListAdapter(activity, myList, mTwoPane)
mRecyclerView.adapter = mAdapter
return view
}
}
try this
mTwoPane = (activity as FragmentActivity).findViewById<View>(R.id.master_container) != null
when I add a fragment to my view, it is not displayed. Only when I click an OptionsButton the fragment becomes visible. But I don't know if the RecyclerView just doesn't show any elements or if the fragment isn't started.
Code from MainActivity
override fun onCreate(savedInstanceState: Bundle?) {
val fragment= ArticleFragment()
var args=Bundle()
args.putInt("id",0)
fragment.arguments=args
supportFragmentManager.beginTransaction().replace(R.id.fragment_area,fragment).addToBackStack(null).commit()
}
Code from ArticleFragment
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
val view= inflater.inflate(R.layout.fragment_article, container, false)
articles=getArticleJson(url)
val recyclerView=view.findViewById<RecyclerView>(R.id.articlesRecyclerView)
recyclerView.setHasFixedSize(true)
val lm = LinearLayoutManager(activity)
recyclerView.layoutManager = lm
val adapter = ArticleAdapter(context = context, articles = articles) {
val intent = Intent(activity, ArticleWebActivity::class.java).apply
{
putExtra("article", Gson().toJson(it))
}
startActivity(intent)
}
recyclerView.adapter = adapter
return view
}