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?
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.
How to create auto image slider with the help of com.smarteist.autoimageslider.SliderLayout in android studio
I was trying to create a autoimage slider with the help of com.smarteist.autoimageslider.SliderLayout
under the fragment but when i run the the app its show some errors
Errors
Process: com.example.mycollege, PID: 23523
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mycollege/com.example.mycollege.MainActivity}: android.view.InflateException: Binary XML file line #18: Binary XML file line #18: Error inflating class com.smarteist.autoimageslider.SliderLayout
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2887)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2965)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1669)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:192)
at android.app.ActivityThread.main(ActivityThread.java:6702)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:826)
Caused by: android.view.InflateException: Binary XML file line #18: Binary XML file line #18: Error inflating class com.smarteist.autoimageslider.SliderLayout
Caused by: android.view.InflateException: Binary XML file line #18: Error inflating class com.smarteist.autoimageslider.SliderLayout
Caused by: java.lang.reflect.InvocationTargetException
fragement_home.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"
tools:context=".ui.home.homeFragment">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.smarteist.autoimageslider.SliderLayout
android:layout_width="match_parent"
android:layout_height="250dp"
android:id="#+id/slider_image"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/black"
android:text="HOME"/>
</ScrollView>
</FrameLayout>
homefragement.java
public class homeFragment extends Fragment {
private SliderLayout sliderLayout;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
sliderLayout = view.findViewById(R.id.slider_image);
sliderLayout.setIndicatorAnimation(IndicatorAnimations.FILL);
sliderLayout.setSliderTransformAnimation(SliderAnimations.SIMPLETRANSFORMATION);
sliderLayout.setScrollTimeInSec(1);
setsliderview();
return view;
}
private void setsliderview() {
for (int i = 0; i < 5; i++) {
DefaultSliderView defaultSliderView = new DefaultSliderView(getContext());
switch (i) {
case 0:
defaultSliderView.setImageUrl("Imageurl");
break;
case 1:
defaultSliderView.setImageUrl("Imageurl");
break;
case 2:
defaultSliderView.setImageUrl("Imageurl");
break;
case 3:
defaultSliderView.setImageUrl("Imageurl");
break;
case 4:
defaultSliderView.setImageUrl("Imageurl");
break;
}
defaultSliderView.setImageScaleType(ImageView.ScaleType.CENTER_CROP);
sliderLayout.addSliderView(defaultSliderView);
}
}
}
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!
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.
I was changing a Activity to a Fragment to use inside a Scrollable Tab Activity. But I getting this exception I load this fragment:
FATAL EXCEPTION: main
android.view.InflateException: Binary XML file line #17: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at com.zhensydow.demo.MainMenuFragment.onCreateView(MainMenuFragment.java:43)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
....
And the used (simplified) xml loaded is:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
... >
<fragment <<---- LINE #17
android:id="#+id/mlist"
android:name="com.zhensydow.demo.MListFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="#android:layout/mcontent" />
<FrameLayout ... />
</LinearLayout>
The error is caused in this code:
public class MainMenuFragment extends Fragment implements
MenuListFragment.Callbacks {
// ...
#Override
public View onCreateView( LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
super.onCreateView(inflater, container, savedInstanceState);
LinearLayout ll = (LinearLayout) inflater.inflate(
R.layout.activity_main_menu, container, false);
return ll;
}
// ...
}
The error is on a fragment, I think it's caused because the old activity loaded two fragments inside him.
How can I solve it?
UPDATE: added full fragmen xml data
I suspect your Fragment contains some code like this one, requiring your Activity to implement a callback interface, and your Activity does not implement that interface.
A fragment tag is required to have both a name and an id.