0

I am trying to add Tab control inside an activity which has action bar as well. Here is the reference code

MessageViewActivity.java

public class MessageViewActivity extends ActionBarActivity {
  TabHost messageViewTabHost;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_message_view);

    messageViewTabHost = (TabHost)findViewById(R.id.messageViewTabHost);

    TabHost.TabSpec spec;
    Intent intent;

    intent = new Intent().setClass(this, MessageCategoryTab.class);
    spec = messageViewTabHost.newTabSpec("Category").setIndicator("Category").setContent(intent);

    messageViewTabHost.addTab(spec);

activity_message_view.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MessageViewActivity">


<TabHost
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/messageViewTabHost"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"></TabWidget>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent">


        </FrameLayout>
    </LinearLayout>
</TabHost>

MessageCategoryTab.java

public class MessageCategoryTab extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_message_category_tab);
}
}

When I run above code, The error that is getting displayed as following

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.carbonrider.texto/com.carbonrider.texto.MessageViewActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TabWidget.addView(android.view.View)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
        at android.app.ActivityThread.access$800(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5257)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TabWidget.addView(android.view.View)' on a null object reference
        at android.widget.TabHost.addTab(TabHost.java:243)
        at com.xyz.MessageViewActivity.onCreate(MessageViewActivity.java:31)

I am totally clueless as what is happening. I have seen other posts indicating to use fragments etc. but I need to know what is the problem with the current code.

CuriousMind
  • 3,143
  • 3
  • 29
  • 54
  • Watch this [tutorial](https://www.youtube.com/watch?v=m1AeMJux0Zo) and compare with your code to identify what you have done wrong. – JanithaR Aug 12 '15 at 07:24
  • Seriously? Did you even look at the question and the video that you have posted? what is the resemblance? – CuriousMind Aug 12 '15 at 13:05
  • Did you??? You have not properly setup your TabHost and that's why you get the NullPointerException. The video shows how to properly set up a TabHost. With little time I had linking you to the video was better than me typing a whole bunch of text. – JanithaR Aug 13 '15 at 03:44
  • I did see the video. The video shows how the contents are added directly to tabs definition in XML file itself (Do check what I am trying to achieve). The `setup` method doesn't help either. – CuriousMind Aug 13 '15 at 15:05

1 Answers1

0

I know you said try setup method, but try below code after define your tab host:(

messageViewTabHost = (TabHost)findViewById(R.id.messageViewTabHost); )
LocalActivityManager mLocalActivityManager = new calActivityManager(this, false);
mLocalActivityManager.dispatchCreate(savedInstanceState);
messageViewTabHost.setup(mLocalActivityManager);

its works for me!

CuriousMind
  • 3,143
  • 3
  • 29
  • 54
shirin niki
  • 323
  • 1
  • 3