Error Inflating Class Fragment when changing fragments with Shinobicharts in Xamarin


#1

Hi, I am using Xamarin.Android to create an app. I am using Xamarin.Android and have implemented Shinobicharts. The chart is in a fragment and the XML code looks like that:

   <?xml version=“1.0” encoding=“utf-8”?>
    <LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android
    android:orientation=“vertical”
    android:minWidth=“25px”
    android:minHeight=“25px”
    android:layout_width=“match_parent”
    android:layout_height=“500dp”
    android:id="@+id/linearLayout2"
    android:background="#ffffffff">
    <LinearLayout
        android:orientation=“vertical”
        android:minWidth=“25px”
        android:minHeight=“25px”
        android:layout_width=“match_parent”
        android:layout_height=“wrap_content”
        android:id="@+id/linearLayout1">
        <TableLayout
            android:minWidth=“25px”
            android:minHeight=“25px”
            android:layout_width=“match_parent”
            android:layout_height=“wrap_content”
            android:id="@+id/tableLayout1"
            android:stretchColumns=“0,1”>
            <TableRow
                android:minWidth=“25px”
                android:minHeight=“25px”
                android:id="@+id/tableRow1">
                <RelativeLayout
                    android:minWidth=“25px”
                    android:minHeight=“25px”
                    android:id="@+id/relativeLayout1">
                    <ImageButton
                        android:src="@drawable/ic_action_refresh_default"
                        android:layout_width=“48dp”
                        android:layout_height=“48dp”
                        android:id="@+id/imageButton1"
                        android:layout_alignParentRight=“true”
                        android:background="#ffffffff" />
                </RelativeLayout>
            </TableRow>
        </TableLayout>
    </LinearLayout>
    <fragment
        class=“com.shinobicontrols.charts.ChartFragment”
        android:id="@+id/chart"
        android:layout_width=“match_parent”
        android:layout_height=“match_parent” />
    </LinearLayout>
  
My fragment is inheriting the Fragment class and not the activity:

        public class AnsprechpartnerFragment : Fragment

When I run the app and the fragment is loaded the first time, it works fine. But when I change the fragment  and return back to the fragment containing the Shinobicharts , I get the following error: 
Binary XML file line #1: Error inflating class fragment   
The error raises in the following line: 

    public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            if (layoutId == null || layoutId == 0)
                layoutId = UI.Resource.Layout.ChartFragment;
            var view = inflater.Inflate (layoutId, container, false);
            return view;
        }

I tried it on Genymotion with API 17 and on a real Nexus 10 device with Android 4.4 , but I get the same error again.
I check the manifest file and tried the following versions, but none of them fixed the error: 

    <?xml version=“1.0” encoding=“Windows-1252”?>
    <manifest xmlns:android=“http://schemas.android.com/apk/res/android” android:versionCode=“1” android:versionName=“1.0” package=“com.shinobicontrols.charts.samples.annotations”>
    <uses-sdk android:minSdkVersion=“17” />
    <application android:label=“Annotations” android:hardwareAccelerated=“true”></application>
    </manifest>

    <?xml version=“1.0” encoding=“utf-8”?>
    <manifest xmlns:android=“http://schemas.android.com/apk/res/android” android:installLocation=“auto” package=“dk.ostebaronen.drawersample” android:versionCode=“1” android:versionName=“1.0”>
    <uses-sdk android:targetSdkVersion=“19” android:minSdkVersion=“8” />
    <application android:label=“Drawer Sample”></application>
    </manifest>

Can anybody help me? I googled a lot and couldn’t find anything solving this issue. 

The full error is this:

  

        Android.Views.InflateException: Binary XML file line #1: Error inflating class fragment
      at Android.Runtime.JNIEnv.CallObjectMethod (intptr,intptr,Android.Runtime.JValue[]) [0x00064] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.12-series/7f0e3d3c/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:194
      at Android.Views.LayoutInflater.Inflate (int,Android.Views.ViewGroup,bool) [0x00043] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.12-series/7f0e3d3c/source/monodroid/src/Mono.Android/platforms/android-14/src/generated/Android.Views.LayoutInflater.cs:609
      at UI.ChartFragment.OnCreateView (Android.Views.LayoutInflater,Android.Views.ViewGroup,Android.OS.Bundle) [0x0001c] in d:\Xamarin University\DrawerSample\UI\MainFragment.cs:275
      at Android.App.Fragment.n_OnCreateView_Landroid_view_LayoutInflater_Landroid_view_ViewGroup_Landroid_os_Bundle_ (intptr,intptr,intptr,intptr,intptr) [0x00022] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.12-series/7f0e3d3c/source/monodroid/src/Mono.Android/platforms/android-14/src/generated/Android.App.Fragment.cs:943
      at at (wrapper dynamic-method) object.f9c9d4ea-e23d-4651-94cd-0e470a3f81c2 (intptr,intptr,intptr,intptr,intptr) <IL 0x00023, 0x0005f>
      at — End of managed exception stack trace —
      at android.view.InflateException: Binary XML file line #1: Error inflating class fragment
      at at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
      at at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
      at at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
      at at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
      at at ui.ChartFragment.n_onCreateView(Native Method)
      at at ui.ChartFragment.onCreateView(ChartFragment.java:38)
      at at android.app.Fragment.performCreateView(Fragment.java:1700)
      at at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
      at at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
      at at android.app.BackStackRecord.run(BackStackRecord.java:684)
      at at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
      at at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
      at at android.os.Handler.handleCallback(Handler.java:733)
      at at android.os.Handler.dispatchMessage(Handler.java:95)
      at at android.os.Looper.loop(Looper.java:136)
      at at android.app.ActivityThread.main(ActivityThread.java:5017)
      at at java.lang.reflect.Method.invokeNative(Native Method)
      at at java.lang.reflect.Method.invoke(Method.java:515)
      at at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
      at at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
      at at dalvik.system.NativeStart.main(Native Method)
      at Caused by: java.lang.IllegalArgumentException: Binary XML file line #1: Duplicate id 0x7f0c0026, tag null, or parent id 0x7f0c001f with another fragment for com.shinobicontrols.charts.ChartFragment
      at at android.app.Activity.onCreateView(Activity.java:4791)
      at at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
      at … 20 more


