5

我试图在http://developer.android.com/training/animation/screen-slide.html中实现一个 android 项目,我在一个类中有一个错误

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.NavUtils;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class MainActivity extends FragmentActivity {
 private static final int NUM_PAGES = 5;
 private ViewPager mPager;
 private PagerAdapter mPagerAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mPager = (ViewPager) findViewById(R.id.pager);
    mPagerAdapter = new ScreenSlidePagerAdapter(getFragmentManager());
    mPager.setAdapter(mPagerAdapter);
    mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            // When changing pages, reset the action bar actions since they are dependent
            // on which page is currently active. An alternative approach is to have each
            // fragment expose actions itself (rather than the activity exposing actions),
            // but for simplicity, the activity provides the actions in this sample.
            invalidateOptionsMenu();
        }
    });
}
@SuppressLint("NewApi")
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    getMenuInflater().inflate(R.menu.activity_main, menu);

    menu.findItem(R.id.action_previous).setEnabled(mPager.getCurrentItem() > 0);

    // Add either a "next" or "finish" button to the action bar, depending on which       page
    // is currently selected.
    MenuItem item = menu.add(Menu.NONE, R.id.action_next, Menu.NONE,
            (mPager.getCurrentItem() == mPagerAdapter.getCount() - 1)
                    ? R.string.action_finish
                    : R.string.action_next);
    item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            // Navigate "up" the demo structure to the launchpad activity.
            // See http://developer.android.com/design/patterns/navigation.html for more.
            NavUtils.navigateUpTo(this, new Intent(this, MainActivity.class));
            return true;

        case R.id.action_previous:
            // Go to the previous step in the wizard. If there is no previous step,
            // setCurrentItem will do nothing.
            mPager.setCurrentItem(mPager.getCurrentItem() - 1);
            return true;

        case R.id.action_next:
            // Advance to the next step in the wizard. If there is no next step, setCurrentItem
            // will do nothing.
            mPager.setCurrentItem(mPager.getCurrentItem() + 1);
            return true;
    }

    return super.onOptionsItemSelected(item);
}


     private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
      public ScreenSlidePagerAdapter(FragmentManager fm) {
         super(fm);
     }

 public Fragment getItem(int position) {
        return ScreenSlidePageFragment.create(position);
    }


    public int getCount() {
        return NUM_PAGES;
    }
}
    }

我正在 android 4.2 中构建项目。

编辑:我已将代码更改为

private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
    public ScreenSlidePagerAdapter(android.support.v4.app.FragmentManager fm) {
        super(fm);
    }

 @Override
    public android.support.v4.app.Fragment getItem(int position) {
        return Firstframe.create(position);
    }

然后发生错误

mPagerAdapter = new ScreenSlidePagerAdapter(getFragmentManager());

错误 :The constructor MainActivity.ScreenSlidePagerAdapter(FragmentManager) is undefined

4

4 回答 4

12

而不是这个:

mPagerAdapter = new ScreenSlidePagerAdapter(getFragmentManager());

做这个:

mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());

因为

getFragmentManager()搭配import android.app.FragmentManager

getSupportFragmentManager()搭配android.support.v4.app.FragmentManager

于 2013-02-27T23:31:11.177 回答
2

将您的 FragmentManager 更改为 android.support.v4.app.FragmentManager。导入

android.support.v4.app.FragmentStatePagerAdapter;ScreenSlideActivity.java的以下代码 :

公共类 ScreenSlideActivity 扩展 FragmentActivity {

private static final int NUM_PAGES = 5;

private ViewPager mPager;


private PagerAdapter mPagerAdapter;

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

    // Instantiate a ViewPager and a PagerAdapter.
    mPager = (ViewPager) findViewById(R.id.pager);
    mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
    mPager.setAdapter(mPagerAdapter);
    mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {

            invalidateOptionsMenu();
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    getMenuInflater().inflate(R.menu.activity_screen_slide, menu);

    menu.findItem(R.id.action_previous).setEnabled(mPager.getCurrentItem() > 0);


    MenuItem item = menu.add(Menu.NONE, R.id.action_next, Menu.NONE,
            (mPager.getCurrentItem() == mPagerAdapter.getCount() - 1)
                    ? R.string.action_finish
                    : R.string.action_next);
    item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:

            NavUtils.navigateUpTo(this, new Intent(this, MainActivity.class));
            return true;

        case R.id.action_previous:

            mPager.setCurrentItem(mPager.getCurrentItem() - 1);
            return true;

        case R.id.action_next:

            mPager.setCurrentItem(mPager.getCurrentItem() + 1);
            return true;
    }

    return super.onOptionsItemSelected(item);
}


private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
    public ScreenSlidePagerAdapter(android.support.v4.app.FragmentManager fm) {
        super(fm);
    }

    @Override
    public android.support.v4.app.Fragment getItem(int position) {
        return ScreenSlidePageFragment.create(position);
    }

    @Override
    public int getCount() {
        return NUM_PAGES;
    }
}}

ScreenSlidePageFragment.java的以下代码 :

