0

I referred this on SO as the problem was similar to mine.

In my main activity, I'm calling a class TabControl which extends ActivityGroup.

Please note that I can't extend my main class from ActivityGroup as I want to use Custom_Title feature which will not be possible with ActivityGroup.

1). main class :

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

2.) TabControl class :

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>

But I'm getting following error :

 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)

As I'm absolute beginner to Mono for Android, any help appreciated...

Community
  • 1
  • 1
GAMA
  • 5,958
  • 14
  • 79
  • 126

1 Answers1

0

In your main activity, just create tab control and refer all other activities through it. Extend the main activity from TabActivity.

Krunal
  • 6,440
  • 21
  • 91
  • 155