当前位置:  开发笔记 > 编程语言 > 正文

Android TabLayout setOnTabSelectedListener无法正常工作

如何解决《AndroidTabLayoutsetOnTabSelectedListener无法正常工作》经验,为你挑选了0个好方法。

假设我有一个MainActivity类,其中TabLayoutand和ViewPager用相应的4页初始化。

MainActivity.class

public class MainActivity extends AppCompatActivity {
    private SectionsPagerAdapter mSectionsPagerAdapter;
    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        // TabLayout
        TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        tabLayout.setupWithViewPager(mViewPager);
    }

    public class SectionsPagerAdapter extends FragmentPagerAdapter {
        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }
        @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).
            // return PlaceholderFragment.newInstance(position + 1);
            switch (position) {
                case 0:
                    ActivityZero tab0 = new ActivityZero();
                    return tab0;
                case 1:
                    ActivityOne tab1 = new ActivityOne();
                    return tab1;
                case 2:
                    ActivityTwo tab2 = new ActivityTwo();
                    return tab2;
                case 3:
                    ActivityThree tab3 = new ActivityThree();
                    return tab3;
                default:
                    return null;
            }
        }
        @Override
        public int getCount() {
            // Show 4 total pages.
            return 4;
        }
        //Setting the ICON
        private int[] imageResId = {
                R.drawable.ic_zero
                R.drawable.ic_one,
                R.drawable.ic_two,
                R.drawable.ic_three
        };
        @Override
        public CharSequence getPageTitle(int position) {
            Drawable image = ContextCompat.getDrawable(getApplicationContext(), imageResId[position]);
            image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight());
            SpannableString sb = new SpannableString(" ");
            ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BOTTOM);
            sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            return sb;
        }
    }
}

一切工作正常与上面的代码,但是当我添加setOnTabSelectedListeneronCreate此:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        // TabLayout
        TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        tabLayout.setupWithViewPager(mViewPager);

        //This is what i'm trying to add
        mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mViewPager.setCurrentItem(tab.getPosition());
                tab.getIcon().setAlpha(255);
            }
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                tab.getIcon().setAlpha(127);
            }
            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
    }

运行时,一切正常,但是当我滑动以更改选项卡时,该应用程序将不再响应并开始崩溃。

我正在尝试获取选定的标签,position并在上设置iconwith ,但没有运气。setAlpha(127)selected page

注意:我正在使用支持库,当前的API是8。

更新:

12-04 11:32:12.342 11884-11884/com.sample.app.mysampleapp E/InputEventReceiver: Exception dispatching input event.
12-04 11:32:12.342 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI: java.lang.NullPointerException
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at com.sample.app.mysampleapp.MainActivity$1.onTabUnselected(MainActivity.java:53)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.support.design.widget.TabLayout.selectTab(TabLayout.java:867)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.support.design.widget.TabLayout$TabLayoutOnPageChangeListener.onPageSelected(TabLayout.java:1759)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.support.v4.view.ViewPager.dispatchOnPageSelected(ViewPager.java:1794)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:569)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:553)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.support.v4.view.ViewPager.onTouchEvent(ViewPager.java:2102)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.View.dispatchTouchEvent(View.java:7396)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2462)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2195)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2468)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2210)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2468)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2210)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2468)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2210)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2468)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2210)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2468)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2210)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2468)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2210)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2177)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1482)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.app.Activity.dispatchTouchEvent(Activity.java:2470)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2125)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.View.dispatchPointerEvent(View.java:7581)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3571)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3503)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4614)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4592)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4696)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.os.MessageQueue.nativePollOnce(Native Method)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.os.MessageQueue.next(MessageQueue.java:125)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.os.Looper.loop(Looper.java:138)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.app.ActivityThread.main(ActivityThread.java:4947)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at java.lang.reflect.Method.invokeNative(Native Method)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at java.lang.reflect.Method.invoke(Method.java:511)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at dalvik.system.NativeStart.main(Native Method)
12-04 11:32:12.412 11884-11884/com.sample.app.mysampleapp D/AndroidRuntime: Shutting down VM
12-04 11:32:12.412 11884-11884/com.sample.app.mysampleapp W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x4131e2a0)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime: FATAL EXCEPTION: main
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime: java.lang.NullPointerException
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at com.sample.app.mysampleapp.MainActivity$1.onTabUnselected(MainActivity.java:53)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.support.design.widget.TabLayout.selectTab(TabLayout.java:867)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.support.design.widget.TabLayout$TabLayoutOnPageChangeListener.onPageSelected(TabLayout.java:1759)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.support.v4.view.ViewPager.dispatchOnPageSelected(ViewPager.java:1794)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:569)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:553)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.support.v4.view.ViewPager.onTouchEvent(ViewPager.java:2102)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.View.dispatchTouchEvent(View.java:7396)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2462)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2195)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2468)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2210)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2468)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2210)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2468)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2210)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2468)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2210)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2468)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2210)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2468)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2210)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2177)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1482)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.app.Activity.dispatchTouchEvent(Activity.java:2470)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2125)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.View.dispatchPointerEvent(View.java:7581)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3571)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3503)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4614)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4592)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4696)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.os.MessageQueue.nativePollOnce(Native Method)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.os.MessageQueue.next(MessageQueue.java:125)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:138)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:4947)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:511)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)

有人可以帮我吗?非常感谢你。

推荐阅读
我我檬檬我我186
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有