4

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

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

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

这是片段的代码

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;
        }
    }
}

这是我的部分适配器

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:确保类名存在,是公共的,并且
在 Android.Runtime.JNIEnv.CallStaticObjectMethod (intptr, intptr,Android.Runtime.JValue[]) [0x00024] 在 /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] 在 /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

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

在 Android.Support.V4.App.FragmentPagerAdapter.n_GetItem_I (intptr,intptr,int) [0x00008] 在 /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

在(包装动态方法)object.780b3637-f853-4d6e-8435-9c92bc6a9dd5 (intptr,intptr,int)

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

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

  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)
4

2 回答 2

11

命名空间必须全部小写Fragment.Instantiate才能工作。我相信这个限制是从 Java 继承下来的。

var page = Fragment.Instantiate(_activity, typeof(T).Namespace.ToLower() + "." +  
                                typeof(T).Name); 
于 2012-07-26T02:58:04.543 回答
5

更好的答案是:

var page = Fragment.Instantiate (_activity, Java.Lang.Class.FromType(T).Name);
于 2013-07-22T22:19:15.607 回答