#2

Hi mbProg,

At the end of the full stack trace I noticed:

at Caused by: java.lang.IllegalArgumentException: Binary XML file line #1: Duplicate id 0x7f0c0026, tag null, or parent id 0x7f0c001f with another fragment for com.shinobicontrols.charts.ChartFragment

Might be worth checking you’ve not got a duplicated id anywhere in your XML? This kind of thing tends to catch me out when I’m happily copying and pasting!  :wink:

Kind regards,

Patrick


#3

Thanks for the reply, I have checked the XML, but there wasn’t any duplicated ID. To ensure I have also changed the IDs, all of them, But the bug is still there.


#4

Can somebody help me? Some developers say that this is a bug from Shinobi Charts. Is that right? If you want I can offer a sample, too


#5

Hi mbProg,

If you can please send a sample project to info@shinobicontrols.com we can take a look at it and determine whether it’s a ShinobiCharts bug or not.

Kind regards,

Patrick


#6

I have sent a sample yesterday. The people from Xamarin told me again, that there must be a bug in Shinobi. Do you agree with that?

With warm regards


#7

Hi mbProg,

Thanks for sending over your sample. I’ve taken a quick look at it and it’s neither a bug in Xamarin nor in ShinobiCharts.

The reason why you’re getting the crash is because you’ve statically nested a (Shinobi) ChartFragment within your own Fragment (also called ChartFragment). On each button click you’re creating a new Fragment but as the Activity is not being destroyed neither are the existing Fragments. In your ChartFragment you statically declare a Shinobi ChartFragment with an id of chart1. So when you create a new (your) ChartFragment, by clicking back on the Charts button, the app is trying to create a new Shinobi ChartFragment with the id of chart1 but this already exists, hence the error message.

Firstly, while support for nested Fragments was introduced in Android 4.2, a ChartFragment (at least currently) can’t really be nested inside another Fragment - you may end up with some unpredictable behaviour.

In your situation I would recommend using a ChartView instead of a (Shinobi) ChartFragment. The only extra work you’ll need to do is forward on your ChartFragment’s lifecycle calls to the ChartView. So for example your ChartFragment will need to override the OnPause method as follows:

public override void OnPause()
{
    base.OnPause();
    Com.ShinobiControls.Charts.ChartView chartView = (Com.ShinobiControls.Charts.ChartView)View.FindViewById(Resource.Id.chart1);
    chartView.OnPause();
}

and similarly for OnResume and OnDestroy (in this situation OnCreate isn’t applicable as the ChartView isn’t created until OnCreateView). One thing of note is unlike (Shinobi) ChartFragments, ChartViews aren’t retained over configuration changes (e.g. rotating the device) which may affect the way you handle loading your data and saving the state of your chart, though on the face of it I’m not sure it will effect your app too much anyway.

Kind regards,

Patrick


#8

I have found a super solution for this issue due to http://stackoverflow.com/questions/14083950/duplicate-id-tag-null-or-parent-id-with-another-fragment-for-com-google-androi:

Just change the the OnCreateView of the fragment:

        private static View view;

        public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {

            if (view != null)
            {
                ViewGroup parent = (ViewGroup)view.Parent;
                if(parent != null)
                {
                    parent.RemoveView (view);
                }
            }

            try
            {
                if (layoutId == null || layoutId == 0)
                    layoutId = UI.Resource.Layout.FrgChart;
                view = inflater.Inflate (layoutId,container,false);
            }
            catch(InflateException e)
            {

            }
            return view;

        }

And it works fine. :laughing:


#9

Hi mbProg,

Glad to hear it!

Kind regards,

Patrick