0

Mainactivity:

 public class MainActivity extends AppCompatActivity {
    TabHost mTabHost;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mTabHost = (TabHost) findViewById(android.R.id.tabhost);
            setupTab(new TextView(this), "Tab 1");
            setupTab(new TextView(this), "Tab 2");
            setupTab(new TextView(this), "Tab 3");

        }
        private void setupTab(final View view, final String tag) {
            View tabview = createTabView(mTabHost.getContext(), tag);
            TabHost.TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(new TabHost.TabContentFactory() {

                public View createTabContent(String tag) {return view;}
            });
            mTabHost.addTab(setContent);
        }
        private static View createTabView(final Context context, final String text) {
            View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
            TextView tv = (TextView) view.findViewById(R.id.tabsText);
            tv.setText(text);
            return view;
        }

    }

Activity_main

<?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">

<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">

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

I am experimenting with tabs in order to make a bottom navigation bar without material design library. How to fix this error? (Attempt to invoke virtual method 'void android.widget.TabWidget.setStripEnabled(boolean)' on a null object reference)

1 Answer 1

3

You need invoke mTabHost.setup(...);

The reason is your activity does not extends TabActivity, you should invoke TabHost.setup(...) before TabHost.addTab(...) method.

image

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.