Error inflating class fragment with class using FragmentActivity - android-layout

I have coded a Google Maps application in Xamarin, and am getting this error:
Android.Views.InflateException: Binary XML file line #1: Error
inflating class fragment
I am getting this error when the following line of code is run:
SetContentView(Resource.Layout.MapWithOverlayLayout);
Here is my MapWithOverlayLayout.axml layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment class="SimpleMapDemo.MapWithMarkersActivity"
android:id="#+id/mapWithOverlay"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
The namespace is called: SimpleMapDemo
Here is my class declaration:
public class MapWithMarkersActivity : Android.Support.V4.App.FragmentActivity
I am using a SupportMapFragment object to display a Google Map
Can I please have some help to get this code working?

I think the problem is with the class attribute you set on the fragment element. Have you tired inflating the layout without it?

I have figured it out myself. I did not have the correct class name.
Here is the correct layout code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</LinearLayout>

I had the same problem and fixed it by deleting the AndroidManifest in the obj/Debug/android directory, in the VS or XS and rewriting it so that it only works on me
That is because the app can't distinguish between google play services manifest and the app's one.

Related

Why Relativelayout doesn't work on fragments?

I have created a bottom navigation bar from this tutorial https://www.youtube.com/watch?v=JS3DIb4x1JQ&t=1352s, but RelativeLayout doesn't work properly on fragments.
Here is the code of bluetooth fragment with textview in the middle:
<?xml version="1.0" encoding="utf-8"?>
<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/bluetooth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#0472FE"
android:text="Bluetooth"
android:textSize="30dp"
android:layout_centerInParent="true"
/>
</RelativeLayout>
This is what it looks like in
design
And this is what it looks like in
android emulator
How do I fix it?
Are there other components in this view? check the main view if it set to match_parent.

App on phone looks very different to android studio rendering

I'm trying to get a fragment to appear, as rendered in android studio on my phone. I have a Samsung S7 which has 1440 x 2560 pixels, so I've selected the Nexus 6 rendering option in Android Studio.
I have setup my fragment so that it is contained in a content file called content_add_edit_recipe.xml. My activity, activity_add_edit_recipe.xml includes this file, shown below.
<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:id="#+id/fragment"
android:orientation="vertical"
android:name="com.jcg.grocerylistconverter.AddEditRecipeActivityFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:layout="#layout/fragment_add_edit_recipe"/>
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.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="com.jcg.grocerylistconverter.AddEditRecipeActivity">
here is activity file:
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.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"/>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_add_edit_recipe"/>
here is the fragment xml for the layout tag (I don't think the rest is relevant right now):
<android.support.constraint.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/content_add_edit_recipe"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:orientation="vertical"
tools:context="com.jcg.grocerylistconverter.AddEditRecipeActivityFragment"
tools:showIn="#layout/activity_add_edit_recipe">....
And here is how the fragment looks in the preview:
while here is how it appears on the phone:
I suspect that this is to do with my actual java code in starting the fragment, but I can't figure out for the life of me where/how this is occurring, after tracing through my code the xmls that are inflated.
If nothing is evident from the xmls I will post the code initiating the activity/fragment, but first can someone please point out if something appears off with my xml?
I think you didn't consider text size and length of text inside while designing layout. You should test your layout with different text length and size inside spinner

Error inflating SwipeRefreshLayout

I am trying to implement a swiperefreshlayout using the following code in my fragment,
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/recentList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/grey" >
</ListView>
</android.support.v4.widget.SwipeRefreshLayout>
Whens tarring the activity i get the error "Error inflating class android.support.v4.widget.SwipeRfreshLayout"
I have never used this control before though i am currently using other controls from v4 support library without issue.

ViewPager cannot cast from source type to destination type

In Xamarin, I am coding a ViewPager application and am getting this error:
Cannot cast from source type to destination type.
At this line of code:
ViewPager pager = (ViewPager) FindViewById(Resource.Id.viewPager);
Here is my activity_main.axml layout file with the reference to the ViewPager:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/viewPager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
May I have some help with this please?
Thanks in advance
It's incorrect for monodroid, use this one instead:
ViewPager pager = FindViewById<ViewPager>(Resource.Id.viewPager);

Having some problems trying to put views in the right places

I'm generating dynamic tablerows in this XML
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none" >
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layoutPrincipal1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="1">
</TableLayout>
</ScrollView>
The problem is: I would like to have some views in a specific position using gravity, but when a define for example, button1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT,Gravity.CENTER_VERTICAL)); it doesn't work, but if I do this directly in the xml It works(with layout_gravity), someone knows what to do?

Resources