返回类型与FragmentPagerAdapter.getItem(int)不兼容更多错误

时间:2014-04-19 19:20:13

标签: java android android-fragments

我收到的返回类型与代码底部的public void Fragment getItem(int position)的FragmentPagerAdapter.getItem(int)不兼容。错误来自Fragment。

我已经找到了一些关于此错误的相关主题,我试图解决这个问题,但似乎没有解决错误,或者它修复了一个错误,然后打开了其他5个错误,我尝试修复这些错误打开了30个错误。有任何想法吗?

我更改了导入android.app.Fragment;导入android.support.v4.app.Fragment;和其他一些导入更改,并将其归结为这一个错误。任何帮助将不胜感激。谢谢!

我包含了大部分代码,因为我不确定我是否在别处犯错误。我认为这与进口有关,但我尝试了许多差异组合而没有运气。

package com.example.hellofragsandwich;

import java.util.Locale;

import android.app.Activity;
import android.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v4.app.FragmentPagerAdapter;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MainActivity extends FragmentActivity implements ActionBar.TabListener, Communicator {


    public final int TOTAL_FRAGMENTS=7;
    Fragment fragment_0 = new Fragment_0();
    Fragment fragment_1 = new Fragment_1();
    Fragment fragment_2 = new Fragment_2();
    Fragment fragment_3 = new Fragment_3();
    Fragment fragment_4 = new Fragment_4();
    Fragment fragment_5 = new Fragment_5();


    SectionsPagerAdapter mSectionsPagerAdapter;


    ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Set up the action bar.
        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

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

        // When swiping between different sections, select the corresponding
        // tab. We can also use ActionBar.Tab#select() to do this if we have
        // a reference to the Tab.
        mViewPager
                .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {
                        actionBar.setSelectedNavigationItem(position);
                    }
                });

        // For each of the sections in the app, add a tab to the action bar.
        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
            // Create a tab with text corresponding to the page title defined by
            // the adapter. Also specify this Activity object, which implements
            // the TabListener interface, as the callback (listener) for when
            // this tab is selected.
            actionBar.addTab(actionBar.newTab()
                    .setText(mSectionsPagerAdapter.getPageTitle(i))
                    .setTabListener(this));
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab,
            FragmentTransaction fragmentTransaction) {
        // When the given tab is selected, switch to the corresponding page in
        // the ViewPager.
        mViewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab,
            FragmentTransaction fragmentTransaction) {
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab,
            FragmentTransaction fragmentTransaction) {
    }

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    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).
            Fragment fragment = null;
            switch (position) {
            case 0:
                return fragment_0;
            case 1:
                return fragment_1;      
            case 2:
                return fragment_2;
            case 3:
                return fragment_3;
            case 4:
                return fragment_4;      
            case 5:
                return fragment_5;
            case 6:
                return fragment_6;
            }
            return fragment;

        }


}

1 个答案:

答案 0 :(得分:3)

如果您使用v13支持库中的import android.support.v13.app.FragmentPagerAdapter;,则应使用android.app.Fragment而不是v4。 (使用v13,你应该使用getFragmentManager())

或者如果你想使用android.support.v4.app.Fragment,你应该使用android.support.v4.app.FragmentPagerAdapter而不是v13一个(使用v4片段你应该使用getSupportFragmentManager())