-1
    **An unhandled exception occurred in Tab View in Xamarin Android in VS2012**

    **This is my layout main.axml**

我不知道使用 c# 在 Xamarin 中实现选项卡视图并使用 vs2012 所以请帮助我这个错误发生在我在 Visual Studio 警报框中的 android 模拟器中运行我的应用程序时未处理的异常

    <?xml version="1.0" encoding="utf-8"?>
    <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5dp">
            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:padding="5dp" />
        </LinearLayout>
    </TabHost>

这是我的 MainActiviy Activity1.cs

    using System;

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

    namespace TabView 
    {
        [Activity(Label = "TabView", MainLauncher = true, Icon = "@drawable/icon")]
        public class Activity1 : TabActivity
        {


            protected override void OnCreate(Bundle bundle)
            {
                base.OnCreate(bundle);

                // Set our view from the "main" layout resource
                SetContentView(Resource.Layout.Main);
                CreateTab(typeof(WhatsOnActivity), "whats_on", "What's On", Resource.Drawable.ic_tab_whats_on);
                CreateTab(typeof(SpeakersActivity), "speakers", "Speakers", Resource.Drawable.ic_tab_speakers);
                CreateTab(typeof(SessionsActivity), "sessions", "Sessions", Resource.Drawable.ic_tab_sessions);
                CreateTab(typeof(MyScheduleActivity), "my_schedule", "My Schedule", Resource.Drawable.ic_tab_my_schedule);

            }
            private void CreateTab(Type activityType, string tag, string label, int drawableId)
            {
                var intent = new Intent(this, activityType);
                intent.AddFlags(ActivityFlags.NewTask);

                var spec = TabHost.NewTabSpec(tag);
                var drawableIcon = Resources.GetDrawable(drawableId);
                spec.SetIndicator(label, drawableIcon);
                spec.SetContent(intent);

                TabHost.AddTab(spec);
            }


            }
        }

我想在我的应用程序中添加四个标签

    **this 1st tab activity MyScheduleActivity.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;

    namespace TabView
    {
        [Activity]
        public class MyScheduleActivity : Activity
        {
            protected override void OnCreate(Bundle bundle)
            {
                base.OnCreate(bundle);

                TextView textview = new TextView(this);
                textview.Text = "This is the My Schedule tab";
                SetContentView(textview);
            }
        }
    }


    **this is 2nd activity SessionsActivity.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;

    namespace TabView
    {
        [Activity]
        public class SessionsActivity : Activity
        {
            protected override void OnCreate(Bundle bundle)
            {
                base.OnCreate(bundle);

                TextView textview = new TextView(this);
                textview.Text = "This is the My Session tab";
                SetContentView(textview);
            }
        }
    }


    **this is 3rd activity SpeakersActivity.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;

    namespace TabView
    {
        [Activity]
        public class SpeakersActivity : Activity
        {
            protected override void OnCreate(Bundle bundle)
            {
                base.OnCreate(bundle);

                TextView textview = new TextView(this);
                textview.Text = "This is the My Speakers tab";
                SetContentView(textview);
            }
        }
    }

**this is 4th activity WhatsOnActivity.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;

namespace TabView
{
    [Activity]
    public class WhatsOnActivity : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            TextView textview = new TextView(this);
            textview.Text = "This is the My Whats On tab";
            SetContentView(textview);
        }
    }
}


**AND**

**this is my xml in  Resources=>drawable folder** 

1) ic_tab_my_schedule.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@drawable/ic_tab_my_schedule" android:state_selected="true"/>
  <item android:drawable="@drawable/ic_tab_my_schedule"/>
</selector>


2)ic_tab_sessions.xml
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@drawable/ic_tab_sessions"
        android:state_selected="true"/>
  <item android:drawable="@drawable/ic_tab_sessions"/>
</selector>

3)ic_tab_speakers.xml
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@drawable/ic_tab_speakers"
        android:state_selected="true"/>
  <item android:drawable="@drawable/ic_tab_speakers"/>
</selector>

4)ic_tab_whats_on.xml
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@drawable/ic_tab_whats_on"
        android:state_selected="true"/>
  <item android:drawable="@drawable/ic_tab_whats_on"/>
</selector>

我在可绘制文件夹选择状态和未选择状态中添加了两个图像!

我按照这个链接:- TabHost演练

提前谢谢我希望你的回应..

4

1 回答 1

0

也许你应该从这个工作样本开始

https://github.com/xamarin/monodroid-samples/blob/master/ApiDemo/Tutorials/TabLayoutTutorial.cs

于 2013-08-26T14:54:48.393 回答