0

作为初学者,我想学习使用 tabhost/tabwidget 的 android 选项卡式布局,尽管我已经阅读了一些已经弃用的帖子。所以...基本上,我想要实现的是有一个带有两个选项卡的布局,这两个选项卡都将显示一个具有不同内容的 web 视图。但是每次我尝试访问该布局时,我的应用程序都会崩溃。

这是它在我的 Logcat 中所说的:

日志猫:

   04-09 22:30:53.628: E/AndroidRuntime(10406): FATAL EXCEPTION: main
04-09 22:30:53.628: E/AndroidRuntime(10406): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nicx.amazing/com.nicx.amazing.TabActivity}: java.lang.IllegalStateException: Already attached
04-09 22:30:53.628: E/AndroidRuntime(10406):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
04-09 22:30:53.628: E/AndroidRuntime(10406):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
04-09 22:30:53.628: E/AndroidRuntime(10406):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
04-09 22:30:53.628: E/AndroidRuntime(10406):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
04-09 22:30:53.628: E/AndroidRuntime(10406):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-09 22:30:53.628: E/AndroidRuntime(10406):    at android.os.Looper.loop(Looper.java:137)
04-09 22:30:53.628: E/AndroidRuntime(10406):    at android.app.ActivityThread.main(ActivityThread.java:4448)
04-09 22:30:53.628: E/AndroidRuntime(10406):    at java.lang.reflect.Method.invokeNative(Native Method)
04-09 22:30:53.628: E/AndroidRuntime(10406):    at java.lang.reflect.Method.invoke(Method.java:511)
04-09 22:30:53.628: E/AndroidRuntime(10406):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
04-09 22:30:53.628: E/AndroidRuntime(10406):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
04-09 22:30:53.628: E/AndroidRuntime(10406):    at dalvik.system.NativeStart.main(Native Method)
04-09 22:30:53.628: E/AndroidRuntime(10406): Caused by: java.lang.IllegalStateException: Already attached
04-09 22:30:53.628: E/AndroidRuntime(10406):    at android.support.v4.app.FragmentManagerImpl.attachActivity(FragmentManager.java:1844)
04-09 22:30:53.628: E/AndroidRuntime(10406):    at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:193)
04-09 22:30:53.628: E/AndroidRuntime(10406):    at com.nicx.amazing.TabActivity.onCreate(TabActivity.java:15)
04-09 22:30:53.628: E/AndroidRuntime(10406):    at android.app.Activity.performCreate(Activity.java:4465)
04-09 22:30:53.628: E/AndroidRuntime(10406):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-09 22:30:53.628: E/AndroidRuntime(10406):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
04-09 22:30:53.628: E/AndroidRuntime(10406):    ... 11 more

这是我的 TabActivity.java 中的内容:

import android.os.Bundle;
import android.support.v4.app.*;
import android.widget.TabHost;

public class TabActivity extends FragmentActivity {

        TabHost tHost;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            tHost = (TabHost) findViewById(android.R.id.tabhost);
            tHost.setup();

            TabHost.TabSpec fb = tHost.newTabSpec("facebook");
            fb.setIndicator("Facebook", null);
            fb.setContent(new DummyTabContent(getBaseContext()));
            tHost.addTab(fb);

            TabHost.TabSpec tw = tHost.newTabSpec("twitter");
            tw.setIndicator("Twitter", null);
            tw.setContent(new DummyTabContent(getBaseContext()));
            tHost.addTab(tw);

        }
}

注意:我还没有弄清楚如何在我的选项卡中放置 web 视图,我只想首先查看选项卡的布局。

我的 activity_tab.xml 中也有这个:

<TabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0"/>

        <FrameLayout
            android:id="@+id/realtabcontent"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>

    </LinearLayout>
</TabHost>

我使用http://wptrafficanalyzer.in/blog/creating-navigation-tabs-using-tabhost-and-fragments-in-android/作为指南。我只是没有遵循它的片段,因为我没有使用它。你能帮我解决这个问题吗?

4

1 回答 1

0

哦,亲爱的,Facebook和Twitter肯定还有两个活动台,你把它们抛在后面了吗?

您必须创建这些活动才能完成您的应用程序。

而且我发现您的 TabActivity 是从 FrameActivity 扩展而来的,这似乎不太正确。您的 MainActivity 应该扩展 TabActivity。

这是我的一个例子

package com.izzyleung.mixedbartest;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost;

@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TabHost tabHost = getTabHost();

    Intent imageIntent = new Intent();
    imageIntent.setClass(this, ImageActivity.class);
    TabHost.TabSpec imageSpec = tabHost.newTabSpec("Image");
    imageSpec.setIndicator("Image");
    imageSpec.setContent(imageIntent);
    tabHost.addTab(imageSpec);

    Intent notificationIntent = new Intent();
    notificationIntent.setClass(this, NotificationActivity.class);
    TabHost.TabSpec notificationSpec = tabHost.newTabSpec("Notification");
    notificationSpec.setIndicator("Notification");
    notificationSpec.setContent(notificationIntent);
    tabHost.addTab(notificationSpec);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.action_settings:
        Intent settingsIntent = new Intent();
        settingsIntent.setClass(this, SettingsActivity.class);
        startActivity(settingsIntent);
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
   }
}

我的 XML 文件是这样的:

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5sp" >

    <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"
        android:padding="5sp" >
    </FrameLayout>
</LinearLayout>

于 2013-04-09T15:03:27.043 回答