公共类 ScreenSlidePageFragment 扩展片段 {

public static final String ARG_PAGE = "page";


private int mPageNumber;


public static android.support.v4.app.Fragment create(int pageNumber) {
    android.support.v4.app.Fragment fragment = new android.support.v4.app.Fragment();
    Bundle args = new Bundle();
    args.putInt(ARG_PAGE, pageNumber);
    fragment.setArguments(args);
    return fragment;
}

public ScreenSlidePageFragment() {
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mPageNumber = getArguments().getInt(ARG_PAGE);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    ViewGroup rootView = (ViewGroup) inflater
            .inflate(R.layout.fragment_screen_slide_page, container, false);

    // Set the title view to show the page number.
    ((TextView) rootView.findViewById(android.R.id.text1)).setText(
            getString(R.string.title_template_step, mPageNumber + 1));

    return rootView;
}



public int getPageNumber() {
    return mPageNumber;
}}
于 2014-10-07T05:22:19.347 回答
0
  #For Full code

package com.team.test;

    import android.content.Intent;
    import android.content.pm.ActivityInfo;
    import android.os.Bundle;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.app.FragmentStatePagerAdapter;
    import android.support.v4.app.NavUtils;
    import android.support.v4.view.PagerAdapter;
    import android.support.v4.view.ViewPager;
    import android.view.Menu;
    import android.view.MenuItem;

    /**
     * Demonstrates a "screen-slide" animation using a {@link ViewPager}. Because
     * {@link ViewPager} automatically plays such an animation when calling
     * {@link ViewPager#setCurrentItem(int)}, there isn't any animation-specific
     * code in this sample.
     * 
     * <p>
     * This sample shows a "next" button that advances the user to the next step in
     * a wizard, animating the current screen out (to the left) and the next screen
     * in (from the right). The reverse animation is played when the user presses
     * the "previous" button.
     * </p>
     * 
     * @see ScreenSlidePageFragment
     */
    public class MenuActivity extends FragmentActivity {
        /**
         * The number of pages (wizard steps) to show in this demo.
         */
        private static final int NUM_PAGES = 6;

        /**
         * The pager widget, which handles animation and allows swiping horizontally
         * to access previous and next wizard steps.
         */
        private ViewPager mPager;

        /**
         * The pager adapter, which provides the pages to the view pager widget.
         */
        private PagerAdapter mPagerAdapter;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            setContentView(R.layout.activity_screen_slide);

            // Instantiate a ViewPager and a PagerAdapter.
            mPager = (ViewPager) findViewById(R.id.pager);
            mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
            mPager.setAdapter(mPagerAdapter);
            mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    // When changing pages, reset the action bar actions since they
                    // are dependent
                    // on which page is currently active. An alternative approach is
                    // to have each
                    // fragment expose actions itself (rather than the activity
                    // exposing actions),
                    // but for simplicity, the activity provides the actions in this
                    // sample.
                    invalidateOptionsMenu();
                }
            });
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            super.onCreateOptionsMenu(menu);
            getMenuInflater().inflate(R.menu.activity_screen_slide, menu);

            menu.findItem(R.id.action_previous).setEnabled(
                    mPager.getCurrentItem() > 0);

            // Add either a "next" or "finish" button to the action bar, depending
            // on which page
            // is currently selected.
            MenuItem item = menu
                    .add(Menu.NONE,
                            R.id.action_next,
                            Menu.NONE,
                            (mPager.getCurrentItem() == mPagerAdapter.getCount() - 1) ? R.string.action_finish
                                    : R.string.action_next);
            item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM
                    | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
            case android.R.id.home:
                // Navigate "up" the demo structure to the launchpad activity.
                // See http://developer.android.com/design/patterns/navigation.html
                // for more.
                NavUtils.navigateUpTo(this, new Intent(this, Login.class));
                return true;

            case R.id.action_previous:
                // Go to the previous step in the wizard. If there is no previous
                // step,
                // setCurrentItem will do nothing.
                mPager.setCurrentItem(mPager.getCurrentItem() - 1);
                return true;

            case R.id.action_next:
                // Advance to the next step in the wizard. If there is no next step,
                // setCurrentItem
                // will do nothing.
                mPager.setCurrentItem(mPager.getCurrentItem() + 1);
                return true;
            }

            return super.onOptionsItemSelected(item);
        }

        /**
         * A simple pager adapter that represents 5 {@link ScreenSlidePageFragment}
         * objects, in sequence.
         */
        public class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
            public ScreenSlidePagerAdapter(android.support.v4.app.FragmentManager fm) {
                super(fm);
            }

            @Override
            public android.support.v4.app.Fragment getItem(int position) {
                return ScreenSlidePageFragment.create(position);
            }

            @Override
            public int getCount() {
                return NUM_PAGES;
            }
        }
    }
于 2014-05-06T10:47:24.723 回答
0

正如我通过您的代码看到的那样,我看到FragmentManager您的构造函数中的类与您在代码中导入的类不兼容,import android.app.FragmentManager并且android.support.v4.app.FragmentManager

只需检查FragmentManager在这种情况下您真正想要使用的类,您的问题就应该得到解决。谢谢。

IE。根据您的决定选择:

public ScreenSlidePagerAdapter(android.app.FragmentManager fm) {
        super(fm);
    }

或者

public ScreenSlidePagerAdapter(android.support.v4.app.FragmentManager fm) {
        super(fm);
    }

并使用适当的类在您的类中导入MainActivity

于 2013-02-19T06:21:37.150 回答