选中选项卡时加载活动

时间:2012-01-09 10:07:27

标签: android

当我在我的应用中选择收藏夹标签时,我正在尝试加载“收藏夹”列表视图。所有选项卡都正确显示,但当我选择收藏夹时,应用程序因IllegalStateException异常而崩溃。

java代码如下:

    public class tabs extends Activity{


        protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.tabmain);

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

            tabs.setup();

            TabHost.TabSpec spec=tabs.newTabSpec("tag1");    
            spec.setContent(R.id.tab1);
            spec.setIndicator("tab1");
            tabs.addTab(spec);

            spec=tabs.newTabSpec("tag2");
            spec.setContent(R.id.tab2);
            spec.setIndicator("tab2");
            tabs.addTab(spec);

            spec=tabs.newTabSpec("tag3");
            spec.setIndicator("Favourites", getResources().getDrawable(R.drawable.favourites_star));
            spec.setContent(new Intent(this,favourites.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
            tabs.addTab(spec);


        }


    }


    The xml file is as follows: 

    <?xml version="1.0" encoding="utf-8"?>
    <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/tabhost"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">
      <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <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">

           <LinearLayout 
          android:id="@+id/tab1"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:orientation="vertical"/>

           <LinearLayout 
          android:id="@+id/tab2"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:orientation="vertical"/>

          <LinearLayout 
          android:id="@+id/tab3"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:orientation="vertical"/>

        </FrameLayout>
      </LinearLayout>
    </TabHost>


Stack Trace: 

Thread [<3> main] (Suspended (exception IllegalStateException)) 
    ViewRoot.handleMessage(Message) line: 1740  
    ViewRoot(Handler).dispatchMessage(Message) line: 99 
    Looper.loop() line: 123 
    ActivityThread.main(String[]) line: 4363    
    Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
    Method.invoke(Object, Object...) line: 521  
    ZygoteInit$MethodAndArgsCaller.run() line: 860  
    ZygoteInit.main(String[]) line: 618 
    NativeStart.main(String[]) line: not available [native method]  

1 个答案:

答案 0 :(得分:0)

确保您在Android清单文件中定义了favourites活动