将tab添加到actionbar null指针

时间:2014-08-23 05:08:11

标签: android android-actionbar actionbarsherlock

我一直在寻找解决方案几个小时,最后放弃了。 我正在关注使用可滑动的viewpager for actionbarsherlock的教程。但是在向操作栏添加选项卡时出现错误:

错误:

08-23 05:40:38.750: E/AndroidRuntime(13230): FATAL EXCEPTION: main
08-23 05:40:38.750: E/AndroidRuntime(13230): Process: com.example.tabtest, PID: 13230
08-23 05:40:38.750: E/AndroidRuntime(13230): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tabtest/com.dise.nefs.ui.MainActivity}: java.lang.IllegalStateException: Action Bar Tab must have a Callback
08-23 05:40:38.750: E/AndroidRuntime(13230):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2248)
08-23 05:40:38.750: E/AndroidRuntime(13230):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2298)
08-23 05:40:38.750: E/AndroidRuntime(13230):    at android.app.ActivityThread.access$800(ActivityThread.java:144)
08-23 05:40:38.750: E/AndroidRuntime(13230):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
08-23 05:40:38.750: E/AndroidRuntime(13230):    at android.os.Handler.dispatchMessage(Handler.java:102)
08-23 05:40:38.750: E/AndroidRuntime(13230):    at android.os.Looper.loop(Looper.java:212)
08-23 05:40:38.750: E/AndroidRuntime(13230):    at android.app.ActivityThread.main(ActivityThread.java:5151)
08-23 05:40:38.750: E/AndroidRuntime(13230):    at java.lang.reflect.Method.invokeNative(Native Method)
08-23 05:40:38.750: E/AndroidRuntime(13230):    at java.lang.reflect.Method.invoke(Method.java:515)
08-23 05:40:38.750: E/AndroidRuntime(13230):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-23 05:40:38.750: E/AndroidRuntime(13230):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)
08-23 05:40:38.750: E/AndroidRuntime(13230):    at dalvik.system.NativeStart.main(Native Method)
08-23 05:40:38.750: E/AndroidRuntime(13230): Caused by: java.lang.IllegalStateException: Action Bar Tab must have a Callback
08-23 05:40:38.750: E/AndroidRuntime(13230):    at com.android.internal.app.ActionBarImpl.configureTab(ActionBarImpl.java:478)
08-23 05:40:38.750: E/AndroidRuntime(13230):    at com.android.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:504)
08-23 05:40:38.750: E/AndroidRuntime(13230):    at com.android.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:492)
08-23 05:40:38.750: E/AndroidRuntime(13230):    at com.actionbarsherlock.internal.app.ActionBarWrapper.addTab(ActionBarWrapper.java:389)
08-23 05:40:38.750: E/AndroidRuntime(13230):    at com.dise.nefs.ui.MainActivity.onCreate(MainActivity.java:90)
08-23 05:40:38.750: E/AndroidRuntime(13230):    at android.app.Activity.performCreate(Activity.java:5231)
08-23 05:40:38.750: E/AndroidRuntime(13230):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-23 05:40:38.750: E/AndroidRuntime(13230):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2212)
08-23 05:40:38.750: E/AndroidRuntime(13230):    ... 11 more

MainActivity.java:

    package com.dise.nefs.ui;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.Stack;
import java.util.UUID;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.util.Log;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;
import com.dise.nefs.adapters.SectionsPagerAdapter;
import com.dise.nefs.util.Const;
import com.dise.nefs.util.OnItemSelectedListener;
import com.example.tabtest.R;

