Nested Activities Android

3.7k views Asked by At

I have searched for the past three hours this morning to learn how to launch an activity inside a tabbed activity.

So far i've found that I need to use a tabbed activity and found multiple examples online.

However even with copy and pasting none of them are working!

I can switch between tabs if they refer to a textview element inside layout/main.xml no problem. However as soon as I click the tab which links to the TabActivity class it will instantly crash.

Here's the code i've been using:

First class is main.java which creates the tabs and intent to load FirstGroup.class FirstGroup.java is the ActivityGroup which will open CitiesActivity.java.

All cities activities does is print text-onscreen

package com.carbonaware.guigps;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;

public class main extends TabActivity{

    public TabHost tabHost;

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

            // Get the tabHost
        this.tabHost = getTabHost();

        TabHost.TabSpec spec;  // Resusable TabSpec for each tab
        Intent intent;  // Reusable Intent for each tab

        // Create an Intent to launch the first Activity for the tab (to be reused)
        intent = new Intent();

        intent.setClass(this, FirstGroup.class);

        // Initialize a TabSpec for the first tab and add it to the TabHost
        tabHost.addTab(tabHost.newTabSpec("FirstGroup").setIndicator("FirstGroup").setContent(R.id.textview1));
        tabHost.addTab(tabHost.newTabSpec("SecondGroup").setIndicator("SecondGroup").setContent(R.id.textview2));
        tabHost.addTab(tabHost.newTabSpec("ThirdGroup").setIndicator("ThirdGroup").setContent(R.id.textview3));
        tabHost.addTab(tabHost.newTabSpec("FourthGroup").setIndicator("FourthGroup").setContent(intent));

        tabHost.setCurrentTab(0);

    }
}

package com.carbonaware.guigps;

import java.util.ArrayList;
import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;

public class FirstGroup extends ActivityGroup {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);

          //you can get the local activitymanager to start the new activity

          View view = getLocalActivityManager()
                                    .startActivity("CitiesActivity", new
          Intent(this,CitiesActivity.class)
                                    .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
                                    .getDecorView();
           this.setContentView(view);

       }
    }

package com.carbonaware.guigps;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class CitiesActivity extends Activity{

       // Data to put in the ListAdapter

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

        TextView t = (TextView)findViewById(R.string.hello);


    }

}

Any help whatsoever would be fully appreciated!

1

There are 1 answers

2
tonys On

Might be a dumb question - but have you declared FirstGroup and CitiesActivity as Activities in your manifest ?

(and maybe look at/post the stacktrace from LogCat - it generally gives you a pretty good reason as to why things are crashing).