扩展TabActivity在android中不起作用

时间:2011-12-25 15:45:33

标签: android android-tabhost android-tabactivity

我正在尝试创建一个选项卡式应用程序但是,我在尝试运行应用程序时遇到问题,而主类扩展了TabActivity。 主类代码如下:

package org.practical.newsupdate;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class NewsPage extends TabActivity{
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.newspage);

    TabHost host = (TabHost) findViewById(R.id.tabhost);
    host.setup();

    new Intent().setClass(this, LocalNews.class);
    TabSpec localTab = host.newTabSpec("Local");
    localTab.setIndicator(getResources().getString(R.string.local),
    getResources().getDrawable(android.R.drawable.star_on));
    localTab.setContent(R.id.local_tab);
    host.addTab(localTab);

    new Intent().setClass(this, ForeignNews.class);
    TabSpec foreignTab = host.newTabSpec("Foreign");
    foreignTab.setIndicator(getResources().getString(R.string.foreign),
    getResources().getDrawable(android.R.drawable.star_on));
    foreignTab.setContent(R.id.foreign_tab);
    host.addTab(foreignTab);

    host.setCurrentTabByTag("Local");
}   
} 

XML如下:     

<RelativeLayout
    android:id="@+id/relativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/newsimages" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/newsimages" />

    <TextView
        android:id="@+id/newspage_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/menu_newspage" />
</RelativeLayout>

<TabHost
    android:id="@+id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </TabWidget>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <LinearLayout
                android:id="@+id/local_tab"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </LinearLayout>

            <LinearLayout
                android:id="@+id/foreign_tab"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>
</TabHost>
</LinearLayout>

日志cat显示以下错误:

12-25 18:11:24.846:E / AndroidRuntime(569):致命异常:主要 12-25 18:11:24.846:E / AndroidRuntime(569):java.lang.RuntimeException:无法启动活动ComponentInfo {org.practical.newsupdate / org.practical.newsupdate.NewsPage}:java.lang.RuntimeException:Your content必须有一个TabHost,其id属性为'android.R.id.tabhost' 12-25 18:11:24.846:E / AndroidRuntime(569):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955) 12-25 18:11:24.846:E / AndroidRuntime(569):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980) 12-25 18:11:24.846:E / AndroidRuntime(569):在android.app.ActivityThread.access $ 600(ActivityThread.java:122) 12-25 18:11:24.846:E / AndroidRuntime(569):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1146) 12-25 18:11:24.846:E / AndroidRuntime(569):在android.os.Handler.dispatchMessage(Handler.java:99) 12-25 18:11:24.846:E / AndroidRuntime(569):在android.os.Looper.loop(Looper.java:137) 12-25 18:11:24.846:E / AndroidRuntime(569):在android.app.ActivityThread.main(ActivityThread.java:4340) 12-25 18:11:24.846:E / AndroidRuntime(569):at java.lang.reflect.Method.invokeNative(Native Method) 12-25 18:11:24.846:E / AndroidRuntime(569):at java.lang.reflect.Method.invoke(Method.java:511) 12-25 18:11:24.846:E / AndroidRuntime(569):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784) 12-25 18:11:24.846:E / AndroidRuntime(569):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 12-25 18:11:24.846:E / AndroidRuntime(569):at dalvik.system.NativeStart.main(Native Method) 12-25 18:11:24.846:E / AndroidRuntime(569):引起:java.lang.RuntimeException:您的内容必须有一个TabHost,其id属性为'android.R.id.tabhost' 12-25 18:11:24.846:E / AndroidRuntime(569):在android.app.TabActivity.onContentChanged(TabActivity.java:131) 12-25 18:11:24.846:E / AndroidRuntime(569):at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:254) 12-25 18:11:24.846:E / AndroidRuntime(569):在android.app.Activity.setContentView(Activity.java:1835) 12-25 18:11:24.846:E / AndroidRuntime(569):at org.practical.newsupdate.NewsPage.onCreate(NewsPage.java:13) 12-25 18:11:24.846:E / AndroidRuntime(569):在android.app.Activity.performCreate(Activity.java:4465) 12-25 18:11:24.846:E / AndroidRuntime(569):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 12-25 18:11:24.846:E / AndroidRuntime(569):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919) 12-25 18:11:24.846:E / AndroidRuntime(569):... 11 more

显示的错误是应用程序突然停止工作。有人可以帮助我吗?

提前感谢您提供任何帮助

1 个答案:

答案 0 :(得分:1)

以下行给出了错误 Java.lang.RuntimeException:您的内容必须有一个TabHost,其id属性为'android.R.id.tabhost'

包名称是否与Java包名称相同或不同。如果是,请输入完全合格的R级