0

下面是我正在使用选项卡布局的片段活动的代码。代码似乎工作得很好,但从没有开始给出异常。我已经在两个片段中检查了我的视图,但我找不到任何错误。

package com.test.tabwithfragment;

import java.util.HashMap;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.TabContentFactory;


public class MainActivity extends FragmentActivity implements TabHost.OnTabChangeListener {

    private TabHost mTabHost;
    private HashMap<String, TabInfo> mapTabInfo = new HashMap<String, TabInfo>();
    private TabInfo mLastTab = null;

    private class TabInfo {
         private String tag;
         private Class<?> clss;
         private Bundle args;
         private Fragment fragment;
         TabInfo(String tag, Class<?> clazz, Bundle args) {
             this.tag = tag;
             this.clss = clazz;
             this.args = args;
         }

    }

    class TabFactory implements TabContentFactory {

        private final Context mContext;

        public TabFactory(Context context) {
            mContext = context;
        }

        public View createTabContent(String tag) {
            View v = new View(mContext);
            v.setMinimumWidth(0);
            v.setMinimumHeight(0);
            return v;
        }

    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initialiseTabHost(savedInstanceState);
        if (savedInstanceState != null) {
            mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); //set the tab as per the saved state
        }
    }

    protected void onSaveInstanceState(Bundle outState) {
        outState.putString("tab", mTabHost.getCurrentTabTag()); //save the tab selected
        super.onSaveInstanceState(outState);
    }

    private void initialiseTabHost(Bundle args) {
        mTabHost = (TabHost)findViewById(android.R.id.tabhost);
        mTabHost.setup();
        TabInfo tabInfo = null;
/*        MainActivity.addTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab1").setIndicator("Tab 1"), ( tabInfo = new TabInfo("Tab1", FragmentOne.class, args)));
        this.mapTabInfo.put(tabInfo.tag, tabInfo);*/
        MainActivity.addTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab2").setIndicator("Tab 2"), ( tabInfo = new TabInfo("Tab2", FragmentTwo.class, args)));
        this.mapTabInfo.put(tabInfo.tag, tabInfo);
        MainActivity.addTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab3").setIndicator("Tab 3"), ( tabInfo = new TabInfo("Tab3", FragmentTwo.class, args)));
        this.mapTabInfo.put(tabInfo.tag, tabInfo);
        this.onTabChanged("Tab1");
        //
        mTabHost.setOnTabChangedListener(this);
    }

    private static void addTab(MainActivity activity, TabHost tabHost, TabHost.TabSpec tabSpec, TabInfo tabInfo) {
        tabSpec.setContent(activity.new TabFactory(activity));
        String tag = tabSpec.getTag();
        tabInfo.fragment = activity.getSupportFragmentManager().findFragmentByTag(tag);
        if (tabInfo.fragment != null && !tabInfo.fragment.isDetached()) {
            FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
            ft.detach(tabInfo.fragment);
            ft.commit();
            activity.getSupportFragmentManager().executePendingTransactions();
        }

        tabHost.addTab(tabSpec);
    }

    public void onTabChanged(String tag) {
        TabInfo newTab = this.mapTabInfo.get(tag);
        if (mLastTab != newTab) {
            FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();
            if (mLastTab != null) {
                if (mLastTab.fragment != null) {
                    ft.detach(mLastTab.fragment);
                }
            }
            if (newTab != null) {
                if (newTab.fragment == null) {
                    newTab.fragment = Fragment.instantiate(this,
                            newTab.clss.getName(), newTab.args);
                    ft.add(R.id.realtabcontent, newTab.fragment, newTab.tag);
                } else {
                    ft.attach(newTab.fragment);
                }
            }

            mLastTab = newTab;
            ft.commit();
            this.getSupportFragmentManager().executePendingTransactions();
        }
    }

}

这是我的 logcat 的副本-

06-07 17:19:06.691: E/AndroidRuntime(13410): FATAL EXCEPTION: main
06-07 17:19:06.691: E/AndroidRuntime(13410): java.lang.IllegalArgumentException: No view found for id 0x7f080000 (com.test.tabwithfragment:id/realtabcontent) for fragment FragmentTwo{419aca50 #0 id=0x7f080000 Tab3}
06-07 17:19:06.691: E/AndroidRuntime(13410):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)
06-07 17:19:06.691: E/AndroidRuntime(13410):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
06-07 17:19:06.691: E/AndroidRuntime(13410):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
06-07 17:19:06.691: E/AndroidRuntime(13410):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
06-07 17:19:06.691: E/AndroidRuntime(13410):    at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
06-07 17:19:06.691: E/AndroidRuntime(13410):    at com.test.tabwithfragment.MainActivity.onTabChanged(MainActivity.java:115)
06-07 17:19:06.691: E/AndroidRuntime(13410):    at android.widget.TabHost.invokeOnTabChangeListener(TabHost.java:391)
06-07 17:19:06.691: E/AndroidRuntime(13410):    at android.widget.TabHost.setCurrentTab(TabHost.java:376)
06-07 17:19:06.691: E/AndroidRuntime(13410):    at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:150)
06-07 17:19:06.691: E/AndroidRuntime(13410):    at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:548)
06-07 17:19:06.691: E/AndroidRuntime(13410):    at android.view.View.performClick(View.java:4110)
06-07 17:19:06.691: E/AndroidRuntime(13410):    at android.view.View$PerformClick.run(View.java:17001)
06-07 17:19:06.691: E/AndroidRuntime(13410):    at android.os.Handler.handleCallback(Handler.java:615)
06-07 17:19:06.691: E/AndroidRuntime(13410):    at android.os.Handler.dispatchMessage(Handler.java:92)
06-07 17:19:06.691: E/AndroidRuntime(13410):    at android.os.Looper.loop(Looper.java:137)
06-07 17:19:06.691: E/AndroidRuntime(13410):    at android.app.ActivityThread.main(ActivityThread.java:4854)
06-07 17:19:06.691: E/AndroidRuntime(13410):    at java.lang.reflect.Method.invokeNative(Native Method)
06-07 17:19:06.691: E/AndroidRuntime(13410):    at java.lang.reflect.Method.invoke(Method.java:511)
06-07 17:19:06.691: E/AndroidRuntime(13410):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
06-07 17:19:06.691: E/AndroidRuntime(13410):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
06-07 17:19:06.691: E/AndroidRuntime(13410):    at dalvik.system.NativeStart.main(Native Method)

更新

我的xml文件

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

1 回答 1

0

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

代替

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

在布局中

于 2013-06-07T12:02:35.630 回答