【问题标题】:Working with Tab Control in Mono for Android在 Mono for Android 中使用 Tab 控件
【发布时间】:2012-04-03 07:28:53
【问题描述】:

我在 SO 上提到了this,因为问题与我的相似。

在我的主要活动中,我正在调用一个扩展 ActivityGroup 的类 TabControl

请注意,我无法从 ActivityGroup 扩展我的主类,因为我想使用 Custom_Title 功能,而 ActivityGroup 无法使用。

1)。主类:

public class MainActivity: Activity
{
   TabControl _tabControl;
   protected override void OnCreate (Bundle bundle)
   {
         _tabControl=new TabControl();
    _tabControl.CreateTabs();
    }
}

2.) TabControl 类:

public class TabControl:ActivityGroup
    {
        TabHost tabHost;

        protected override void OnCreate (Bundle bundle)
        {            
            base.OnCreate (bundle);
            SetContentView(Resource.Layout.TabView);
        }

    public void CreateTabs()
    {
        try
        {
             tabHost=(TabHost)FindViewById(Resource.Id.tabHost);
         LocalActivityManager mLocalActivityManager = new LocalActivityManager(this, false);
             tabHost.Setup(mLocalActivityManager);
             //tabHost.Setup();             

        TabHost.TabSpec spec;
        Intent intent; 

        intent = new Intent (this, typeof (MainActivity));
        intent.AddFlags (ActivityFlags.NewTask);

                spec=tabHost.NewTabSpec("Heat Map");
                spec.SetIndicator("Heat Map");
                spec.SetContent(intent);
                tabHost.AddTab(spec);

        intent = new Intent (this, typeof (LiveMkt));
        intent.AddFlags (ActivityFlags.NewTask);

                spec=tabHost.NewTabSpec("Live Mkt");
                spec.SetIndicator("Live Mkt");
                spec.SetContent(intent);
                tabHost.AddTab(spec);

                tabHost.CurrentTab = 1;                 
            }
            catch(Exception ex)
            {}
        }       
    }

3.) TabView.xml:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/tabLayout">

<TabHost 
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/tabHost"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
        <RelativeLayout
            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"            
                    android:layout_alignParentBottom="true" />
            <FrameLayout
                    android:id="@android:id/tabcontent"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:padding="5dp"       
                    android:layout_alignParentTop="true"/>
        </RelativeLayout>
</TabHost>  
</RelativeLayout>

但我收到以下错误:

 Module Name : TabControl

Method Name  : CreateTabs

Exception Details : Java.Lang.NullPointerException: Exception of type 'Java.Lang.NullPointerException' was thrown.

at Android.Runtime.JNIEnv.CallNonvirtualObjectMethod (IntPtr jobject, IntPtr jclass, IntPtr jmethod, Android.Runtime.JValue[] parms) [0x00000] in <filename unknown>:0 

at Android.App.Activity.FindViewById (Int32 id) [0x00000] in <filename unknown>:0 

at StockGadget.Controls.TabControl.CreateTabs () [0x00000] in <filename unknown>:0 

--- End of managed exception stack trace ---

java.lang.NullPointerException

at android.app.Activity.findViewById(Activity.java:1637)

由于我是 Mono for Android 的绝对初学者,任何帮助表示赞赏...

【问题讨论】:

    标签: android nullpointerexception xamarin.android android-tabhost


    【解决方案1】:

    在您的主要活动中,只需创建选项卡控件并通过它引用所有其他活动。 从TabActivity扩展主要活动。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-05
      • 1970-01-01
      • 1970-01-01
      • 2012-06-08
      • 1970-01-01
      • 2012-10-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多