0

我有这个设置选项卡布局的 xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
    android:orientation="vertical"
    android:id="@+id/myLayout"
    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"
            android:padding="5dp">      
            <!-- tabs are included dynamically from adapter -->           
        </FrameLayout>      
    </LinearLayout>
</TabHost>

这是我的活动:

public class TabBackupRestoreActivity extends SuperActivity {
    @Override View manageActivity() throws Exception {
        return new TabBackupRestoreAdapter(this, this,savedInstanceState);
    }
}

这是管理所有选项卡的适配器:

public class TabBackupRestoreAdapter extends TabHost {

    private static int TAB_HEIGHT=40;
    TabHost thisView;

    public TabBackupRestoreAdapter(Context context,ActivityGroup act,Bundle state) {
        super(context);
        thisView = (TabHost)inflate(getContext(), R.layout.tab_backup_restrore, this);

        LocalActivityManager lam = act.getLocalActivityManager();
        lam.dispatchCreate(state);
        thisView.setup(lam);

        addNewTab(R.string.backup,new Intent(getContext(),BackupActivity.class));
        addNewTab(R.string.restore, new Intent(getContext(),RestoreActivity.class));
    }

    private void addNewTab(int stringId, Intent activity){

        TabHost.TabSpec spec = thisView.newTabSpec(getContext().getString(stringId));
        spec = spec.setContent(activity);
        spec = spec.setIndicator(getContext().getString(stringId));
        thisView.addTab(spec);

        //modifico l'altezza del tab
        int totalTabs = thisView.getTabWidget().getChildCount();
        thisView.getTabWidget().getChildAt(totalTabs-1).getLayoutParams().height = TAB_HEIGHT;
    }
}

直到 andorid 2.2 一切正常,但如果这个应用程序在 Android 4 中运行,它会抛出这个异常:

android.content.res.Resources$NotFoundException: Resource ID #0x0
android.content.res.Resources.getValue(Resources.java:1018)
android.content.res.Resources.loadXmlResourceParser(Resources.java:2105)
android.content.res.Resources.getLayout(Resources.java:857)
android.view.LayoutInflater.inflate(LayoutInflater.java:394)
android.widget.TabHost$LabelIndicatorStrategy.createIndicatorView(TabHost.java:531)
android.widget.TabHost.addTab(TabHost.java:223)
it.hyde.adapters.TabBackupRestoreAdapter.addNewTab(TabBackupRestoreAdapter.java:41)
it.hyde.adapters.TabBackupRestoreAdapter.<init>(TabBackupRestoreAdapter.java:31)
it.hyde.activities.TabBackupRestoreActivity.manageActivity(TabBackupRestoreActivity.java:9)
it.hyde.activities.SuperActivity.onCreate(SuperActivity.java:41)
android.app.Activity.performCreate(Activity.java:4465)
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
android.app.ActivityThread.access$600(ActivityThread.java:123)
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
android.os.Handler.dispatchMessage(Handler.java:99)
android.os.Looper.loop(Looper.java:137)
android.app.ActivityThread.main(ActivityThread.java:4424)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:511)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
dalvik.system.NativeStart.main(Native Method)

我试图用android4库编译,但它是一样的。

我发现这篇文章Honeycomb and TabHost specs which resolve using a costructor,但我不能使用这个解决方案,因为我必须膨胀 xml。

有人可以帮助我吗?

问候


我找到了这个:

http://code.google.com/p/android/issues/detail?id=22605

-.-'

4

1 回答 1

1

我找到了解决方案,我不知道这是一种解决方法还是正确的方法。我要改变什么?我把TabHost作为一个LinearLayout而不是根标签的孩子

按照xml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TabHost 
        android:id="@+id/tabContainer"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <LinearLayout
            android:orientation="vertical"
            android:id="@+id/myLayout"
            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"
                android:padding="5dp">        
                <!-- tabs are included dynamically from adapter -->           
            </FrameLayout>        
        </LinearLayout>
    </TabHost>        
</LinearLayout>

这是它的适配器:

public class TabBackupRestoreAdapter extends LinearLayout {

    private static int TAB_HEIGHT=40;

    TabHost tabView;

    public TabBackupRestoreAdapter(Context context,ActivityGroup act,Bundle state) {
        super(context);
        View view = inflate(getContext(), R.layout.tab_backup_restrore, this);

        tabView = (TabHost)view.findViewById(R.id.tabContainer);    
        LocalActivityManager lam = act.getLocalActivityManager();
        lam.dispatchCreate(state);
        tabView.setup(lam);

        addNewTab(R.string.backup,new Intent(getContext(),BackupActivity.class));
        addNewTab(R.string.restore, new Intent(getContext(),RestoreActivity.class));
    }

    private void addNewTab(int stringId, Intent activity){

        TabHost.TabSpec spec = tabView.newTabSpec(getContext().getString(stringId));
        spec = spec.setIndicator(getContext().getString(stringId));
        spec = spec.setContent(activity);
        tabView.addTab(spec);

        //modifico l'altezza del tab
        int totalTabs = tabView.getTabWidget().getChildCount();
        tabView.getTabWidget().getChildAt(totalTabs-1).getLayoutParams().height = TAB_HEIGHT;
    }
}
于 2012-08-03T08:35:25.627 回答