user2677969
user2677969

Reputation: 1

ANDROID: Customized tab design crashes app on startup

I have bin trying to customize how my tabs look, and I don't get any errors. But when I try to run the app it crashes. This is what I got:

setContentView(R.layout.activity_main);
tabHost = (TabHost) findViewById(android.R.id.tabhost);

and:

private void setUpTabs() {
    // tabHost.setup();
    //
    // TabSpec spec1 = tabHost.newTabSpec("TAB 1");
    // spec1.setContent(R.id.tab_1);
    // spec1.setIndicator("", getResources().getDrawable(R.raw.home));
    //
    // TabSpec spec2 = tabHost.newTabSpec("TAB 2");
    // spec2.setContent(R.id.tab_2);
    // spec2.setIndicator("", getResources().getDrawable(R.raw.most));
    // TabSpec spec3 = tabHost.newTabSpec("TAB 3");
    // spec3.setContent(R.id.tab_3);
    // spec3.setIndicator("", getResources().getDrawable(R.raw.favorites));
    // TabSpec spec4 = tabHost.newTabSpec("TAB 4");
    // spec4.setContent(R.id.tab_4);
    // spec4.setIndicator("",
    // getResources().getDrawable(R.raw.search_icon));
    // tabHost.addTab(spec1);
    // tabHost.addTab(spec2);
    // tabHost.addTab(spec3);
    // tabHost.addTab(spec4);
    // tabHost.setOnTabChangedListener(this);

    setContentView(R.layout.activity_main);
    tabHost = (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(tabHost.getContext(), tag);
    TabSpec setContent = tabHost.newTabSpec(tag).setIndicator(tabview).setContent(new TabContentFactory() {
                public View createTabContent(String tag) {
                    return view;
                }
            });
    tabHost.addTab(setContent);
}

private static View createTabView(final Context context, final String text) {
    View view = LayoutInflater.from(context)
            .inflate(R.xml.tabs_bg, null);
    TextView tv = (TextView) view.findViewById(R.id.tabsText);
    tv.setText(text);
    return view;
}

The section commented out, is what I did before using the standard tab design. The .xml parts so far should have no problems, sins that it so far is based 100% on other stuff that I dug up in the net.

Upvotes: 0

Views: 189

Answers (2)

GuruPrasath S
GuruPrasath S

Reputation: 93

private void setupTab(final View view, final String tag) {

  View tabview = createTabView(tabHost.getContext(), tag);

TabSpec setContent = tabHost.newTabSpec(tag).setIndicator(tabview).setContent(new      TabContentFactory() {
            public View createTabContent(String tag) {
             final TextView tv = new TextView(this);
              tv.setText("Content for tab with tag " + tag);
               return view;
            }
        });
tabHost.addTab(setContent);

} like this..........

Upvotes: 1

GuruPrasath S
GuruPrasath S

Reputation: 93

You have to specify the view inside createTabContent{} method........the view returns null thats why its crashing.......

Upvotes: 0

Related Questions