为什么我的android tabhost会一直崩溃?

时间:2012-09-12 08:54:27

标签: android android-tabhost

这是标签主机的xml 我不认为错误在这里但仍然是..

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/negru"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/scoreId"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/scores"
        android:textColor="@color/galben"
        android:textSize="@dimen/menuDim" />

    <TabHost
        android:id="@+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"
            android:padding="5dp" >

            <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"
                android:padding="5dp" >

                <include
                    android:id="@+id/cell1"
                    layout="@layout/cel1" />

                <include
                    android:id="@+id/cell2"
                    layout="@layout/cel1" />
            </FrameLayout>
        </LinearLayout>
    </TabHost>

</LinearLayout>

这是包含tabhost设置的java文件 我想在这里我在初始化tabhost时有一个错误,但我无法弄清楚

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scores);

    TabHost tabHost = (TabHost)findViewById(R.id.tabhost);

    tabHost.setup();

    TabSpec allScore = tabHost.newTabSpec("allTab");
    allScore.setIndicator("allTab");
    allScore.setContent(R.id.cell1);
    tabHost.addTab(allScore);

    TabSpec friendScore = tabHost.newTabSpec("friendTab");
    allScore.setIndicator("friendTab");
    allScore.setContent(R.id.cell2);
    tabHost.addTab(friendScore);

    tabHost.setCurrentTabByTag("allTab");

}
用于在单元格中显示的

和cell1布局

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollAllScore"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scrollbars="vertical" >

    <TableLayout
        android:id="@+id/AllScore"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="*" >
        <TableRow 
            >
            <TextView 
                android:text="@string/app_name"/>
        </TableRow>
        <TableRow 
            >
            <TextView 
                android:text="@string/app_name"/>
        </TableRow>
    </TableLayout>
</ScrollView>

这是调试信息

Thread [<1> main] (Suspended (exception RuntimeException))  
    <VM does not provide monitor information>   
    ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 1664    
    ActivityThread.handleLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 1680 
    ActivityThread.access$1500(ActivityThread, ActivityThread$ActivityClientRecord, Intent) line: 117   
    ActivityThread$H.handleMessage(Message) line: 931   
    ActivityThread$H(Handler).dispatchMessage(Message) line: 99 
    Looper.loop() line: 130 
    ActivityThread.main(String[]) line: 3703    
    Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
    Method.invoke(Object, Object...) line: 507  
    ZygoteInit$MethodAndArgsCaller.run() line: 841  
    ZygoteInit.main(String[]) line: 599 
    NativeStart.main(String[]) line: not available [native method]  

请帮帮我,我真的看不出有什么问题!

2 个答案:

答案 0 :(得分:0)

尝试更改root标签:

<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" >

可能是他的问题。

答案 1 :(得分:0)

错误就在这里

TabSpec allScore = tabHost.newTabSpec("allTab");
    allScore.setIndicator("allTab");
    allScore.setContent(R.id.cell1);
    tabHost.addTab(allScore);

    TabSpec friendScore = tabHost.newTabSpec("friendTab");
    allScore.setIndicator("friendTab");
    allScore.setContent(R.id.cell2);
    tabHost.addTab(friendScore);

我忘了将tabspec变量名从allscore更改为friendscore