MonoDroid:Fragment.Instantiate ClassNotFoundException

时间:2012-07-25 01:17:34

标签: android mono xamarin.android

我正在尝试使用Mono for Android中的ActionBar创建一个滑动标签视图。

eclipse插件的adroid将此作为默认项目模板,我已设法将其转换,我现在正尝试添加功能以将我的片段分开。

当我尝试运行我的项目时,我得到一个ClassNotFoundException

以下是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;
        }
    }
}

这是我的部分适配器<​​/ p>

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:无法实例化片段   MobileCompanion.AndroidOS.App.UI.Customers.HomeTab:确保上课   name存在,是public,并且有一个空的构造函数,是公共的   在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

     

在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:\项目\ MobileCompanion \树干\ MobileCompanion.Android.App \ SectionsPagerAdapter.cs:28

     

在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(包装器动态方法)   object.780b3637-f853-4d6e-8435-9c92bc6a9dd5(intptr,intptr,int)

     

---托管异常堆栈跟踪结束---

     

android.support.v4.app.Fragment $ InstantiationException:无法执行   实例化片段   MobileCompanion.AndroidOS.App.UI.Customers.HomeTab:确保上课   name存在,是公共的,并且有一个空的构造函数是公共的

  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(母语   方法)

  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)
     

引起: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)

2 个答案:

答案 0 :(得分:11)

命名空间必须全部小写才能使Fragment.Instantiate正常工作。我相信这种限制是从Java继续的。

var page = Fragment.Instantiate(_activity, typeof(T).Namespace.ToLower() + "." +  
                                typeof(T).Name); 

答案 1 :(得分:5)

更好的答案是:

var page = Fragment.Instantiate (_activity, Java.Lang.Class.FromType(T).Name);