The import android.support.v13.app.FragmentActivity cannot be resolved

后端 未结 4 1346
旧巷少年郎
旧巷少年郎 2020-12-06 07:41

I\'m testing an android project (http://developer.android.com/training/animation/screen-slide.html) and I have an error in one class.

/*
 * Copyright 2012 Th         


        
相关标签:
4条回答
  • 2020-12-06 07:54

    I think i should explain the difference...

    if you want to support api <11. Then you should import android.support.v4.app.FragmentPagerAdapter; and then create all Fragment by import android.support.v4.app.Fragment; and you are done.

    API==11

    If you don't want to support api <11. or you simply want to use simple Fragment(without support) then you should import android.support.v13.app.FragmentPagerAdapter; then getItem will need a simple Fragment.

    @Override
        public android.app.Fragment getItem(int position) {
            return ScreenSlidePagerAdapter.create(position);
        }
    
    0 讨论(0)
  • 2020-12-06 08:00

    Here is a full code for further reference

    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;
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-06 08:05

    I was blocked on this for a while and just managed to fix it,

    Try using getSupportFragmentManager() instead of getFragmentManager().

    0 讨论(0)
  • 2020-12-06 08:11

    Change the "import android.support.v13.app.FragmentActivity" to "import android.support.v4.app.FragmentActivity"

    For the undefined part, try this syntax:

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

    and:

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

    Original suggestion: Have you added the libraries to your build path? Just right-click on the project and the menu item "Java Build Path" will be listed. Select this and add the libraries to your path.

    0 讨论(0)
提交回复
热议问题