I'm currently exploring the new MvRx stack from Airbnb
Rigth now i am adding a google maps fragment successfully to a BaseMvRxFragment in a BottomNavigationView.
The problem is that the 2. time i navigate to the MapFragment my app crashes and i get the following error message:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: getflareapp.com.s2s, PID: 19184
android.view.InflateException: Binary XML file line #7: Binary XML file line #7: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
Caused by: java.lang.IllegalArgumentException: Binary XML file line #7: Duplicate id 0x7f080093, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.SupportMapFragment
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3752)
at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)
at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:405)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:387)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:780)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.view.View.inflate(View.java:23239)
at getflareapp.com.s2s.views.MapView.<init>(MapView.kt:19)
at getflareapp.com.s2s.views.MapView.<init>(MapView.kt:14)
at getflareapp.com.s2s.views.MapView.<init>(Unknown Source:6)
at getflareapp.com.s2s.views.MapViewModel_.buildView(MapViewModel_.java:42)
at getflareapp.com.s2s.views.MapViewModel_.buildView(MapViewModel_.java:22)
MapFragment.kt
/**
* A simple [BaseFragment] subclass.
*
*/
class MapFragment : BaseFragment() {
private val mViewModel: ChatViewModel by fragmentViewModel()
override fun epoxyController() = simpleController(mViewModel) {state ->
mapView{
id("map")
}
}
}
MapView.kt
#ModelView(autoLayout = ModelView.Size.MATCH_WIDTH_MATCH_HEIGHT)
class MapView #JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {
init {
// TODO: Fix crash on second view.
inflate(context, R.layout.view_map, this)
}
}
view_map.xml
<?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:layout_width="match_parent"
android:layout_height="match_parent">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/map2"
android:name="getflareapp.com.s2s.ui.map.MapFragment"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.MainActivity" />
</FrameLayout>
Thanks for any help! <3
I´ve problems adding directly the map fragment inner other fragment, so i suggest change that with Framelayout and inflate the fragment manually.
Related
I am trying to build an app with a single activity that hosts multiple fragments. I am also using Nav Graph and Data Binding.
I am getting this error which I can't find a way around it even after reviewing similar questions on StackOverflow. All I am getting is old ways about using support fragment manager and maps fragment which are not helping my issue.
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.uxstate.goldsearchkotlin/com.uxstate.goldsearchkotlin.MainActivity}: android.view.InflateException: Binary XML file line #9 in com.uxstate.goldsearchkotlin:layout/activity_main: Binary XML file line #9 in com.uxstate.goldsearchkotlin:layout/activity_main: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #9 in com.uxstate.goldsearchkotlin:layout/activity_main: Binary XML file line #9 in com.uxstate.goldsearchkotlin:layout/activity_main: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #9 in com.uxstate.goldsearchkotlin:layout/activity_main: Binary XML file line #9 in com.uxstate.goldsearchkotlin:layout/activity_main: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #9 in com.uxstate.goldsearchkotlin:layout/activity_main: Error inflating class fragment
Caused by: kotlin.UninitializedPropertyAccessException: lateinit property adapter has not been initialized
at com.uxstate.goldsearchkotlin.MainListFragment.getAdapter(MainListFragment.kt:23)
at com.uxstate.goldsearchkotlin.MainListFragment.onViewCreated(MainListFragment.kt:57)
This is my NavHostFragment which is the main cause of the crash.
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="#navigation/nav_graph" />
Surprisingly, if I change <fragment> to FrameLayout the app is not crashing but the layout is not working right with FrameLayout.
This is how I inflate the layout for the home fragment.
class MainListFragment : Fragment() {
private val viewModel: ListViewModel by activityViewModels()
private var _binding: FragmentMainListBinding? = null
private val binding get() = _binding!!
lateinit var adapter: MainAdapter
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentMainListBinding.inflate(inflater, container, false)
val root: View = binding.root
return root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// Add click listener to floating action button
binding.fabMain.setOnClickListener {
addListItem()
}
adapter = MainAdapter(viewModel.mainList)
main_list_view.adapter = adapter
main_list_view.layoutManager = LinearLayoutManager(requireContext())
}
Need help on how to address this exception.
The error came about because I had not initialized lateinit var adapter: MainAdapter
I initialized this inside onCreateView() and the app did not crash.
I am referring to both questions:
Android ViewBinding with CustomView and
How to use View Binding on custom views
I am currently migrating from Kotlin synthetics to view binding and I am using alot of custom generated views, mainly to ajust the view size when pressed. It looks like the following (prior to that was java code, if I did something wrong while switching to kotlin please feel free to correct me):
class CustomButton constructor(context: Context, attrs: AttributeSet?): AppCompatButton(context, attrs) {
#SuppressLint("ClickableViewAccessibility")
override fun onTouchEvent(event: MotionEvent): Boolean {
super.onTouchEvent(event)
if (event.action == MotionEvent.ACTION_CANCEL) {
val regainer =
AnimatorInflater.loadAnimator(context, R.animator.regain_size) as AnimatorSet
regainer.setTarget(this)
regainer.start()
return true
}
when (event.action) {
MotionEvent.ACTION_DOWN -> {
val reducer = AnimatorInflater.loadAnimator(context, R.animator.reduce_size) as AnimatorSet
reducer.setTarget(this)
reducer.start()
return true
}
MotionEvent.ACTION_UP -> {
val regainer = AnimatorInflater.loadAnimator(context, R.animator.regain_size) as AnimatorSet
regainer.setTarget(this)
regainer.start()
return true
}
}
return false
}
override fun performClick(): Boolean {
super.performClick()
return true
}
Unfortunatly all the questions I looked up so far only reffer to a solution including a custom .xml file. My binding in the activity looks like this:
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.customButton.//...
}
What I am currently getting is an android.view.InflateException through that custom view. Does anyone have an idea on how to implement view binding in this case or how to initialize it in the CustomButton class? Any help with this is much appreciated!
Best regards,
Markus
This is the stack trace of the error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.my.app, PID: 13035
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.my.app/com.my.app.MainActivity}: android.view.InflateException: Binary XML file line #39 in com.my.app:layout/activity_main: Binary XML file line #39 in com.my.app:layout/activity_main: Error inflating class com.my.app.custom.CustomTextView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3611)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3775)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2246)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:233)
at android.app.ActivityThread.main(ActivityThread.java:8010)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:631)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:978)
Caused by: android.view.InflateException: Binary XML file line #39 in com.my.app:layout/activity_main: Binary XML file line #39 in com.my.app:layout/activity_main: Error inflating class com.my.app.custom.CustomTextView
Caused by: android.view.InflateException: Binary XML file line #39 in com.my.app:layout/activity_main: Error inflating class com.my.app.custom.CustomTextView
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at android.view.LayoutInflater.createView(LayoutInflater.java:852)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1004)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1121)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
at android.view.LayoutInflater.inflate(LayoutInflater.java:680)
at android.view.LayoutInflater.inflate(LayoutInflater.java:532)
at com.my.app.databinding.ActivityMainBinding.inflate(ActivityMainBinding.java:78)
at com.my.app.databinding.ActivityMainBinding.inflate(ActivityMainBinding.java:72)
at com.my.app.MainActivity.onCreate(MainActivity.kt:59)
at android.app.Activity.performCreate(Activity.java:8006)
at android.app.Activity.performCreate(Activity.java:7990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1329)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3584)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3775)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2246)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:233)
at android.app.ActivityThread.main(ActivityThread.java:8010)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:631)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:978)
Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 5: TypedValue{t=0x2/d=0x7f0302ca a=-1}
at android.content.res.TypedArray.getColorStateList(TypedArray.java:625)
at android.widget.TextView.readTextAppearance(TextView.java:4000)
E/AndroidRuntime: at android.widget.TextView.<init>(TextView.java:1093)
at android.widget.TextView.<init>(TextView.java:994)
at androidx.appcompat.widget.AppCompatTextView.<init>(AppCompatTextView.java:102)
at androidx.appcompat.widget.AppCompatTextView.<init>(AppCompatTextView.java:97)
at com.my.app.custom.CustomTextView.<init>(CustomTextView.kt:13)
... 27 more
CustomTextView.kt:13 is this line:
class CustomTextView constructor(context: Context, attrs: AttributeSet?) : AppCompatTextView(context, attrs) {
MainActivity.kt:59 is this line:
binding = ActivityMainBinding.inflate(layoutInflater)
The activity_main.xml looks like this:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_constrainedHeight="true"
tools:context=".MainActivity">
<androidx.constraintlayout.widget.Guideline
android:id="#+id/mainGuidelineVerticalLeft"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.05"/>
<androidx.constraintlayout.widget.Guideline
android:id="#+id/mainGuidelineVerticalRight"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.95"/>
<com.my.app.custom.CustomTextView
android:id="#+id/mainSubmenuSettings"
android:layout_width="wrap_content"
android:layout_height="#dimen/minimum"
android:gravity="center"
android:paddingHorizontal="12dp"
android:layout_marginEnd="12dp"
android:text="#string/textViewSettings"
android:textColor="?attr/primaryTextColorDefault"
android:textSize="#dimen/textViewS"
android:textStyle="bold"
android:focusable="true"
android:clickable="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<com.my.app.custom.CustomCardView
android:id="#+id/mainCardView1"
android:layout_width="0dp"
android:layout_height="#dimen/cardViewHeightLarge"
android:layout_marginVertical="#dimen/cardViewMarginVertical"
android:clickable="true"
android:focusable="true"
android:elevation="#dimen/elevation"
android:foreground="?android:attr/selectableItemBackground"
app:cardCornerRadius="#dimen/cardViewCornerRadiusLarge"
app:cardUseCompatPadding="true"
app:layout_constraintBottom_toTopOf="#id/mainCardView2"
app:layout_constraintEnd_toStartOf="#+id/mainGuidelineVerticalRight"
app:layout_constraintStart_toStartOf="#+id/mainGuidelineVerticalLeft"
app:layout_constraintVertical_chainStyle="packed">
</com.my.app.custom.CustomCardView>
<com.my.app.custom.CustomCardView
android:id="#+id/mainCardView2"
android:layout_width="0dp"
android:layout_height="#dimen/cardViewHeightLarge"
android:layout_marginTop="#dimen/cardViewMarginVertical"
android:layout_marginBottom="10dp"
android:clickable="true"
android:focusable="true"
android:elevation="#dimen/elevation"
android:foreground="?android:attr/selectableItemBackground"
app:cardCornerRadius="#dimen/cardViewCornerRadiusLarge"
app:cardUseCompatPadding="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/mainGuidelineVerticalRight"
app:layout_constraintStart_toStartOf="#+id/mainGuidelineVerticalLeft"
app:layout_constraintVertical_chainStyle="packed">
</com.my.app.custom.CustomCardView>
</androidx.constraintlayout.widget.ConstraintLayout>
Nothing in the stack trace points to View Binding as being the problem here. Instead, it looks like a theme issue:
Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 5: TypedValue{t=0x2/d=0x7f0302ca a=-1}
at android.content.res.TypedArray.getColorStateList(TypedArray.java:625)
at android.widget.TextView.readTextAppearance(TextView.java:4000)
The first place I'd look is here:
<com.my.app.custom.CustomTextView
android:textColor="?attr/primaryTextColorDefault"
... />
Make sure that you actually define <item name="primaryTextColorDefault"> in your theme.
I have map fragment activity and This is the layout file
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/llLocationName">
<fragment
android:id="#+id/mapFragment"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:cameraZoom="16"
map:uiMapToolbar="false"
map:uiRotateGestures="false"
map:uiTiltGestures="false" />
Java Code I'm using
private fun inItGoogleMap() {
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
try {
val mapFragment = supportFragmentManager.findFragmentById(R.id.mapFragment) as SupportMapFragment
mapFragment.retainInstance = true
mapFragment.getMapAsync(this#HomeActivity)
}catch (e:Exception){
e.printStackTrace()
}
Error logcat in Please check once
Binary XML file line #42 in com.emojizone:layout/activity_home: Binary XML file line #42 in com.emojizone:layout/activity_home: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #42 in com.emojizone:layout/activity_home: Error inflating class fragment
Caused by: java.lang.IllegalStateException: The provided context is not an application context
at com.google.maps.api.android.lib6.common.m.e(:com.google.android.gms.dynamite_mapsdynamite#211816098#21.18.16 (150406-0):0)
at com.google.android.gms.maps.internal.CreatorImpl.b(:com.google.android.gms.dynamite_mapsdynamite#211816098#21.18.16 (150406-0):10)
at com.google.android.gms.maps.internal.CreatorImpl.newMapFragmentDelegate(:com.google.android.gms.dynamite_mapsdynamite#211816098#21.18.16 (150406-0):2)
at com.google.android.gms.maps.internal.g.aX(:com.google.android.gms.dynamite_mapsdynamite#211816098#21.18.16 (150406-0):22)
at dq.onTransact(:com.google.android.gms.dynamite_mapsdynamite#211816098#21.18.16 (150406-0):4)
How can it be solved?
I'm using custom editText, and receive crush on a start. It's also ruins my constraints on a design page of xml. With default one everything is ok.
Code for custom EditText: Using it to lock cursor in the end.
class CustomEditText(context: Context) : EditText(context) {
public override fun onSelectionChanged(start: Int, end: Int) {
val text = text
if (text != null) {
if (start != text.length || end != text.length) {
setSelection(text.length, text.length)
return
}
}
super.onSelectionChanged(start, end)
}
}
Code in xml:
<sh1gure.test.creditcardform.view.CustomEditText
android:id="#+id/etCardDate"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginEnd="8dp"
android:background="#drawable/edit_text_back"
android:ems="10"
android:hint="#string/mm_yy"
android:inputType="number"
android:maxLength="#integer/max_length_date"
android:paddingLeft="8dp"
android:paddingRight="8dp"
app:layout_constraintBottom_toTopOf="#+id/btn_add_card"
app:layout_constraintEnd_toStartOf="#+id/etCardCvv"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="#+id/etCardNumber1"
app:layout_constraintTop_toBottomOf="#+id/textView3" />
Logcat:
Process: sh1gure.test.creditcardform, PID: 24143
java.lang.RuntimeException: Unable to start activity ComponentInfo{sh1gure.test.creditcardform/sh1gure.test.creditcardform.view.MainActivity}: android.view.InflateException: Binary XML file line #53: Binary XML file line #53: Error inflating class sh1gure.test.creditcardform.view.CustomEditText
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2925)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3060)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1818)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6762)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: android.view.InflateException: Binary XML file line #53: Binary XML file line #53: Error inflating class sh1gure.test.creditcardform.view.CustomEditText
Caused by: android.view.InflateException: Binary XML file line #53: Error inflating class sh1gure.test.creditcardform.view.CustomEditText
For XML inflation, you must provide a constructor with an AttributeSet as second parameter:
class CustomEditText(context: Context, attrs: AttributeSet? = null) : EditText(context, attrs) {
public override fun onSelectionChanged(start: Int, end: Int) {
val text = text
if (text != null) {
if (start != text.length || end != text.length) {
setSelection(text.length, text.length)
return
}
}
super.onSelectionChanged(start, end)
}
}
I'm creating an application where it uses the Navigation Architecture, but my navigation has no default host, and can be set based on the selected destination by the user.
So this is the setup of my NavHostFragment
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var finalHost: NavHostFragment? = null
when (bundle) {
REQUEST_FIRST -> {
finalHost = NavHostFragment.create(R.navigation.navigation_first)
}
REQUEST_SECOND -> {
finalHost = NavHostFragment.create(R.navigation.navigation_second)
}
REQUEST_THIRD -> {
finalHost = NavHostFragment.create(R.navigation.navigation_third)
}
}
supportFragmentManager.beginTransaction()
.replace(R.id.fragmentMain, finalHost!!)
.setPrimaryNavigationFragment(finalHost)
.commit()
}
and this is my main activity layout.
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black">
<!-- some stuff -->
<fragment
android:id="#+id/fragmentMain"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
So, my MainActivity is loading correctly but when I start to rotate the screen of the device, It encounters some errors.
java.lang.RuntimeException: Unable to start activity ComponentInfo: android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class fragment
the said error is pointing on my setContentView(R.layout.activity_main) and the NavHostFragment <fragment> in layout.
any help is appreciated, Thanks!