Question

In Androd_Tab_view.java file, I have the following code :

tabs = (TabHost)findViewById(R.id.TabHost1); tabs.setup();

TabHost.TabSpec first_tab = tabs.newTabSpec("tag1");
first_tab.setContent(new Intent(this,FirstTab.class));
first_tab.setIndicator("Book");
tabs.addTab(first_tab);
//SecondTab.class
TabHost.TabSpec second_tab = tabs.newTabSpec("tag2");
second_tab.setContent(new Intent(this,SecondTab.class));
second_tab.setIndicator("Authors");
tabs.addTab(second_tab);

In FirstTab.java file I have the following code :

public class FirstTab extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

/* First Tab Content */
        TextView textView = new TextView(this); 
        textView.setText("First Tab");
        setContentView(textView);

    }
}

In SecondTab.java file I have the following code :

     public class SecondTab extends Activity {
/** Called when the activity is first created. */
@Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);  /* Second Tab Content */

        TextView textView = new TextView(this);
        textView.setText("Second Tab"); 
        setContentView(textView);

    }
}

But when I try to run this code I get an exception . What can be possible solution for this problem ??

Was it helpful?

Solution

You create Tab like this

   void tabCreation() {

    tabHost.setup();

    TabSpec spec1 = tabHost.newTabSpec("Accounts");
    spec1.setIndicator(createTabView(tabHost.getContext(), "Accounts", R.drawable.tab_home));
    Intent inte = new Intent(TabsActivity.this,  Activityone.class);
    inte.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    spec1.setContent(inte);
    // spec1.setContent(R.id.tab1);

    TabSpec spec2 = tabHost.newTabSpec("Contacts");
    spec2.setIndicator(createTabView(tabHost.getContext(), "Contacts", R.drawable.tab_account));
    Intent _int = new Intent(TabsActivity.this,  Activitytwo.class);
    _int.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    spec2.setContent(_int);
    // /spec2.setContent(R.id.tab2);

    TabSpec spec3 = tabHost.newTabSpec("Chats");         

    tabHost.addTab(spec1);
    tabHost.addTab(spec2);       

    tabHost.setCurrentTab(0);
}

private static View createTabView(final Context context, final String text, final int id) {

    View view = LayoutInflater.from(context).inflate(R.layout.tab_indicator, null);
    TextView tv = (TextView) view.findViewById(R.id.title);
    tv.setText(text);
    ImageView icon = (ImageView) view.findViewById(R.id.icon);
    icon.setImageResource(id);
    return view;
}
 }

and your Activities are

 public class FirstTab extends Activity {
  /** Called when the activity is first created. */
    @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_accounts); 


    /* First Tab Content */
       TextView textView = new TextView(this); 
       textView.setText("First Tab");
      setContentView(textView);

and

  public class secondTab extends Activity {
  /** Called when the activity is first created. */
    @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.contacts_accounts); 


    /* First Tab Content */
       TextView textView = new TextView(this); 
       textView.setText("First Tab");
      setContentView(textView);

    }
}

and tis http://www.androidhive.info/2011/08/android-tab-layout-tutorial/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top