// OnItemSelectedListener is a custom interface that enables communication between Fragment and
// its Activity (see bottom of the code for more information)
public class MainActivity extends SherlockFragmentActivity implements
        OnItemSelectedListener {

    protected SharedPreferences spfReminder;
    protected SharedPreferences spfNews;
    protected SharedPreferences spfEvents;
    protected ActionBar bar;
    ViewPager mViewPager;
    SectionsPagerAdapter mSectionsPagerAdapter;

    enum TabType {
        EVENTS, NEWS, SPORTS, INFO
    }

    // Tab back stacks
    private HashMap<TabType, Stack<String>> backStacks;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mViewPager = new ViewPager(this);
        mViewPager.setId(R.id.pager);

        setContentView(mViewPager);

        // Initialize ActionBar
        ActionBar bar = getSupportActionBar();
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        mSectionsPagerAdapter = new SectionsPagerAdapter(this, mViewPager);

        mViewPager.setAdapter(mSectionsPagerAdapter);

        // Create icons
        Drawable eventIcon = getResources().getDrawable(
                R.drawable.ic_action_events);
        eventIcon.setAlpha(204);
        Drawable newsIcon = getResources().getDrawable(
                R.drawable.ic_action_news);
        newsIcon.setAlpha(77);
        Drawable sportIcon = getResources().getDrawable(
                R.drawable.ic_action_medal);
        sportIcon.setAlpha(77);
        Drawable infoIcon = getResources().getDrawable(
                R.drawable.ic_action_more);
        infoIcon.setAlpha(77);

        mSectionsPagerAdapter.addTab(bar.newTab().setText("events"),EventsFragment.class,null);
             mSectionsPagerAdapter.addTab(bar.newTab().setTag(TabType.NEWS).setIcon(newsIcon),NewsFragment.class,null);
        mSectionsPagerAdapter.addTab(bar.newTab().setTag(TabType.SPORTS).setIcon(sportIcon),SportsFragment.class,null);
        mSectionsPagerAdapter.addTab(bar.newTab().setTag(TabType.INFO).setIcon(infoIcon),InfoFragment.class,null);

}

SectionsPagerAdapter.java

        package com.dise.nefs.adapters;

    import java.util.ArrayList;
    import java.util.Locale;

    import android.content.Context;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentPagerAdapter;
    import android.support.v4.app.FragmentTransaction;
    import android.support.v4.view.ViewPager;
    import android.util.Log;

    import com.actionbarsherlock.app.ActionBar;
    import com.actionbarsherlock.app.ActionBar.Tab;
    import com.actionbarsherlock.app.SherlockFragmentActivity;
    import com.dise.nefs.ui.MainActivity;
    import com.example.tabtest.R;

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to one
     * of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter implements
            ActionBar.TabListener, ViewPager.OnPageChangeListener {

        final protected static int TABS = 4;

        private final Context mContext;
        private final ActionBar mActionBar;
        private final ViewPager mViewPager;
        private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
        protected MainActivity mActivity;

        static final class TabInfo {
            private final Class<?> clss;
            private final Bundle args;

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

        public SectionsPagerAdapter(SherlockFragmentActivity activity,
                ViewPager pager) {
            super(activity.getSupportFragmentManager());

            mContext = activity;
            mActivity = ((MainActivity) mContext);
            mActionBar = activity.getSupportActionBar();
            mViewPager = pager;
            mViewPager.setAdapter(this);
            mViewPager.setOnPageChangeListener(this);
        }

        public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {

            TabInfo info = new TabInfo(clss, args);
            tab.setTag(info);
            tab.setTabListener(this);
            mTabs.add(info);
            mActionBar.addTab(tab);
            notifyDataSetChanged();
        }

        @Override
        public void onPageSelected(int position) {
            mActionBar.setSelectedNavigationItem(position);
        }

        @Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            Object tag = tab.getTag();
            for (int i = 0; i < mTabs.size(); i++) {
                if (mTabs.get(i) == tag) {
                    mViewPager.setCurrentItem(i);
                }
            }

            mActivity.TabSelected(tab, ft);

        }

        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            mActivity.TabUnselected(tab, ft);

        }

        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft) {
            mActivity.TabReselected(tab, ft);
        }

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a PlaceholderFragment (defined as a static inner class below).

            ActionBar ab = mActivity.getSupportActionBar();

            switch (position) {

            case 0:
                return mActivity.getFragment(ab.getTabAt(0));
            case 1:
                return mActivity.getFragment(ab.getTabAt(1));
            case 2:
                return mActivity.getFragment(ab.getTabAt(2));
            case 3:
                return mActivity.getFragment(ab.getTabAt(3));

            }

            return null;

        }

        @Override
        public int getCount() {
            return mTabs.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            Locale l = Locale.getDefault();
            switch (position) {
            case 0:
                return mContext.getString(R.string.title_section1);
            case 1:
                return mContext.getString(R.string.title_section2);
            case 2:
                return mContext.getString(R.string.title_section3);
            case 3:
                return mContext.getString(R.string.title_section4);
            }
            return null;
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub

        }

    }

1 个答案:

答案 0 :(得分:2)

您正在实施错误的倾听者ActionBar.TabListener

您不能混合native和sherlock侦听器,将您的工具ActionBar.TabListener替换为: com.actionbarsherlock.app.ActionBar.TabListener