Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MonoDroid: Fragment.Instantiate ClassNotFoundException

I am trying to create a swipping tab view using the ActionBar in Mono for Android.

The adroid for eclipse plugin has this as a default project template and I have manage to convert it over, I am now trying to add the functionally to separate my fragments out.

When I try to run my project I get a ClassNotFoundException

Here is the code for the Fragment

using Fragment = Android.Support.V4.App.Fragment;

namespace MobileCompanion.AndroidOS.App.UI.Customers
{
    public  class HomeTab : Fragment
    {
        public override Android.Views.View OnCreateView(Android.Views.LayoutInflater inflater, Android.Views.ViewGroup viewGroup, Android.OS.Bundle p2)
        {

            return null;
        }
    }
}

and here is my section adapter

using System.Collections.Generic;
using Android.Content;
using Android.OS;
using Android.Support.V4.App;
using Java.Lang;

namespace MobileCompanion.AndroidOS.App
{
    public class SectionsPagerAdapter : FragmentPagerAdapter
    {
        private readonly Context _context;
        public readonly IList<TabInfo> Tabs = new List<TabInfo>();
        private Bundle _bundle;

        public SectionsPagerAdapter(FragmentManager fm, Context context, Bundle savedInstanceState)
            : base(fm)
        {
            this._context = context;
            this._bundle = savedInstanceState;

        }

        public override Fragment GetItem(int i)
        {
            var args = new Bundle();
            var page = Fragment.Instantiate(this._context, typeof(HomeTab).FullName);
            //          var page = new HomeTab(); <<--- Work but nothing displayed as none of the create methods are run
            page.Arguments = args;
            return page;
        }

        public override int Count
        {
            get { return this.Tabs.Count; }
        }

        public override ICharSequence GetPageTitleFormatted(int p0)
        {
            return new String(this.Tabs[p0].PageTitle);
        }

    }
}

Java.Lang.RuntimeException: Unable to instantiate fragment MobileCompanion.AndroidOS.App.UI.Customers.HomeTab: make sure class name exists, is public, and has an empty constructor that is public
at Android.Runtime.JNIEnv.CallStaticObjectMethod (intptr,intptr,Android.Runtime.JValue[]) [0x00024] in /Users/builder/data/lanes/monodroid-mac-monodroid-4.2.4-branch/9f7cbd60/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:767

at Android.Support.V4.App.Fragment.Instantiate (Android.Content.Context,string) [0x00034] in /Users/builder/data/lanes/monodroid-mac-monodroid-4.2.4-branch/9f7cbd60/source/monodroid-bindings/Compatibility-v4/bindings/obj/platform-14/src/generated/Android.Support.V4.App.Fragment.cs:624

at MobileCompanion.AndroidOS.App.SectionsPagerAdapter.GetItem (int) [0x00007] in c:\Projects\MobileCompanion\trunk\MobileCompanion.Android.App\SectionsPagerAdapter.cs:28

at Android.Support.V4.App.FragmentPagerAdapter.n_GetItem_I (intptr,intptr,int) [0x00008] in /Users/builder/data/lanes/monodroid-mac-monodroid-4.2.4-branch/9f7cbd60/source/monodroid-bindings/Compatibility-v4/bindings/obj/platform-14/src/generated/Android.Support.V4.App.FragmentPagerAdapter.cs:56

at (wrapper dynamic-method) object.780b3637-f853-4d6e-8435-9c92bc6a9dd5 (intptr,intptr,int)

--- End of managed exception stack trace ---

android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment MobileCompanion.AndroidOS.App.UI.Customers.HomeTab: make sure class name exists, is public, and has an empty constructor that is public

  at android.support.v4.app.Fragment.instantiate(Fragment.java:395)

  at android.support.v4.app.Fragment.instantiate(Fragment.java:363)

  at

mobilecompanion.androidos.app.SectionsPagerAdapter.n_getItem(Native Method)

  at

mobilecompanion.androidos.app.SectionsPagerAdapter.getItem(SectionsPagerAdapter.java:30)

  at

android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:97)

  at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:692)

  at android.support.v4.view.ViewPager.populate(ViewPager.java:821)

  at android.support.v4.view.ViewPager.populate(ViewPager.java:772)

  at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1234)

  at android.view.View.measure(View.java:12723)

  at

android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)

  at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)

  at android.view.View.measure(View.java:12723)

  at

android.widget.LinearLayout.measureVertical(LinearLayout.java:812)

  at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)

  at android.view.View.measure(View.java:12723)

  at

android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)

  at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)

  at

com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2092)

  at android.view.View.measure(View.java:12723)

  at

android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064)

  at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)

  at android.os.Handler.dispatchMessage(Handler.java:99)

  at android.os.Looper.loop(Looper.java:137)

  at android.app.ActivityThread.main(ActivityThread.java:4424)

  at java.lang.reflect.Method.invokeNative(Native Method)

  at java.lang.reflect.Method.invoke(Method.java:511)

  at

com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)

  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)

  at dalvik.system.NativeStart.main(Native Method)

Caused by: java.lang.ClassNotFoundException: MobileCompanion.AndroidOS.App.UI.Customers.HomeTab

  at

dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)

  at java.lang.ClassLoader.loadClass(ClassLoader.java:501)

  at java.lang.ClassLoader.loadClass(ClassLoader.java:461)

  at android.support.v4.app.Fragment.instantiate(Fragment.java:385)
like image 353
TheRealTy Avatar asked Dec 05 '22 15:12

TheRealTy


1 Answers

The namespace must be all lowercase for Fragment.Instantiate to work. I believe this limitation is carried over from Java.

var page = Fragment.Instantiate(_activity, typeof(T).Namespace.ToLower() + "." +  
                                typeof(T).Name); 
like image 80
Jeremy Avatar answered Jan 12 '23 10:01

Jeremy