实现ViewPager的Android错误

时间:2014-03-22 09:12:51

标签: android android-fragments android-viewpager

我正在尝试创建一个视图寻呼机,但我陷入了无限循环的修复,因此Eclipse更改了某些东西,使其他东西中断,然后恢复到原始错误。

我一直关注this tutorial,但似乎无法让它发挥作用任何人都可以帮我找到我错的地方?

这是我到目前为止所做的:

import android.support.v4.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;


public class Menu extends FragmentActivity {
        // ...
    FragmentPagerAdapter adapterViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_level_pager);
        ViewPager pager = (ViewPager) findViewById(R.id.pager);
        adapterViewPager = new MyPagerAdapter(getSupportFragmentManager());
        pager.setAdapter(adapterViewPager);
    }


        public static class MyPagerAdapter extends FragmentPagerAdapter {
        private static int NUM_ITEMS = 3;

            public MyPagerAdapter(android.support.v4.app.FragmentManager fragmentManager) {
                super(fragmentManager);
            }

            // Returns total number of pages
            @Override
            public int getCount() {
                return NUM_ITEMS;
            }

            // Returns the fragment to display for that page
            @Override
            public MyFragmentAdapter getItem(int position) {
                switch (position) {
                case 0: // Fragment # 0 - This will show FirstFragment
                    return  MyFragmentAdapter.newInstance(0, "Page # 1");
                case 1: // Fragment # 0 - This will show FirstFragment different title
                    return MyFragmentAdapter.newInstance(1, "Page # 2");
                case 2: // Fragment # 1 - This will show SecondFragment
                    return MyFragmentAdapter.newInstance(2, "Page # 3");
                default:
                    return null;
                }
            }

            // Returns the page title for the top indicator
            @Override
            public CharSequence getPageTitle(int position) {
                return "Page " + position;
            }

        }

    }

这是MyFragmentAdapter

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MyFragmentAdapter extends Fragment {
        // Store instance variables
        private String title;
        private int page;

        // newInstance constructor for creating fragment with arguments
        public static MyFragmentAdapter newInstance(int page, String title) {
            MyFragmentAdapter fragment = new MyFragmentAdapter();
            Bundle args = new Bundle();
            args.putInt("someInt", page);
            args.putString("someTitle", title);
            fragment.setArguments(args);
            return fragment;
        }

        // Store instance variables based on arguments passed
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            page = getArguments().getInt("someInt", 0);
            title = getArguments().getString("someTitle");
        }

        // Inflate the view for the fragment based on layout XML
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.layout_level_select, container, false);

            return view;
        }
    }

这是我得到的错误:

The return type is incompatible with FragmentPagerAdapter.getItem(int) Menu.java line 42

2 个答案:

答案 0 :(得分:3)

使用

import android.support.v4.app.FragmentManager 

而不是

import android.app.FragmentManager; 
中的

import android.support.v4.app.Fragment; 

而不是

import android.app.Fragment;
MyFragmentAdapter

中的

答案 1 :(得分:2)

在我看来,问题出在MyFragmentAdapter的自动生成导入中。在MyFragmentAdapter中,您正在使用import android.app.Fragment;,同时尝试在菜单类中使用import android.support.v4.app.Fragment;引用它。  因此,我的建议是更改MyFragmentAdapter中片段的引用。 :)