FragmentTabHost in VS2013 - xamarin C#
本文关键字:xamarin VS2013 in FragmentTabHost | 更新日期: 2023-09-27 18:21:14
我用来创建FragmentTabHost的代码:(我正在为android 2.2以上版本设计)我在谷歌上搜索了很多,但没有用,所以我已经把我所有的代码都放在这里了。我在中出错
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.xml
<?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>
碎片1.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的异常。在Android.Runtime.JNIEnv.CallVoidMethod(IntPtr jobject,IntPtr jmethod,Android.Runtime.JValue[]parms)
我已经做到了:更改:
mTabHost.AddTab(spec);
收件人:
mTabHost.Setup(this, SupportFragmentManager, Resource.Id.tabFrameLayout);
mTabHost.AddTab(spec, Java.Lang.Class.FromType(typeof(Fragment1)), null);
结束!
我在这里发现了同样的错误。问题就在你的片段中:
using Android.App;
更改为:
using Android.Support.V4.App;
现在没事了。。。
它浪费了我一整天。
好的,在输出窗口中显示强制类型错误。像这样:
Android.App.Fragment can't cast to Android.Support.V4.App.Fragment.