2

I'm trying to create navigation drawer in my app. In one of my navigation drawer item, View , I want it to has 2 tabs with swipe view feature. I get error Viewview is not abstract and does not override abstract method onTabReselected.

MainActivity // for navigation drawer item

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;

private void selectItem(int position) {

        Fragment fragment = null;

        switch (position) {
            case 0:
                fragment=new Information();
                break;
            case 1:

                fragment=new Claims1();
                Bundle bundle=new Bundle();
                bundle.putLong("ab",WorkDetailsTable.ab);
                fragment.setArguments(bundle);
                break;

            case 2:
                fragment=new ViewView();
                break;

            default:
                break;
        }

        if (fragment != null) {
            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit(); 

            mDrawerList.setItemChecked(position, true);
            mDrawerList.setSelection(position);
            setTitle(mNavigationDrawerItemTitles[position]);
            mDrawerLayout.closeDrawer(mDrawerList);

        } else {
            Log.e("MainActivity", "Error in creating fragment");
        }
    }
}

ViewView

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.AdapterView;

public class ViewView extends Fragment implements ActionBar.TabListener {

    private ViewPager viewPager;
    private TabsFragmentPagerAdapter mAdapter;
    private ActionBar actionBar;
    private String[] tabs = {"Information", "receipt"};


    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View View1 = inflater.inflate(R.layout.viewview1, container, false);
        viewPager = (ViewPager) View1.findViewById(R.id.pager);
        mAdapter = new TabsFragmentPagerAdapter(getActivity().getSupportFragmentManager());
        viewPager.setAdapter(mAdapter);
        actionBar = getActivity().getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        for (int i = 0; i < 2; i++) {
            actionBar.addTab(actionBar.newTab().setText(tabs[i]).setTabListener(this));
        }
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg) {
                // TODO Auto-generated method stub
                actionBar.setSelectedNavigationItem(arg);
            }

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

            }

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

            }


        });


        BuildList();
        return View1;
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getActivity().getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    public void onTabReselected(ActionBar.Tab arg0, FragmentTransaction arg1) {
        // TODO Auto-generated method stub

    }


    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction arg1) {
        // TODO Auto-generated method stub
        viewPager.setCurrentItem(tab.getPosition());
    }


    public void onTabUnselected(ActionBar.Tab arg0, FragmentTransaction arg1) {
        // TODO Auto-generated method stub
    }
}

TabsFragmentPagerAdapter

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class TabsFragmentPagerAdapter extends FragmentPagerAdapter {

    public TabsFragmentPagerAdapter(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub
    }

    @Override
    public Fragment getItem(int index) {
        // TODO Auto-generated method stub

        switch(index) {
            case 0:
                return new UpdatePage2();
            case 1:
                return new Receipt();

        }
        return null;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 2;
    }

}

Error

Error:(50, 47) error: incompatible types: android.app.ActionBar cannot be converted to android.support.v7.app.ActionBar

Tony
  • 2,515
  • 14
  • 38
  • 71

1 Answers1

1

Well, the error message is clear. Your ViewView class implements TabListener but doesn't contain implementation of onTabReselected. You have an implementation of onTabReselected in the anonymous class instance implementing ViewPager.OnPageChangeListener(), which you define in your onCreateView method.

The question is do you need ViewView to implement TabListener. If you do, add the methods required by TabListener directly to ViewView. If not, remove the implements ActionBar.TabListener clause.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • ya, I need. Did you mean add `ViewPager.OnPageChangeListener()` in my `onCreateView` method? – Tony Nov 21 '15 at 12:50
  • How to add TabListener directly to Viewview ? – Tony Nov 21 '15 at 12:53
  • @Tony `ViewView` doesn't contain an implementation of `onTabReselcted`. It must contain such an implementation if you wish it to implement `TabListener`. – Eran Nov 21 '15 at 12:54
  • @Tony put `onTabReselected` outside your `onCreateView` method. – Eran Nov 21 '15 at 12:54
  • After `onPageSelected`, everything move outside or just `onTabReselected`? – Tony Nov 21 '15 at 12:56
  • @Tony That depends on whether the other methods are also required by the `TabListener` interface. – Eran Nov 21 '15 at 12:57
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/95795/discussion-between-tony-and-eran). – Tony Nov 21 '15 at 12:57