android.support.v4.app.FragmentTransaction无法转换为片段管理器

时间:2015-11-21 10:44:10

标签: android tabs navigation-drawer swipeview

我试图在我的应用中创建navigation drawer。在我的navigation drawer itemView之一中,我希望它有2 tabs具有swipe view功能。我可以单独创建它们,但是当我尝试在1中进行时,我会收到错误。

MainActivity //用于导航抽屉项目

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");
        }
    }
}

信息

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;

      public void onClick(View arg0) {
            FragmentTransaction ft = getFragmentManager().beginTransaction(); 
            dialog.show(ft, "DatePicker"); //cannot resolve method show

        }

      public static class DateDialog extends android.app.DialogFragment implements DatePickerDialog.OnDateSetListener {
            //  EditText txtdate;
            T t;


            public DateDialog() {

            }



            public Dialog onCreateDialog(Bundle savedInstanceState)
            {

                final Calendar c=Calendar.getInstance();
                int year=c.get(Calendar.YEAR);
                int month=c.get(Calendar.MONTH);
                int day=c.get(Calendar.DAY_OF_MONTH);
                return new DatePickerDialog(getActivity(),this,year,month,day);
            }

            public void onDateSet(DatePicker view,int year, int month, int day)
            {
                String date=day+"-"+(month+1)+"-"+year;
                txtDate.setText(date);
                date1= txtDate.getText().toString();
                return ;
                //t.add(date);


            }
        }

ViewView

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;

public class ViewView extends Fragment implements ActionBar.TabListener { // must either be declared abstract or...

    private ViewPager viewPager;
    private TabsFragmentPagerAdapter mAdapter;
    private ActionBar actionBar;
    private String[] tabs = {"Information", "receipt"}; // This are the two tabs in view item

  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View View1 = inflater.inflate(R.layout.viewview1, container, false);
        listView = (ListView) View1.findViewById(R.id.listView1);
        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;
    }

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

}

错误

  

错误:(85,15)错误:找不到合适的方法   show(android.support.v4.app.FragmentTransaction,String)方法   DialogFragment.show(FragmentManager,String)不适用   (参数不匹配; android.support.v4.app.FragmentTransaction不能   被转换为FragmentManager)方法   DialogFragment.show(android.app.FragmentTransaction,String)不是   适用(论证不匹配;   android.support.v4.app.FragmentTransaction无法转换为   android.app.FragmentTransaction)

     

错误:(28,9)错误:ViewView不是抽象的,也不会覆盖   抽象方法onTabReselected(Tab,FragmentTransaction)中   TabListener

1 个答案:

答案 0 :(得分:4)

您收到此错误是因为 MainActivity ViewView 活动正在使用

import android.app.Fragment;
import android.app.FragmentManager;

TabsFragmentPagerAdapter 正在使用

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

在所有活动中使用其中任何一项。