2014-09-08 2 views
-1

FragmentTabHost를 만드는 데 사용한 코드 : (안드로이드 2.2에서 위쪽으로 설계) 나는 봤지만 많이 사용하지는 않았다. 나는 내 코드를 모두 여기에 넣었다. 오류가 발생합니다.FragmentTabHost in VS2013 - xamarin C#

mTabHost.AddTab(spec); 

어떻게 해결할 수 있습니까?

활동 2.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 


using Android.App; 
using Android.Content; 
using Android.OS; 
using Android.Runtime; 
using Android.Views; 
using Android.Widget; 
using Android.Content.PM; 
using Android.Support.V4.App; 


namespace ANRestaurant.Forms 
{ 
[Activity(Label = "ANRestaurant", MainLauncher = true, Icon = "@drawable/icon", ScreenOrientation = ScreenOrientation.Portrait)] 


public class Activity2 : FragmentActivity 
{ 
    FragmentTabHost mTabHost; 
    protected override void OnCreate(Bundle bundle) 
    { 
     base.OnCreate(bundle); 


     SetContentView(Resource.Layout.activity_main); 
     try 
     { 
      mTabHost = FindViewById<FragmentTabHost>(Resource.Id.tabhostN ); 
      Intent intent; 
      intent = new Intent(this, typeof(Fragment1)); 
      intent.AddFlags(ActivityFlags.NewTask); 
      //mTabHost.Setup(this, SupportFragmentManager, Resource.Id.tabFrameLayout); 


      TabHost.TabSpec spec; 
      spec = mTabHost.NewTabSpec("artists"); 
      spec.SetIndicator("Artists", Resources.GetDrawable(Resource.Drawable.tab_icon1) ); 
      spec.SetContent(intent); 
      mTabHost.AddTab(spec); ======>Here is the error 
      //mTabHost.AddTab(
      //  mTabHost.NewTabSpec("tab2").SetIndicator("Tab 2", null), 
      //  intent, null); 
      //mTabHost.addTab(
      //  mTabHost.newTabSpec("tab2").setIndicator("Tab 2", null), 
      //  FragmentTab.class, null); 
      //mTabHost.addTab(
      //  mTabHost.newTabSpec("tab3").setIndicator("Tab 3", null), 
      //  FragmentTab.class, null); 
     } 
     catch (Exception ex) 
     { 


     } 
    } 
} 
} 

activity_main.axml

<?xml version="1.0" encoding="utf-8"?> <android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tabhostN" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TabWidget android:id="@+id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0" android:orientation="horizontal" /> <FrameLayout android:id="@+id/tabFrameLayout" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> </LinearLayout> </android.support.v4.app.FragmentTabHost>

Fragment1.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 


using Android.App; 
using Android.Content; 
using Android.OS; 
using Android.Runtime; 
using Android.Util; 
using Android.Views; 
using Android.Widget; 


namespace ANRestaurant.Forms 
{ 
public class Fragment1 : Fragment 
{ 



    public override void OnCreate(Bundle savedInstanceState) 
    { 
     base.OnCreate(savedInstanceState); 


     // Create your fragment here 
    } 
    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    { 
     View view = inflater.Inflate(Resource.Layout.fragment1, container, false); 
     return view; 
     //return base.OnCreateView(inflater, container, savedInstanceState); 
    } 
} 
} 

오류는 다음과 같습니다 java.lang.NullPointerException이 : 유형의 예외 Java.Lang.NullPointerException이 (가) 번째였습니다. rown. 변화 : Android.Runtime.JNIEnv.CallVoidMethod에서 (을 IntPtr하는 jobject, jmethod을 IntPtr, Android.Runtime.JValue []는 PARMS)

+0

"오류가 발생했습니다"는 오류 *가 무엇인지 알려주지 않습니다. 자세한 내용을 알려주십시오. –

+0

전체 스택 트레이스를 게시 하시겠습니까? – Frank

답변

1

I에 의해 행한 것으로

mTabHost.AddTab(spec);

행 :

mTabHost.Setup(this, SupportFragmentManager, Resource.Id.tabFrameLayout); mTabHost.AddTab(spec, Java.Lang.Class.FromType(typeof(Fragment1)), null);

끝!

0

동일한 오류가 발생하여 여기에서 발견되었습니다. 문제는 당신의 조각에 여기에 있습니다 :

using Android.App; 

변경 그것을 :

using Android.Support.V4.App; 

그것은 확인 지금 ...

그것은 나에게 낭비 하루 종일입니다.

출력 창에서 전송 유형 오류가 표시됩니다. like :

Android.App.Fragment can't cast to Android.Support.V4.App.Fragment.