Search code examples
androidbuttontabsandroid-tablelayout

Tabbed Application Crashes On Startup


I'm trying to make an Application to keep track of totals and Display them through a TextEdit contained in a Table. I would like to have several Tabs to keep track of different tables and therefore different totals. However when the application boots on an emulator it immediately stops working and crashes. Could anyone tell me what is wrong?

Android code:

public class CounterCounterActivity extends TabActivity implements OnClickListener {
/** Called when the activity is first created. */
TextView YourLife, OppLife;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    TabHost tabHost=(TabHost)findViewById(R.id.tabHost);
    tabHost.setup();

    TabSpec spec1=tabHost.newTabSpec("Tab 1");
    spec1.setContent(R.id.tab1);
    spec1.setIndicator("Life");

    TabSpec spec2=tabHost.newTabSpec("Tab 2");
    spec2.setIndicator("+/-");
    spec2.setContent(R.id.tab2);

    TabSpec spec3=tabHost.newTabSpec("Tab 3");
    spec3.setIndicator("Poison");
    spec3.setContent(R.id.tab3);

    tabHost.addTab(spec1);
    tabHost.addTab(spec2);
    tabHost.addTab(spec3);
    tabHost.getTabWidget().getChildAt(0).getLayoutParams().height =50;
    tabHost.getTabWidget().getChildAt(1).getLayoutParams().height =50;
    tabHost.getTabWidget().getChildAt(2).getLayoutParams().height =50;
    ((Button) findViewById(R.id.button1)).setOnClickListener(this);
    ((Button) findViewById(R.id.button2)).setOnClickListener(this);
    ((Button) findViewById(R.id.button3)).setOnClickListener(this);
    ((Button) findViewById(R.id.button4)).setOnClickListener(this);
    ((Button) findViewById(R.id.button5)).setOnClickListener(this);
    ((Button) findViewById(R.id.button6)).setOnClickListener(this);
    ((Button) findViewById(R.id.button7)).setOnClickListener(this);
    ((Button) findViewById(R.id.button8)).setOnClickListener(this);
    YourLife=(TextView)findViewById(R.id.txt2);
    OppLife=(TextView)findViewById(R.id.txt1);
}
public void onClick(View view) {
    // TODO Auto-generated method stub
    @SuppressWarnings("unused")
    Intent intent = new Intent();
    int LY=20;
    int LO=20;
    int sel=0;
    while (LY>0 && LO>0)
    {
        sel=0;
        if (view.getId()==R.id.button1)
        {
            sel=1;

        }
        if (view.getId()==R.id.button3)
        {
            sel=2;

        }
        if (view.getId()==R.id.button4)
        {
            sel=3;

        }
        if (view.getId()==R.id.button8)
        {
            sel=4;

        }
        if (view.getId()==R.id.button5)
        {
            sel=5;

        }if (view.getId()==R.id.button2)
        {
            sel=6;

        }if (view.getId()==R.id.button6)
        {
            sel=7;

        }if (view.getId()==R.id.button7)
        {
            sel=8;

        }

        switch (sel)
        {
        case 1:
        {
            LO=LO-1;
            OppLife.setText(LO);
        }
        case 2:
        {
            LO=LO-5;
            OppLife.setText(LO);
        }
        case 3:
        {
            LO=LO+1;
            OppLife.setText(LO);    
        }
        case 4:
        {
            LO=LO+5;
            OppLife.setText(LO+"");
        }
        case 5:
        {
            LY=LY-1;
            OppLife.setText(LY+"");
        }
        case 6:
        {
            LY=LY-5;
            OppLife.setText(LY+""); 
        }
        case 7:
        {
            LY=LY+1;
            OppLife.setText(LY+""); 
        }
        case 8:
        {
            LY=LY+5;
            OppLife.setText(LY+"");
        }
        }
    }

Main.XMl:

<?xml version="1.0" encoding="utf-8"?>

<TabHost android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/tabHost"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TabWidget
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@android:id/tabs"
/>
 <FrameLayout
 android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@android:id/tabcontent"
 >
<TableLayout  
xmlns:android="http://schemas.android.com/apk/res/android"  
android:id="@+id/tab1"  
android:layout_width="match_parent"  
android:layout_height="match_parent"  
android:shrinkColumns="*"  
android:stretchColumns="*"
android:paddingTop="60px">  
 <TableRow >
     <TextView  
android:layout_width="fill_parent" 
android:layout_height="100px" 
android:text="You"
android:id="@+id/txt4"
/>

     <TextView
         android:id="@+id/txt4"
         android:layout_width="fill_parent"
         android:layout_height="100px"
         android:text="Opponent" />

 </TableRow>
 <TableRow >
 <TextView  
android:layout_width="fill_parent" 
android:layout_height="200px" 
android:text="20"
android:id="@+id/txt1"
android:layout_weight="1"
android:textSize="100dp"
/>    
<TextView  
android:layout_width="fill_parent" 
android:layout_height="200px" 
android:text="20"
android:id="@+id/txt2"
android:layout_weight="1"
android:textSize="100dp"
/>    
</TableRow>

<TableRow>
    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="You -1"
        android:layout_weight="1" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Opp -1" 
        android:layout_weight="1"/>

    </TableRow>
    <TableRow>

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:text="You -5" />

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:text="Opp -5" />

</TableRow>
<TableRow >
    <Button
        android:id="@+id/button6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="You +1" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Opp +1" />

    </TableRow>

<TableRow >

<Button
    android:id="@+id/button7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="You +5" />

<Button
    android:id="@+id/button8"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Opp +5" />

</TableRow>


 </TableLayout>

 <TableLayout
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:id="@+id/tab2"
 android:shrinkColumns="*"  
 android:stretchColumns="*"
 android:paddingTop="60px"
 >
<TableRow>
 <TextView  
  android:layout_width="fill_parent" 
  android:layout_height="100px" 
  android:text="This is tab 2"
  android:id="@+id/txt2"
 />
</TableRow>
 </TableLayout>

    <TableLayout
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:id="@+id/tab3"
     android:shrinkColumns="*"  
     android:stretchColumns="*"
     android:paddingTop="60px"
 >
<TableRow>
 <TextView  
android:layout_width="fill_parent" 
android:layout_height="100px" 
android:text="This is tab 3"
android:id="@+id/txt3"
/>
</TableRow>
 </TableLayout>
 </FrameLayout>

LogCat Entrys;

09-09 12:48:16.964: D/AndroidRuntime(331): Shutting down VM
09-09 12:48:16.964: W/dalvikvm(331): threadid=1: thread exiting with uncaught exception (group=0x40015560)
09-09 12:48:16.985: E/AndroidRuntime(331): FATAL EXCEPTION: main
09-09 12:48:16.985: E/AndroidRuntime(331): java.lang.RuntimeException: Unable to start activity ComponentInfo{magic.gathering/magic.gathering.CounterCounterActivity}: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'
09-09 12:48:16.985: E/AndroidRuntime(331):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
09-09 12:48:16.985: E/AndroidRuntime(331):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-09 12:48:16.985: E/AndroidRuntime(331):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-09 12:48:16.985: E/AndroidRuntime(331):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-09 12:48:16.985: E/AndroidRuntime(331):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-09 12:48:16.985: E/AndroidRuntime(331):  at android.os.Looper.loop(Looper.java:123)
09-09 12:48:16.985: E/AndroidRuntime(331):  at android.app.ActivityThread.main(ActivityThread.java:3683)
09-09 12:48:16.985: E/AndroidRuntime(331):  at java.lang.reflect.Method.invokeNative(Native Method)
09-09 12:48:16.985: E/AndroidRuntime(331):  at java.lang.reflect.Method.invoke(Method.java:507)
09-09 12:48:16.985: E/AndroidRuntime(331):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-09 12:48:16.985: E/AndroidRuntime(331):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-09 12:48:16.985: E/AndroidRuntime(331):  at dalvik.system.NativeStart.main(Native Method)
09-09 12:48:16.985: E/AndroidRuntime(331): Caused by: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'
09-09 12:48:16.985: E/AndroidRuntime(331):  at android.app.TabActivity.onContentChanged(TabActivity.java:105)
09-09 12:48:16.985: E/AndroidRuntime(331):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:210)
09-09 12:48:16.985: E/AndroidRuntime(331):  at android.app.Activity.setContentView(Activity.java:1657)
09-09 12:48:16.985: E/AndroidRuntime(331):  at magic.gathering.CounterCounterActivity.onCreate(CounterCounterActivity.java:19)
09-09 12:48:16.985: E/AndroidRuntime(331):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-09 12:48:16.985: E/AndroidRuntime(331):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)

Any help would be appreciated.


Solution

  • 12:48:16.985: E/AndroidRuntime(331): Caused by: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'

    Replacing

    android:id="@+id/tabHost"
    

    by

    android:id="@android:id/tabHost"
    

    should work.