Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error inflating class fragment, Xamarin

I have an error on inflating the fragment into a layout. The IDE I am working with is Xamarin, which is a android developing tool in the programming language C#.

I get the error on the classwhich is in this directory

JeugdbewegingApp\JeugdbewegingApp\Presentation\MainMenu.cs

In the following piece of code the error is thrown by the fragment class name. This code is from the NewsFeed.axml layout file.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button
        android:id="@+id/myButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="test" />
    <TextView
        android:text="Text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView1" />
    <fragment
        class="jeugdbewegingApp/jeugdbewegingApp/presentation/MainMenu"
        android:id="@+id/menu_fragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</LinearLayout>

I am aware of the namespace that needs to be written in lowercase but does your files and directories need to be in lowercase to?

I've been stuck for quite a while now and can't find a solution, the tutorial I am using is http://docs.xamarin.com/guides/android/platform_features/fragments/part_1_-_creating_a_fragment/

The full error is this

Android.Views.InflateException: Binary XML file line #1: Error inflating class fragment at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (intptr,intptr,intptr,Android.Runtime.JValue[]) [0x00084] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.12-series/a1e3982a/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:895 at Android.App.Activity.SetContentView (int) [0x00070] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.12-series/a1e3982a/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.App.Activity.cs:4252 at JeugdbewegingApp.NewsFeed.OnCreate (Android.OS.Bundle) [0x00009] in c:\Users\Jacob\Dropbox\2ICT\2de Semester\Projecten 1\Source\Jacob\JeugdbewegingApp\JeugdbewegingApp\Presentation\NewsFeed.cs:24 at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) [0x00011] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.12-series/a1e3982a/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.App.Activity.cs:2178 at at (wrapper dynamic-method) object.08e9bb04-13ac-4397-9345-6bea3473292f (intptr,intptr,intptr) 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 android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290) at at android.app.Activity.setContentView(Activity.java:1929) at at jeugdbewegingapp.NewsFeed.n_onCreate(Native Method) at at jeugdbewegingapp.NewsFeed.onCreate(NewsFeed.java:28) at at android.app.Activity.performCreate(Activity.java:5231) at at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) at at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) at at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) at at android.app.ActivityThread.access$800(ActivityThread.java:135)
at at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at at android.os.Handler.dispatchMessage(Handler.java:102) 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: android.app.Fragment$InstantiationException: Unable to instantiate fragment jeugdbewegingapp/jeugdbewegingapp/presentation/MainMenu: make sure class name exists, is public, and has an empty constructor that is public at at android.app.Fragment.instantiate(Fragment.java:597) at at android.app.Fragment.instantiate(Fragment.java:561) at at android.app.Activity.onCreateView(Activity.java:4778) at at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689) at ... 22 more at Caused by: java.lang.ClassNotFoundException: Didn't find class "jeugdbewegingapp/jeugdbewegingapp/presentation/MainMenu" on path: DexPathList[[zip file "/data/app/JeugdbewegingApp.JeugdbewegingApp-1.apk"],nativeLibraryDirectories=[/data/app-lib/JeugdbewegingApp.JeugdbewegingApp-1, /vendor/lib, /system/lib]] at at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56) at at java.lang.ClassLoader.loadClass(ClassLoader.java:497) at at java.lang.ClassLoader.loadClass(ClassLoader.java:457) at at android.app.Fragment.instantiate(Fragment.java:583) at ... 25 more

If anyone could help me it would be much appreciated!

like image 201
Jacob Notte Avatar asked Jan 10 '23 20:01

Jacob Notte


1 Answers

The class attribute should contain the full namespace of the class, not the path. As you've suggested, the namespace must be all lowercase as Java uses lowercase namespaces. The namespace in the wrapper class generated by Xamarin for your custom class will be lowercase. The actual class name will remain unchanged, and so doesn't need to be lowercase.

So, assuming your namespace is JeugdbewegingApp.JeugdbewegingApp.Presentation.MainMenu, your fragment xml should look like this...

<fragment
    class="jeugdbewegingapp.jeugdbewegingapp.presentation.MainMenu"
    android:id="@+id/menu_fragment"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />
like image 196
Tom Bowers Avatar answered Jan 17 '23 17:01

Tom Bowers