Reputation: 595
I am working on Tabs with each tab showing a List view
My Code is as follows: (I am showing the code for only the first Tab) can anyone please help where am I going wrong?
MY XML file is as follows
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/tab1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="60dp" >
<ListView
android:id="@+id/pcards_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
</LinearLayout>
</FrameLayout>
and my MAIN.XML is as follows
IT shows no errors but still the program crashes when i run it on my emulator
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost mtabhost =(TabHost)findViewById(R.id.tabHost);
mtabhost.setup();
TabSpec spec1 = mtabhost.newTabSpec("Tab 1");
spec1.setContent(R.id.tab1);
spec1.setIndicator("Personal Cards");
ListView pcards_list= (ListView)findViewById(R.id.pcards_list);
ArrayList<String> pcards_arraylist = new ArrayList<String>();
ArrayAdapter<String> adapter1= new ArrayAdapter<String(this,android.R.layout.simple_list_item_2,pcards_arraylist);
pcards_list.setAdapter(adapter1);
mtabhost.addTab(spec1);
}
}
please help me out!
and can any one provide me a good tutorial on tabs and also tabs using fragments having listview ?? thankyou in advance
Upvotes: 0
Views: 318
Reputation: 5258
main.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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/tab1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView
android:id="@+id/myList"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
AndroidTabActivity.java
public class AndroidTabActivity extends TabActivity {
ListView mList;
String[] myContacts = { "Anirudh", "Sonia", "Raj", "Sachin" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mList = (ListView) findViewById(R.id.myList);
ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, myContacts);
mList.setAdapter(myAdapter);
TabHost tabHost = getTabHost();
// Tab for Contact
TabSpec contactspec = tabHost.newTabSpec("Contact");
// setting Title for the Tab
contactspec.setIndicator("Contact");
contactspec.setContent(R.id.tab1);
// Tab for Song
TabSpec songspec = tabHost.newTabSpec("Song");
songspec.setIndicator("Songs");
Intent songIntent = new Intent(this, SongActivity.class);
songspec.setContent(songIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(contactspec); // Adding contact tab
tabHost.addTab(songspec); // Adding song tab
}
}
In this way you can create tabs and set the appropriate Activity
as content.
Hope this helps.
Upvotes: 1
Reputation: 2348
i learned doing the Tabs within the Tutorial from Travis (84 - 86). Not sure if it really helps, while you didnt do the rest of it before.
http://thenewboston.org/watch.php?cat=6&number=84
Upvotes: 1
Reputation: 2373
This is a good tutorial: http://android.codeandmagic.org/2011/07/android-tabs-with-fragments/ Also check: http://thepseudocoder.wordpress.com/2011/10/04/android-tabs-the-fragment-way/
Upvotes: 1
Reputation: 7646
Do u have given permission for that Activity
in AndroidManifest.xml
??? If not, add the permission under application
tag as below:
<activity android:name="MainActivity"/>
Upvotes: 0