2012-07-25 80 views
4

我想創建一個使用ActionBar在單聲道爲Android安裝的swipping選項卡視圖。MonoDroid:Fragment.Instantiate ClassNotFoundException

的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.Hom ETAB:確保類 名稱存在,是公開的,有一個空的構造函數,它是公共
在Android.Runtime.JNIEnv.CallStaticObjectMethod (IntPtr的,IntPtr的,Android.Runtime.JValue [])[0x00024]在 /用戶/建設者/數據/通道/ 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 /兼容性-V4 /綁定/ OBJ /平臺-14/SRC /生成/ Android.Support.V4.App.Fragment.cs:624

在MobileCompanion.AndroidOS.App.SectionsPagerAdapter.GetItem(INT) [0x00007]在 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/ (wrapper dynamic-method) object.780b3637-f853-4d6 E-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) 

回答

11

命名空間必須全部小寫Fragment.Instantiate工作。我相信這個限制是從Java繼承的。

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

命名空間必須是小寫字母,但類名本身需要在正常的情況下,駱駝。例如:var page = Fragment.Instantiate(_activity,typeof(T).Namespace.ToLower()+「。」+ typeof(T).Name); – joates 2012-09-06 22:34:16

+0

不要再使用這種方法。相反,使用:http://stackoverflow.com/a/17798431/10957 – 2015-05-22 07:46:51

5

更好的答案是:

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

我最近剛剛升級了Xamarin,並突然在已經工作的代碼下面的外殼解決方案停止工作。我用這個替換了這個代碼,而且我很棒! – 2015-05-05 22:08:25

+0

這絕對是最新的最佳答案,因爲最新的Xamarin使用新的名稱爲android安裝。 「」Android名稱修改方案Callable Wrappers「已被修改。這意味着爲C#類生成的Java類型名稱是不同的 – 2015-05-22 07:45:33