javaandroidspinnerandroid-contexttabwidget

Android Spinner Within TabWidget Bad Token


I'm fairly new to Android and Java for that matter, and am having a context issue - I have a Spinner within a TabWidget within another TabWidget, and on the code side I have my main class which is a TabActivity, in which I set the intent of a particular tab to another TabActivity, in which I set the intent of a tab to an Activity which I set the content view to a layout where I have my Spinner (hopefully that made sense). Anytime I click on the spinner and it attempts to launch the dialog for the spinner it gives me a bad token error. From what I've read, it's because my spinner has the wrong context, but since I'm not actively setting my context (I created the spinner in XML), and I can't seem to find a method or exposed property to set the context of the spinner...well you get the point, I'm lost on this one despite reading other people's similar issues. Here's my code:

Main:

public class Star_Android_Activity extends TabActivity 
{
    public static void main(String[] args) {
    }

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

        TabHost tabHost = getTabHost();  // The activity TabHost
        TabHost.TabSpec spec;  // Resusable TabSpec for each tab
        Intent intent;  // Reusable Intent for each tab

        // Create an Intent to launch an Activity for the tab (to be reused)
        intent = new Intent().setClass(this, Pick_Activity.class);

        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("pick").setIndicator(LinearLayout.inflate(this,R.layout.tab_header_pick, null))
            .setContent(intent);
        tabHost.addTab(spec);

        tabHost.setCurrentTab(0);
    }
}

Sub-Tabs:

public class Pick_Activity extends TabActivity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

            setContentView(R.layout.pick);

            TabHost tabHost = getTabHost();  // The activity TabHost
            TabHost.TabSpec spec;  // Reusable TabSpec for each tab
            Intent intent;  // Reusable Intent for each tab

            intent = new Intent().setClass(this, Pending_Activity.class);
            spec = tabHost.newTabSpec("pending").setIndicator(LinearLayout.inflate(this,R.layout.tab_header_pending, null))
                          .setContent(intent);
            tabHost.addTab(spec);

            tabHost.setCurrentTab(0);
    }
}

Child Activity:

public class Pending_Activity extends Activity 
{

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

        addSpinnerValues();
    }

    public void addSpinnerValues()
    {
        Spinner spn = (Spinner)findViewById(R.id.spnPick);
        List<String> list = new ArrayList<String>();
        list.add("list 1");
        list.add("list 2");
        list.add("list 3");
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spn.setAdapter(dataAdapter);    
    }

}

The xml files for Main and and Pick are basically copies of the hello tabwidget tutorial, and my pending.xml is just a FrameLayout containing a few buttons and my spinner. I can post the code if it helps, but I was having issues posting anymore code (character limit perhaps).

Any help on how to properly set my contexts (and a small explanation if it's not a hassle) would be greatly appreciated!


Solution

  • Took me a while, but I found another posting that had part of the solution, and at least for my purposes helped a great deal...I simply changed my:

    setContentView(R.layout.pending); 
    

    to:

    View viewToLoad = LayoutInflater.from(this.getParent()).inflate(R.layout.pending, null); 
        this.setContentView(viewToLoad); 
    

    So that the context of my pending.xml page was set to the parent's context...although I'll confess, I don't really understand why it wouldn't have to have been the parent's parent, and why just the parent which itself was a child is acceptable...hopefully given some time I'll start to understand it.