android - 扩展 TabActivity 在 android 中不起作用

标签 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>

log cat 显示以下错误:

12-25 18:11:24.846: E/AndroidRuntime(569): 致命异常: main 12-25 18:11:24.846: E/AndroidRuntime(569): java.lang.RuntimeException: 无法启动 Activity ComponentInfo{org.practical.newsupdate/org.practical.newsupdate.NewsPage}: java.lang.RuntimeException: 你的内容必须有一个 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):在 java.lang.reflect.Method.invokeNative( native 方法) 12-25 18:11:24.846: E/AndroidRuntime(569): 在 java.lang.reflect.Method.invoke(Method.java:511) 12-25 18:11:24.846: E/AndroidRuntime(569): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 12-25 18:11:24.846: E/AndroidRuntime (569): 在 com.android.internal.os.ZygoteInit.main (ZygoteInit.java:551) 12-25 18:11:24.846: E/AndroidRuntime(569): 在 dalvik.system.NativeStart.main( native 方法) 12-25 18:11:24.846: E/AndroidRuntime(569): Caused by: 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): 在 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): 在 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 更多

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

在此先感谢您的帮助

最佳答案

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

包名与Java包名相同还是不同。如果是,请导入完全合格的 R 类

关于android - 扩展 TabActivity 在 android 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8630288/

相关文章:

android - 如何将动态 URL 传递给多选项卡中的每个 Webview Activity ,每个选项卡中有 1 个 Web View - Android App

android - 将图像添加到 Android 中的消息应用程序

android - LoginButton 请求好友列表权限,为什么?

java - 错误的 xml block 3 线性布局和 2 日历

java - 如何删除android中的tabStrip?

Android - 如何更改 TabActivity 中的 Activity ?

android 选项卡式 Activity 列表 fragment

android - 无法让 Adwhirl 显示广告 - 定量权重总和为 0

android - 寻找适用于 Android、HTC Sense、三星等皮肤的通用 TabHost 样式

android - Facebook 使用哪个 Android UX 顶部导航元素?