i want to transfer data from activity to fragment but i get "Error inflating class fragment" I have searched for solution for that problem but still can't.
this is my full code of FirstFragment.kt
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
/**
* A simple [Fragment] subclass.
* Use the [FirstFragment.newInstance] factory method to
* create an instance of this fragment.
*/
class FirstFragment : Fragment() {
// TODO: Rename and change types of parameters
private var param1: String? = null
private var param2: String? = null
private lateinit var communicator: Communicator
private lateinit var myTextView: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_first, container, false)
myTextView = view.findViewById<View>(R.id.profile_name_text) as TextView
// Gets the data from the passed bundle
val bundle = arguments
val message = bundle!!.getString("mText")
// Sets the derived data (type String) in the TextView
myTextView.text = message
return view
}
companion object {
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* #param param1 Parameter 1.
* #param param2 Parameter 2.
* #return A new instance of fragment FirstFragment.
*/
// TODO: Rename and change types and number of parameters
#JvmStatic
fun newInstance(param1: String, param2: String) =
FirstFragment().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
}
}
my function at Home.kt
fun getData() {
val mFragmentManager = supportFragmentManager
val mFragmentTransaction = mFragmentManager.beginTransaction()
val mFragment = FirstFragment()
val mBundle = Bundle()
val testText = "My Name is Jack"
mBundle.putString("mText",testText)
mFragment.arguments = mBundle
mFragmentTransaction.add(R.id.fragmentContainerView, mFragment).commit()
}
at the fragment_first.xml i want to put the text into the TextView
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Home">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/linear_Layout_home_1"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="30dp"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:paddingStart="15dp"
android:paddingEnd="15dp"
android:paddingTop="10dp"
android:orientation="horizontal"
android:background="#drawable/rounded_blue_shape"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/profile_name_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/nama_pengguna"
android:textStyle="bold"
android:textSize="15sp"
android:textColor="#color/cloudy"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
this is code of activity_home.xml
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Home">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="#menu/navigation"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
<fragment
android:id="#+id/fragmentContainerView"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="#navigation/my_nav"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Can anybody help me please?
Base on documentation:
Note: when manually calling setGraph() with arguments, you must not use the app:navGraph attribute when creating the NavHostFragment in XML as that will internally call setGraph() without any arguments, resulting in your graph and start destination being created twice.
docs
You've already defined app: navGraph on your activity(Fragment Container) so you can't call navController. SetGraph from java/kotlin.
Try with remove app:navGraph from Fragment Container
You can pass the data using fragment constructor like this:
fun getData() {
val mFragmentManager = supportFragmentManager
val mFragmentTransaction = mFragmentManager.beginTransaction()
val testText = "My Name is Jack"
val mFragment = FirstFragment(testText)
mFragmentTransaction.add(R.id.fragmentContainerView, mFragment).commit()
}
And fetch it in the fragment like this:
class FirstFragment(private val someText: String): Fragment() {
private lateinit var myTextView: TextView
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_first, container, false)
myTextView = view.findViewById<View>(R.id.profile_name_text) as TextView
// Sets the derived data (type String) in the TextView
myTextView.text = someText
return view
}
}
Also seeing your fragment_first.xml layout, you might wanna put it like this:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/profile_name_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/nama_pengguna"
android:textStyle="bold"
android:textSize="15sp"
android:textColor="#color/cloudy"/>
</RelativeLayout>
Not sure what caused your issue. But noticed you are using Jetpack navigation.
And you're trying to pass data to the start destination of the graph.
Here is an official doc for dealing with your case:
Pass data to the start destination.
And below are some of the code I tried myself, just for your reference:
I think in your MainActivity.kt, you would have something like this:
val navController = findNavController(R.id.fragmentContainerView)
navController.setGraph(R.navigation.my_nav, Bundle().apply {
putString("name", "My name is xx")
})
And in the first fragment, retrieve the parameter:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val name = arguments?.get("name")
Log.w("xx", "$name")
Edit: I updated the findNavController(R.id.fragmentContainerView) and navController.setGraph(R.navigation.my_nav according to the ids in your code.
Related
I'm trying to use fragments to not burden my application with too many activities.
In my mainActivity there is a map, I then created a fragment where there are editText fields, I would like to use these editText to insert numbers to be used as parameters to pass to a function created in another class called navFun.
I tried to write the code but I don't understand why I get these errors when using findViewById:
Unresolved reference: findViewById
this is my fragment code
package uk.co.lorenzopulcinelli.navigationdrawer
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import java.lang.Exception
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
/**
* A simple [Fragment] subclass.
* Use the [CreatePathFragment.newInstance] factory method to
* create an instance of this fragment.
*/
class CreatePathFragment : Fragment() {
// TODO: Rename and change types of parameters
private var param1: String? = null
private var param2: String? = null
val mainActivity: MainActivity = MainActivity()
val navFun: NavFun = NavFun(mainActivity.context, mainActivity.mapView)
private lateinit var editTextLatitudineP1: EditText
private lateinit var editTextLongitudineP1: EditText
private lateinit var editTextLatitudineP2: EditText
private lateinit var editTextLongitudineP2: EditText
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// create path between editText points
editTextLatitudineP1 = findViewById<EditText>(R.id.editTextLatitudineP1)
editTextLongitudineP1 = findViewById<EditText>(R.id.editTextLongitudineP1)
editTextLatitudineP2 = findViewById<EditText>(R.id.editTextLatitudineP2)
editTextLongitudineP2 = findViewById<EditText>(R.id.editTextLongitudineP2)
val b = findViewById<Button>(R.id.location)
b.setOnClickListener{
try {
Log.d("Path", "Clicked")
val p1lati: Double = editTextLatitudineP1.text.toString().toDouble()
val p1long: Double = editTextLongitudineP1.text.toString().toDouble()
val p2lati: Double = editTextLatitudineP2.text.toString().toDouble()
val p2long: Double = editTextLongitudineP2.text.toString().toDouble()
println("latitudine: $p2lati, longitudine: $p2long")
navFun.routePath(p1lati, p1long, p2lati, p2long)
mainActivity.mapView.invalidate()
} catch (e: Exception) {
Toast.makeText(context, "Error when entering coordinates", Toast.LENGTH_SHORT).show()
}
}
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_create_path, container, false)
}
companion object {
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* #param param1 Parameter 1.
* #param param2 Parameter 2.
* #return A new instance of fragment CreatePathFragment.
*/
// TODO: Rename and change types and number of parameters
#JvmStatic
fun newInstance(param1: String, param2: String) =
CreatePathFragment().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
}
}
and this is my fragment xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
tools:context=".CreatePathFragment"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="#string/crea_percorso"
android:textColor="#color/black"
android:textSize="40sp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="#string/inserisci_le_coordinate_dei_punti_tra_cui_vuoi_creare_un_percorso" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="#string/latitudine_punto_di_partenza" />
<EditText
android:id="#+id/editTextLatitudineP1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:ems="10"
android:hint="#string/latitudine_p1"
android:inputType="numberSigned|numberDecimal" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="#string/longitudine_punto_di_partenza" />
<EditText
android:id="#+id/editTextLongitudineP1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:ems="10"
android:hint="#string/longitudine_p1"
android:inputType="numberSigned|numberDecimal" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="#string/latitudine_punto_di_arrivo" />
<EditText
android:id="#+id/editTextLatitudineP2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:ems="10"
android:hint="#string/latitudine_p2"
android:inputType="numberSigned|numberDecimal" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="#string/longitudine_punto_di_arrivo" />
<EditText
android:id="#+id/editTextLongitudineP2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:ems="10"
android:hint="#string/longitudine_p2"
android:inputType="numberSigned|numberDecimal" />
<Button
android:id="#+id/location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="#string/crea_percorso"
/>
</LinearLayout>
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_create_path, container, false)
// create path between editText points
editTextLatitudineP1 = view.findViewById<EditText>(R.id.editTextLatitudineP1)
editTextLongitudineP1 = view.findViewById<EditText>(R.id.editTextLongitudineP1)
editTextLatitudineP2 = view.findViewById<EditText>(R.id.editTextLatitudineP2)
editTextLongitudineP2 = view.findViewById<EditText>(R.id.editTextLongitudineP2)
val b = findViewById<Button>(R.id.location)
b.setOnClickListener{
try {
Log.d("Path", "Clicked")
val p1lati: Double = editTextLatitudineP1.text.toString().toDouble()
val p1long: Double = editTextLongitudineP1.text.toString().toDouble()
val p2lati: Double = editTextLatitudineP2.text.toString().toDouble()
val p2long: Double = editTextLongitudineP2.text.toString().toDouble()
println("latitudine: $p2lati, longitudine: $p2long")
navFun.routePath(p1lati, p1long, p2lati, p2long)
mainActivity.mapView.invalidate()
} catch (e: Exception) {
Toast.makeText(context, "Error when entering coordinates", Toast.LENGTH_SHORT).show()
}
}
// Inflate the layout for this fragment
return view
}
You must create your view first and than call findViewById on that view.
findViewById will not work inside onCreateView, because at that time the UI is being created, so the views are not initialized yet.
try to link the views inside onViewCreated instead. <-- you can import it using ctrl+O
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
editTextLatitudineP1 = findViewById<EditText>(R.id.editTextLatitudineP1)
//.....the rest of your code
}
Do fragment need a very precise way of handling the texts that are set in the xml of the fragment ?
I had two times very similar problems : text written in xml cannot be updated properly.
Here is my first fragment. I previously had a foolish text written in the xml for the textview 'msg2ndfragment'. The issue I had was that during the onViewCreated, the text sent by the new Activity to the text would overlap the foolish text written in the Xml.
So I removed the foolish text, and now it is fine. (I could update thee text three times in a row it would work as long as the text was not initially defined in the xml),
class SecondFragment : Fragment() {
//Passer par new instance pour créé le fragment en lui donnant le nom à afficher
companion object {
fun newInstance(title: String?): SecondFragment {
val fragmentSecond = SecondFragment()
val args = Bundle()
args.putString(MainActivity.MESSAGE_SECOND_ACTIVITE, title)
fragmentSecond.arguments = args
return fragmentSecond
}
}
private lateinit var viewModel: SecondViewModel
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
return inflater.inflate(R.layout.second_fragment, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
viewModel = ViewModelProviders.of(this).get(SecondViewModel::class.java)
val message = arguments!!.getString(MainActivity.MESSAGE_SECOND_ACTIVITE, "")
val aries = view?.findViewById<TextView>(R.id.msg2ndfragment);
aries?.text = message
}
}
But now, I have a very similar issue: in a different fragment in another activity I have an editText with a hint. I want to make the hint disappear. Same issue : if the hint is written in the Xml : any text written by the user will only overlap the old text. If I initially define the hint dynamically, the hint disappears when the user starts writing.
class MainFragment : Fragment() {
companion object {
fun newInstance() = MainFragment()
}
private lateinit var viewModel: MainViewModel
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
return inflater.inflate(R.layout.main_fragment, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
viewModel = ViewModelProviders.of(this).get(MainViewModel::class.java)
val aries = view?.findViewById<Button>(R.id.btnGo2ndActivity);
aries?.setOnClickListener { onGoSecondFragmentClick(it) }
Log.d("p1", "p1");
//Faire disparaitre le int au touch car cela ne se fait pas automatiquement
val edTxtTo2nd = view?.findViewById<EditText>(R.id.edt2ndActEditText)
edTxtTo2nd?.hint = "a"
Log.d("pouf", "pouf");
/*val edTxtTo2nd = view?.findViewById<EditText>(R.id.edt2ndActEditText)
edTxtTo2nd?.setOnClickListener(View.OnClickListener { v ->
edTxtTo2nd?.setHint("")
}) */
}
private var listener: onMvmtClickListener? = null
public interface onMvmtClickListener {
fun onNextActivityClick(name1: String)
}
// Store the listener (activity) that will have events fired once the fragment is attached
override fun onAttach(context: Context) {
super.onAttach(context)
if (context is onMvmtClickListener) {
listener = context as onMvmtClickListener
} else {
throw ClassCastException(
"$context must implement nMvmtClickListener"
)
}
}
fun onGoSecondFragmentClick(v: View?) {
val nom = view?.findViewById<EditText>(R.id.edt2ndActEditText);
listener?.onNextActivityClick(nom?.text.toString())
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.main.MainFragment">
<TextView
android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/btnGo2ndActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="21dp"
android:text="#string/go_to_2nd_act"
app:layout_constraintTop_toBottomOf="#+id/edt2ndActEditText"
tools:layout_editor_absoluteX="111dp"
tools:ignore="MissingConstraints" />
<EditText
android:id="#+id/edt2ndActEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="35dp"
android:layout_marginBottom="21dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintBottom_toTopOf="#+id/btnGo2ndActivity"
app:layout_constraintTop_toTopOf="#+id/message"
tools:layout_editor_absoluteX="95dp"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
Edit : here are the main_activity kotelin and xml
class MainActivity : AppCompatActivity(), MainFragment.onMvmtClickListener {
companion object{
const val MESSAGE_SECOND_ACTIVITE = "Message.s
econd.activite"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)
if (savedInstanceState == null) {
supportFragmentManager.beginTransaction()
.replace(R.id.container, MainFragment.newInstance())
.commitNow()
}
}
override fun onNextActivityClick(name1: String) {
var intent = Intent(this, SecondActivity::class.java)
intent.putExtra(MESSAGE_SECOND_ACTIVITE, name1)
startActivity(intent)
}
}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id
/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<fragment
android:id="#+id/fragment"
android:name="com.example.myapplication.ui.main.MainFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
Using this workaround to define any text or hint feels wrong to me. How can I define them in the Xml of the fragment without having this overlap issue ?
Thank you in advance for any help.
I am attempting to adapt an ArrayList to an AutoCompleteTextView in a Fragment. I have had this working on another program, where I am applying the adapter in the MainActivity.
The error I am getting is:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.AutoCompleteTextView.setAdapter(android.widget.ListAdapter)' on a null object reference
My Fragment code - newFlightFrament.kt:
package com.android.joncb.flightlogbook
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.widget.ArrayAdapter
import com.android.joncb.flightlogbook.ExtFunctions.CreateList
import com.android.joncb.flightlogbook.dto.AirlineDTO
import com.google.gson.GsonBuilder
import kotlinx.android.synthetic.main.fragment_new_flight.*
private const val ARG_PARAM1 = "airlines"
class newFlightFragment : Fragment() {
// TODO: Rename and change types of parameters
private var param1: String? = null
private var airlineNameOnly = ArrayList<String>()
private var airlineFSCodeOnly = ArrayList<String>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
// Log.e("args", param1)
println(param1)
val gson = GsonBuilder().create()
val tempList :List<AirlineDTO> = gson.fromJson(param1,Array<AirlineDTO>::class.java).toList()
val airlines :ArrayList<AirlineDTO> = arrayListOf(AirlineDTO())
airlines.addAll(tempList)
airlineNameOnly = CreateList(airlines, "airlineName")!!
airlineFSCodeOnly = CreateList(airlines, "FS")!!
}
// val adapter = context?.let {
// ArrayAdapter<String>(
// it,
// android.R.layout.simple_list_item_1,
// airlineNameOnly
// )
// }
val adapter = ArrayAdapter<String>(requireActivity().baseContext,android.R.layout.simple_list_item_1,airlineNameOnly)
actxtAirlineName.setAdapter(adapter)
}
}
My Fragment XML - fragment_new_flight.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimary"
tools:context=".newFlightFragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/lblNewFlightInstruction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="#dimen/abPadding"
android:text="#string/lblNewFltInstr"
android:textSize="#dimen/medFont"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/abPadding"
android:paddingVertical="8dp"
android:text="#string/airline_name"
android:textSize="#dimen/medFont"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/lblNewFlightInstruction" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/abPadding"
android:paddingVertical="8dp"
android:text="#string/fsCode"
android:textSize="#dimen/medFont"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/actxtAirlineName" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/abPadding"
android:paddingVertical="8dp"
android:text="#string/fltNumber"
android:textSize="#dimen/medFont"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/etxtFSCode" />
<AutoCompleteTextView
android:id="#+id/actxtAirlineName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/airline_name"
android:paddingStart="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/textView3"
app:layout_constraintTop_toTopOf="#+id/textView" />
.
.
.
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>
I am getting the error when I setAdapter:
actxtAirlineName.setAdapter(adapter)
Note in my commented out section above this call I have tried another approach, which results in the same error.
I can confirm that the ArrayList airlineNameOnly is populated correctly.
Any ideas are good ideas.
Thanks.
You forget override onCreateView
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_new_flight, container, false)
}
Change onCreate to onViewCreated, when onCreate call view doesn't inflate
I cannot see where you inflated your fragment's view. Perhaps there's the issue?
Change your code according to mine
First include this
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout. fragment_new_flight, container, false)
}
arguments?.let {
param1 = it.getString(ARG_PARAM1)
// Log.e("args", param1)
println(param1)
val gson = GsonBuilder().create()
val tempList :List<AirlineDTO> = gson.fromJson(param1,Array<AirlineDTO>::class.java).toList()
val airlines :ArrayList<AirlineDTO> = arrayListOf(AirlineDTO())
airlines.addAll(tempList)
airlineNameOnly = CreateList(airlines, "airlineName")!!
airlineFSCodeOnly = CreateList(airlines, "FS")!!
// val adapter = context?.let {
// ArrayAdapter<String>(
// it,
// android.R.layout.simple_list_item_1,
// airlineNameOnly
// )
// }
val adapter = ArrayAdapter<String>(requireActivity().baseContext,android.R.layout.simple_list_item_1,airlineNameOnly)
actxtAirlineName.setAdapter(adapter)
}
}
Hope it will work Thankew! Happy coding!
I am making an app that has many map interactions, and one of them is seeing a little map inside an Activity where the users will be able to select a point in the map and send a request to our API.
My problem is, after I made the fragment to load the google maps and added it to the activity that should show it, the Activity itself started to crash when landing on it.
Debugging the app, I see the crash occurs on the 'SetContentView' of the Activity.
Here is the XML for the activity:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FaleConoscoActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="#style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay"/>
</com.google.android.material.appbar.AppBarLayout>
<LinearLayout
android:id="#+id/ll_main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:paddingLeft="25dp"
android:paddingTop="70dp"
android:paddingRight="25dp"
android:paddingBottom="40dp"
app:layout_anchor="#+id/ll_main_layout"
app:layout_anchorGravity="center">
//Some EditTexts and Buttons
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<fragment
android:id="#+id/fragment"
android:name="com.example.dumper2.MapFragment"
tools:layout="#layout/fragment_map"
android:layout_width="match_parent"
android:layout_height="294dp" />
</FrameLayout>
</LinearLayout>
...
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Kotlin code for the activity:
class CadastroDePontoActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_cadastro_de_ponto)
}
//Some other interactions
}
And here are the XML and Kotlin for the fragment:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapFragment">
<fragment
android:name="com.google.android.gms.maps.SupportMapFragment"
android:id="#+id/mapFrag"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
Kotlin:
class MapFragment : Fragment(), OnMapReadyCallback {
private var param1: String? = null
private var param2: String? = null
private var listener: OnFragmentInteractionListener? = null
private lateinit var mMap: GoogleMap
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val rootView = inflater.inflate(R.layout.fragment_map, container,
false)
val mapFragment = childFragmentManager.findFragmentById(R.id.mapFrag)as SupportMapFragment
mapFragment.getMapAsync(this)
return rootView
}
override fun onAttach(context: Context) {
super.onAttach(context)
if (context is OnFragmentInteractionListener) {
listener = context
} else {
throw RuntimeException(context.toString() + " must implement
OnFragmentInteractionListener")
}
}
override fun onDetach() {
super.onDetach()
listener = null
}
interface OnFragmentInteractionListener {
// TODO: Update argument type and name
fun onFragmentInteraction(uri: Uri)
}
override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
}
companion object {
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* #param param1 Parameter 1.
* #param param2 Parameter 2.
* #return A new instance of fragment MapFragment.
*/
// TODO: Rename and change types and number of parameters
#JvmStatic
fun newInstance(param1: String, param2: String) =
MapFragment().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
}
}
Without the crashlog it's just guessing, but I don't see your activity extending OnFragmentInteractionListener, so in onAttach it would throw a RuntimeException.
I am relatively new to kotlin and trying to build a project with data binding in some fragments.I have a fragment named UserFragment with a Recyclerview in it like this:
class UserFragment : Fragment() {
private lateinit var binding: FragmentUserBinding
private lateinit var viewModel: UserListViewModel
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
binding=DataBindingUtil.inflate(inflater,R.layout.fragment_user, container, false)
binding.userRecycler.layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
viewModel = ViewModelProviders.of(this).get(UserListViewModel::class.java)
viewModel.errorMessage.observe(this, Observer {
errorMessage -> if(errorMessage != null) showError(errorMessage) else hideError()
})
binding.mViewModel=viewModel
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
//start add activity
val i= Intent(activity,AddUserActivity::class.java)
userFab.setOnClickListener(View.OnClickListener {
startActivity(i)
})
}
private fun showError(#StringRes errorMessage:Int){
errorSnackbar = Snackbar.make(binding.root, errorMessage, Snackbar.LENGTH_INDEFINITE)
errorSnackbar?.setAction(R.string.retry, viewModel.errorClickListener)
errorSnackbar?.show()
}
private fun hideError(){
errorSnackbar?.dismiss()
}
}
and the xml layout file fragment_user.xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="mViewModel"
type="com.example.***.ui.User.UserListViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/userDateEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/pick_date"
android:background="#drawable/roundededittext"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginStart="8dp"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="parent"/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/userRecycler"
android:layout_width="293dp"
android:layout_height="475dp" android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="#+id/userDateEditText"
app:adapter="#{viewModel.getUserListAdapter()}"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp" app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp" android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/userFab"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_marginBottom="48dp"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"/>
<ProgressBar
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:mutableVisibility="#{viewModel.getLoadingVisibility()}"
android:id="#+id/userProgressBar" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="140dp" app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp" android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="parent" android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_bias="0.804"
app:layout_constraintVertical_bias="0.499"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
there is also a similar adapter class and item_user.xml:
class UserListAdapter : RecyclerView.Adapter<UserListAdapter.ViewHolder>() {
private lateinit var userModelList:List<UserModel>
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UserListAdapter.ViewHolder {
val binding: ItemUserBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.context), R.layout.item_user, parent, false)
return ViewHolder(binding)
}
override fun onBindViewHolder(holder: UserListAdapter.ViewHolder, position: Int) {
holder.bind(userModelList[position])
}
override fun getItemCount(): Int {
return if(::userModelList.isInitialized) userModelList.size else 0
}
fun updateUserList(userModelList:List<UserModel>){
this.userModelList = userModelList
notifyDataSetChanged()
}
class ViewHolder(private val binding: ItemUserBinding):RecyclerView.ViewHolder(binding.root){
private val viewModel = UserViewModel()
fun bind(userModel: UserModel){
viewModel.bind(userModel)
binding.viewModel =viewModel
}
}
}
the item-user.xml:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="viewModel"
type="com.example.***.ui.MyUser.UserViewModel" />
</data>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingRight="16dp">
<TextView
android:id="#+id/user_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textStyle="bold"
app:mutableText="#{viewModel.getUserTitle()}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/user_description"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:mutableText="#{viewModel.getUserDesc()}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/user_title" />
</android.support.constraint.ConstraintLayout>
</layout>
note that databinding has been enabled in the gradle
and the very important issue here is that in both my fragment and the adapter
this line:
binding.viewModel =viewModel
reports a type mismatch like this:
Type mismatch.
Required:MyUser.UserListViewModel?
Found:com.example.***.ui.MyUser.UserListViewModel
and when i build my project the error is as follows:
error: cannot find symbol
import com.example.***.databinding.FragmentUserBindingImpl;
The first error is pretty explicit: binding.viewModel expects a nullable UserListViewModel? and get a non-nullable UserListViewModel(see kotlin null safety doc).
You can try something like this to get rid of it:
Declare your viewModel as
private var viewModel: UserListViewModel? = null
and set your binding this way:
viewModel?.let{binding.viewModel = it}
Concerning the second error, your declarations seems fine, but sometimes the Android Studio's cache get corrupted, try Invalidate Caches/Restart, it may help.
Type mismatch.
Required:MyUser.UserListViewModel?
Found:com.example.***.ui.MyUser.UserListViewModel
Basically the error is telling that your
binding.viewModel //is a nullable type and there for it expects a nullable
//type to be assigned as well
So just turn your view model into a nullable type by addind the ? simbol after its delacarion (note late init types do not allow nullable types). try it like this
private var viewModel: UserListViewModel? = null
About the second error data binding library need to compile in order to autogenerate the binding class, just rebuild the project and this error will be gone.
Please feel free to use this template as a base to avoid all that boilerplate code
#Retention(AnnotationRetention.RUNTIME)
#Target(AnnotationTarget.CLASS)
annotation class ContentView(#LayoutRes val id: Int)
fun ViewGroup.inflate(#LayoutRes layoutId: Int,
addContainer: Boolean = false): View {
return LayoutInflater.from(context).inflate(layoutId,this,addContainer)
}
#Suppress("UNCHECKED_CAST")
abstract class BaseFragment<Model : ViewModel, Binding : ViewDataBinding> : Fragment() {
/**
* Data binding class variable all view elements declared on the
* Xml file will be available within this instance if a view model
* Is required for the xml to work we will need to bind it on #onBindViewModel
*/
protected lateinit var binding: WeakReference<Binding?>
/**
* Fragments view model according to MVVM android architecture
* Each fragment class should have one , in order to facilitate
* Live Data and Binding library features, tho we can skip it
*/
protected lateinit var viewModel: WeakReference<Model?>
/**
* Here is where most likely you will get not null data , both binding and
* view model references can be destroyed by garbage collector
* If this application reaches low memory levels
*
* This optional method is used to bind the required view model inside the
* Xml file, this is optional to use tho is recommended
* Bind them by calling the view model binding.customViewModel = viewModel
*/
protected open fun onBindViewModel(viewModel: Model?, binding: Binding?) {}
/**
* There will be the occasion where custom params will be needed on view model's
* Constructor in this case will want to override the default creation #see ViewModelFactory
*/
protected open fun onCreateViewModel(modelType: Class<Model>): Model? = if (modelType != ViewModel::class.java)
ViewModelProviders.of(requireActivity()).get(modelType) else null
/**
* Here we will inherit view model and binding values based on the class
* Parameters and store them in global variables so any class extending
* From Base activity has access to binding and view model instances by default
*/
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val layout = this.javaClass.annotations.find { it.annotationClass == ContentView::class } as ContentView?
?: throw RuntimeException("You annotate this class with #ContentView and provide a layout resource")
container?.let { binding = WeakReference(DataBindingUtil.bind(it.inflate(layout.id))!!) }
?: run { binding = WeakReference(DataBindingUtil.bind(inflater.inflate(layout.id, null))) }
viewModel = WeakReference(
onCreateViewModel(
(this.javaClass.genericSuperclass
as ParameterizedType).actualTypeArguments[0] as Class<Model>
)
)
setHasOptionsMenu(true)
onBindViewModel(viewModel.get(), binding.get())
return binding.get()?.root
}
}
And just use it like this (see how much boiler plate code is gone)
#ContentView(R.layout.fragment_user)
class UserFragment: BaseFragment<UserListViewModel, FragmentUserBinding> {
override fun onBindViewModel(viewModel: UserListViewModel?, binding: FragmentUserBinding?) {
binding.viewModel = viewModel
}
}