2011-12-25 47 views
1

我想创建一个选项卡式应用程序,但我试图运行应用程序时发现问题,并且主类扩展TabActivity。 主类代码如下:扩展TabActivity不工作在android

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> 

日志猫显示以下错误:

12-25 18:11:24.846 :E/AndroidRuntime(569):致命例外:main 12-25 18:11:24.846:E/AndroidRuntime(569):java.lang.RuntimeException:无法启动活动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):at android.app.ActivityThread.performLaunchActivity(ActivityThread。 java:1955) 12-25 18:11:24.846:E/AndroidRuntime(569):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980) 12-25 18:11:24.846:E/AndroidRuntime 569):at android.app.ActivityThread.access $ 600(ActivityThread.java:122) 12-25 18:11:24.846:E/AndroidRuntime(569):at android.app.ActivityThread $ H.handleMessage(ActivityThread.java :1146) 12-25 18:11:24.846:E/AndroidRuntime(569):at android.os.Handler.dispatchMessage(Handler.java:99) 12-25 18:11:24.846:E/AndroidRuntime(569) ):at android.os.Looper.loop(Looper.java:137) 12-25 18:11:24.846:E/AndroidR untime(569):at 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/A ndroidRuntime(569):at 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):at 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):at android.app.Activity.performCreate Activity.java:4465) 12-25 18:11:24.846:E/AndroidRuntime(569):at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 12-25 18:11:24.846:E/AndroidRuntime(569):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919) 12-25 18: 11:24.846:E/AndroidRuntime(569):... 11更多

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

预先感谢任何帮助

+0

请发布您在logcat中获得的例外 – 2011-12-25 15:56:26

+0

我在此问题中发布了logcat例外 – xarababe 2011-12-25 17:32:03

回答

1

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

包名是否与Java包名相同或不同。如果是的话,请进口完全合格的R类

+0

非常感谢您的帮助。它现在有效 – xarababe 2011-12-25 19:49:05

+0

如果它能正常工作,请接受答案! – 2011-12-26 03:06:33