If Tabs and Activities don't go together..Is there a way to cheat?
I am having a real hard time on putting my activities in tabs. I have an activity that parses an XML file and puts them on a list, and it works perfectly on its own. When I call it on a tab however it does not work (i get the dreaded "Sorry! blah blah.. has stopped unexpectedly" prompt... BTW yes I did the manifest).
I have migrated the activities to work as one activity, voila! it worked!!! However this is not the way we wanted to go with this project - WE REALLY NEED to have separate activities.
So as many people had found out that tabs and activities doesn't work well together i开发者_如何学Gos there a way to work around it? Some sort of emulsifier maybe?
Here is the code:
import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.TabHost; import android.widget.Toast;
public class TabDemo extends Activity{
/** data members go here*/
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
try {
TabHost tabs= (TabHost)findViewById(R.id.tabhost);
tabs.setup();
Intent callResultHits = new Intent(this, my.tabebd.layout.ResultHits.class);
TabHost.TabSpec spec = tabs.newTabSpec("tag1");
spec.setContent(callResultHits);
spec.setIndicator("Result", getResources().getDrawable(R.drawable.ic_tab_search_result) );
tabs.addTab(spec);
spec = tabs.newTabSpec("tag2");
spec.setContent(R.id.tab2);
spec.setIndicator("Details",getResources().
getDrawable(R.drawable.ic_tab_details));
tabs.addTab(spec);
tabs.setCurrentTabByTag("tag1");
} catch (Throwable t) {
// TODO Auto-generated catch block
Toast.makeText(this, "Exception: " + t.toString(), 50000).show();
}
}
here is one of the activities...
public class ResultHits extends Activity implements OnItemClickListener {
ListView listView_titles;
ArrayList<String> items = new ArrayList<String>();
String [] test = {"1", "2","3"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listView_titles = (ListView)findViewById(R.id.list);
listView_titles.setAdapter(new ArrayAdapter<String>
(this,R.layout.row, R.id.row_text,test));
}
}
I omitted the xml parsing part... if this basic list can be shown inside the tab then it will be perfect. TY in advance
BTW setcurrenttabByTag was previously setCurrenttab(2)..actualy i did these values 0,1, 2, 3 just in case ;)
Post your code I think you will not have problems putting activities into every tab, This is a basic Example of how put activities in tabs,
static TabHost 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, MainActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("MainActivity")
.setIndicator(null, null)
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, SecondActivity.class);
spec = tabHost.newTabSpec("SecondActivity")
.setIndicator(null, null)
tabHost.addTab(spec);
intent = new Intent().setClass(this, ThirdActivity.class);
spec = tabHost.newTabSpec("ThirdActivity")
.setIndicator(null, null)
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, FourthActivity.class);
spec = tabHost.newTabSpec("FourthActivity")
.setIndicator(null, null)
.setContent(intent);
tabHost.addTab(spec);
精彩评论