Android - 将活动添加到TabHost?

时间:2012-12-25 01:52:15

标签: android android-tabhost

我有一个包含ListView和TabHost的Activity。 在这个TabHost中,我正在尝试添加另一个Activity,它一直在崩溃我。 知道是什么导致了这个吗?

public class MainActivity extends RoboFragmentActivity {

    @InjectView(R.id.listView)
    private ListView listView;
    @InjectView(R.id.tabHost)
    private TabHost tabHost;
    @Inject
    IIntentProxy intentProxy;

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

        tabHost.setup();

        TabHost.TabSpec spec = tabHost.newTabSpec("tag");
        spec.setContent(intentProxy.getIntent(DetailActivity.class));
        spec.setIndicator("Title");

                // It crashes on line below
                // tabHost is not null, neither is spec
        tabHost.addTab(spec);
    }

}

<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:orientation="horizontal"
    android:weightSum="100" >

    <TabHost
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tabHost"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="50" >

        <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" >
                </FrameLayout>
            </TabWidget>
        </LinearLayout>
    </TabHost>

    <ListView
        android:id="@+id/listView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="50" >
    </ListView>

</LinearLayout>

要添加到tabhost的详细活动 xml文件不包含任何内容,只包含LinearLayout

public class DetailActivity extends RoboActivity {

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

堆栈跟踪

12-24 18:06:03.535: E/AndroidRuntime(27143): FATAL EXCEPTION: main
12-24 18:06:03.535: E/AndroidRuntime(27143): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.company.app/com.company.app.pages.MainActivity}: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
12-24 18:06:03.535: E/AndroidRuntime(27143):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-24 18:06:03.535: E/AndroidRuntime(27143):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-24 18:06:03.535: E/AndroidRuntime(27143):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-24 18:06:03.535: E/AndroidRuntime(27143):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-24 18:06:03.535: E/AndroidRuntime(27143):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-24 18:06:03.535: E/AndroidRuntime(27143):    at android.os.Looper.loop(Looper.java:137)
12-24 18:06:03.535: E/AndroidRuntime(27143):    at android.app.ActivityThread.main(ActivityThread.java:5039)
12-24 18:06:03.535: E/AndroidRuntime(27143):    at java.lang.reflect.Method.invokeNative(Native Method)
12-24 18:06:03.535: E/AndroidRuntime(27143):    at java.lang.reflect.Method.invoke(Method.java:511)
12-24 18:06:03.535: E/AndroidRuntime(27143):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-24 18:06:03.535: E/AndroidRuntime(27143):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-24 18:06:03.535: E/AndroidRuntime(27143):    at dalvik.system.NativeStart.main(Native Method)
12-24 18:06:03.535: E/AndroidRuntime(27143): Caused by: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
12-24 18:06:03.535: E/AndroidRuntime(27143):    at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:747)
12-24 18:06:03.535: E/AndroidRuntime(27143):    at android.widget.TabHost.setCurrentTab(TabHost.java:413)
12-24 18:06:03.535: E/AndroidRuntime(27143):    at android.widget.TabHost.addTab(TabHost.java:240)
12-24 18:06:03.535: E/AndroidRuntime(27143):    at com.pages.ChargeSheetActivity.onCreate(ChargeSheetActivity.java:33)
12-24 18:06:03.535: E/AndroidRuntime(27143):    at android.app.Activity.performCreate(Activity.java:5104)
12-24 18:06:03.535: E/AndroidRuntime(27143):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-24 18:06:03.535: E/AndroidRuntime(27143):    at android.app.ActivityThread.performLaunchActivity(ActivityThr

ead.java:2144) 12-24 18:06:03.535:E / AndroidRuntime(27143):... 11更多

2 个答案:

答案 0 :(得分:1)

你不能使用

  

扩展活动

您必须使用

  

扩展ActivityGroup

此链接解释了一点:

Android Exception: Did you forget to call 'public void setup (LocalActivityManager activityGroup)'

答案 1 :(得分:0)

1-我延长了RoboTabActivity而不是RoboFragmentActivity

2-将我的xml从android:id="@+id/tabHost"更改为android:id="@android:id/tabhost"

3-我将活动添加到清单文件

4-而不是注入Tabhost或使用findById我使用了this.getTabHost()

现在唯一的问题是如果我需要使用DialogFragment?通常需要Activity来扩展RoboFragmentActivity