在VS2012中的Xamarin Android中的TabView中发生了未处理的异常

时间:2013-08-26 13:07:23

标签: c# android visual-studio-2012 xamarin

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

    **This is my layout main.axml**

我在Xamarin中使用c#并且使用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 Walkthrough

事先知道了 我希望你的回复..

1 个答案:

答案 0 :(得分:0)