Search code examples
androidandroid-intentandroid-activitytabactivityandroid-tabactivity

Open a TabActivity with "simple" Activity


I have a problem when I try to open a TabActivity with an intent from an Activity.

The code of my activity (ConnexionActivity), which is NOT an activity from a tab :

        buttonConnexion.setOnClickListener(new OnClickListener(){
            public void onClick(View v) {
                     Intent intent = new Intent(ConnexionActivity.this, NeurokiffMobileActivity.class);
                     startActivity(intent);
            }           
        });

and the TabActivity (NeurokiffMobileActivity) :

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    this.getIntent();

    /* ******** Gestion Onglets ******** */
    res = getResources(); // Resource object to get Drawables
    tabHost = (TabHost) findViewById(android.R.id.tabhost);  // The activity TabHost


    /* *** Onglet "Evènements" *** */
    /* Intent responsable de lancer l'activité depuis l'onglet */
    intent = new Intent().setClass(this, EvenementActivity.class);
    bundleEnvoye.putString("id_user", idUser);
    intent.putExtras(bundleEnvoye);
    /* Crée un TabSpec et l'ajoute au TabHost */
    spec = tabHost.newTabSpec("evenement").setIndicator("Evenements",
                      res.getDrawable(R.drawable.ic_tab_evenement))
                   .setContent(intent);
    tabHost.addTab(spec);

    /* *** Onglet "Favoris" *** */
    intent = new Intent().setClass(this, FavorisActivity.class);
    bundleEnvoye.putString("id_user", idUser);
    intent.putExtras(bundleEnvoye);
    spec = tabHost.newTabSpec("favoris").setIndicator("Favoris",
                      res.getDrawable(R.drawable.ic_tab_favoris))
                  .setContent(intent);
    tabHost.addTab(spec);

    /* *** Onglet "Kiffs" *** */
    intent = new Intent().setClass(this, KiffsActivity.class);
    bundleEnvoye.putString("id_user", idUser);
    intent.putExtras(bundleEnvoye);
    spec = tabHost.newTabSpec("kiffs").setIndicator("Kiffs",
                      res.getDrawable(R.drawable.ic_tab_kiffs))
                  .setContent(intent);
    tabHost.addTab(spec);

    tabHost.setCurrentTab(0);

}

and the .xml of this TabActivity :

<?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"
        android:padding="5dp">
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:padding="5dp" />
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:layout_weight="0"/>
    </LinearLayout>
</TabHost>

The error is a NullPointerException on the onCreate method of NeurokiffMobileActivity, and the application close. When I put another "simple" Activity instead of NeurokiffMobileActivity in the intent, it works.

Can someone help me, please ? It seems to be a problem due to TabActivity, but I don't know which...

Thanks in advance ! ;)


Solution

  • Here is the sample for you

    showtab.xml

     <?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" >
        </TabWidget>
    
        <FrameLayout  
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@android:id/tabcontent" >
    
       </FrameLayout>
    
      </LinearLayout>
    
    </TabHost>
    

    Below is the code of AfterLoginTabActivity(a tabactivity to be called)

    public class AfterLoginTabActivity extends TabActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.afterloginview);
            @SuppressWarnings("deprecation")
            TabHost tabHost =getTabHost();
    
            Intent i = new Intent(AfterLoginTabActivity.this,ProfileClass.class);
            TabSpec profileSpec = tabHost.newTabSpec("profile").setIndicator("Profile").setContent(i);
    
            Intent j = new Intent(AfterLoginTabActivity.this,GroupClass.class);
            TabSpec groupSpec = tabHost.newTabSpec("group").setIndicator("group").setContent(j);
    
            tabHost.addTab(profileSpec);
            tabHost.addTab(groupSpec);
    
            tabHost.setCurrentTab(1);
    
        }
        }
    

    Code i used inside my button.setOnClickListener

      if(msg == "success")
        {
            System.out.println("Inside button");
            try {
                Intent intent = new Intent(MainActivity.this,AfterLoginTabActivity.class);
                  startActivity(intent);
                  finish(); 
              } catch (Exception e) {
                   // TODO: handle exception
                  System.out.print("tHIS IS EXCCEPTION" + e);
            }
    
        }