简体   繁体   English

为什么findViewById(android.R.id.tabhost)返回null?

[英]Why findViewById(android.R.id.tabhost) returns null?

This is an example from ActionBarSherlock samples, I completely follow this sample, but findViewById(android.R.id.tabhost) returns null, and my application breaks with NullPointerException. 这是ActionBarSherlock示例的示例,我完全遵循此示例,但是findViewById(android.R.id.tabhost)返回null,并且我的应用程序使用NullPointerException中断。 Does anyone know solution of this problem? 有人知道这个问题的解决方案吗?

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated constructor stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main_fragment_tabs);
    mTabHost = (TabHost)findViewById(android.R.id.tabhost);
    mTabHost.setup();

    mTabManager = new TabManager(this, mTabHost, R.id.realtabcontent);

    mTabManager.addTab(mTabHost.newTabSpec("vote").setIndicator("Vote"), VoteActivity.class, null);


    if (savedInstanceState != null) {
        mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
    }
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putString("tab", mTabHost.getCurrentTabTag());
}

/**
 * This is a helper class that implements a generic mechanism for
 * associating fragments with the tabs in a tab host. It relies on a trick.
 * Normally a tab host has a simple API for supplying a View or Intent that
 * each tab will show. This is not sufficient for switching between
 * fragments. So instead we make the content part of the tab host 0dp high
 * (it is not shown) and the TabManager supplies its own dummy view to show
 * as the tab content. It listens to changes in tabs, and takes care of
 * switch to the correct fragment shown in a separate content area whenever
 * the selected tab changes.
 */
public static class TabManager implements TabHost.OnTabChangeListener {
    private final FragmentActivity mActivity;
    private final TabHost mTabHost;
    private final int mContainerId;
    private final HashMap<String, TabInfo> mTabs = new HashMap<String, TabInfo>();
    TabInfo mLastTab;

    static final class TabInfo {
        private final String tag;
        private final Class<?> clss;
        private final Bundle args;
        private Fragment fragment;

        TabInfo(String _tag, Class<?> _class, Bundle _args) {
            tag = _tag;
            clss = _class;
            args = _args;
        }
    }

    static class DummyTabFactory implements TabHost.TabContentFactory {
        private final Context mContext;

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

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

    public TabManager(FragmentActivity activity, TabHost tabHost,
            int containerId) {
        mActivity = activity;
        mTabHost = tabHost;
        mContainerId = containerId;
        mTabHost.setOnTabChangedListener(this);
    }

    public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {
        tabSpec.setContent(new DummyTabFactory(mActivity));
        String tag = tabSpec.getTag();

        TabInfo info = new TabInfo(tag, clss, args);

        // Check to see if we already have a fragment for this tab, probably
        // from a previously saved state. If so, deactivate it, because our
        // initial state is that a tab isn't shown.
        info.fragment = mActivity.getSupportFragmentManager()
                .findFragmentByTag(tag);
        if (info.fragment != null && !info.fragment.isDetached()) {
            FragmentTransaction ft = mActivity.getSupportFragmentManager()
                    .beginTransaction();
            ft.detach(info.fragment);
            ft.commit();
        }

        mTabs.put(tag, info);
        mTabHost.addTab(tabSpec);
    }

    @Override
    public void onTabChanged(String tabId) {
        TabInfo newTab = mTabs.get(tabId);
        if (mLastTab != newTab) {
            FragmentTransaction ft = mActivity.getSupportFragmentManager()
                    .beginTransaction();
            if (mLastTab != null) {
                if (mLastTab.fragment != null) {
                    ft.detach(mLastTab.fragment);
                }
            }
            if (newTab != null) {
                if (newTab.fragment == null) {
                    newTab.fragment = Fragment.instantiate(mActivity,
                            newTab.clss.getName(), newTab.args);
                    ft.add(mContainerId, newTab.fragment, newTab.tag);
                } else {
                    ft.attach(newTab.fragment);
                }
            }

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

main_fragment_tabs.xml main_fragment_tabs.xml

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

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

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

        <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>

NEVER use android.R in your project (even though sometimes eclipse may suggest you to do so) !!! 永远不要在您的项目中使用android.R (即使有时蚀可能会建议您这样做)

It does not refer to the generated R.java that contains the generated ids for the elements in your XML layout and is thus, the root of many problems beginners face! 它没有引用生成的R.java ,它包含XML布局中元素的生成的ID,因此,它是初学者面临的许多问题的根源!

Always use R instead of android.R 始终使用R而不是android.R

So, your code should be findViewById(R.id.tabhost) 因此,您的代码应为findViewById(R.id.tabhost)

Change this 改变这个

mTabHost = (TabHost)findViewById(android.R.id.tabhost);

do:- 做:-

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM