tags:

views:

58

answers:

1

I wanna add three layout in a TabActivity, but it was force closed when I ran it.

Here is the code:

import android.app.TabActivity; import android.content.res.Resources; import android.os.Bundle; import android.widget.TabHost;

public class Test1 extends TabActivity {

public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);
     Resources res = getResources(); 
     TabHost tabHost = getTabHost();      

    tabHost.addTab(tabHost.newTabSpec("egcquery").setIndicator("EGCQuery").setContent(R.layout.main1)); 
    tabHost.addTab(tabHost.newTabSpec("PatientInfo").setIndicator("PatientInfo").setContent(R.layout.main2)); 
    tabHost.addTab(tabHost.newTabSpec("Comp_Interp").setIndicator("Comp_Interp").setContent(R.layout.main3));
}

}

A: 

See my answer to this question: http://stackoverflow.com/questions/2941241/why-do-i-get-an-error-while-trying-to-set-the-content-of-a-tabspec-in-android/2941481#2941481

Tim Kryger
Thanks for your answer.But I change this it is still force closed :(tabHost.addTab(tabHost.newTabSpec("egcquery").setIndicator("EGCQuery").setContent(R.id.L1)); tabHost.addTab(tabHost.newTabSpec("PatientInfo").setIndicator("PatientInfo").setContent(R.id.L2)); tabHost.addTab(tabHost.newTabSpec("Comp_Interp").setIndicator("Comp_Interp").setContent(R.id.L3));
Kooper
Did you add the corresponding android:id tag to your xml layout files?
Tim Kryger
Yes, I've already added. Like this:<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/L1" ><TextView android:layout_width="186px" android:layout_height="29px" android:text="This is Layout1" android:layout_x="70px" android:id="@+id/text1" android:textSize="24sp" android:layout_y="32px" /></LinearLayout>
Kooper
What exception are you getting now?
Tim Kryger
I just see "The application Test1(process irdc.test1)has stopped unexpectedly. Please try again". Could you tell me how to find the reason??
Kooper
Try single stepping through your code in the debugger and see when it fails.
Tim Kryger
In the Debugger window , the "Method.invokeNative" and "NativaStart.main" lines show that can't be used.
Kooper
Did I see the right place ??
Kooper
At first I put three Activity in a TabActivity and it can works. But I wanna set TextView in Tab2 by clicking a button in Tab1. An member tell me use layout is much easier, no Activity. I've tried but it didn't success util now :( .
Kooper