5

I follow the tutorial of developing swipe-able tabs. When I import:

import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;

Android Studio shows me cannot resolve the symbol "ViewPager" and "FragmentActivity". How to solve this? thanks.

Below is the entire code of my project.

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;

public class MainActivity extends FragmentActivity implements
    ActionBar.TabListener {

private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Top Rated", "Games", "Movies" };

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

    // Initilization
    viewPager = (ViewPager) findViewById(R.id.pager);
    actionBar = getActionBar();
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

    viewPager.setAdapter(mAdapter);
    actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Adding Tabs
    for (String tab_name : tabs) {
        actionBar.addTab(actionBar.newTab().setText(tab_name)
                .setTabListener(this));
    }

    /**
     * on swiping the viewpager make respective tab selected
     * */
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            // on changing the page
            // make respected tab selected
            actionBar.setSelectedNavigationItem(position);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    });
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    // on tab selected
    // show respected fragment view
    viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}

}
Makwana
  • 70
  • 10
FuCloud Sam
  • 99
  • 1
  • 2
  • 10

6 Answers6

6
  1. Make sure you have downloaded the Android Support Repository using the SDK Manager.
  2. Open the build.gradle file for your application.
  3. Add the support library to the dependencies section. For example, to add the v4 support library, add the following lines:

    dependencies { ... compile "com.android.support:support-v4:18.0.+" }

SubinM
  • 394
  • 3
  • 7
  • I am sure I am downloaded the android support repository and also android support library. I also add the line: compile "com.android.support:support-4:18.0.+" but the android studio still keep asking me go and install the android support repository. – FuCloud Sam Nov 19 '14 at 16:46
  • Gradle wont be able to download the jar, if your network is blocked by any proxy. If so you can work in offline mode. Add the jar to the libs folder in your module, right click the jar and select 'Add as Library' option. – SubinM Nov 19 '14 at 18:12
3

I have solved it. I installed everything but I did not import the external library into my library. It was not installed automatically during creation of the new project. So I just opened the project structure and imported the dependencies -> add support-v4 library.

Btw, thanks you guys for helping me a lot and posting the suggestion to me.

Dan
  • 5,153
  • 4
  • 31
  • 42
FuCloud Sam
  • 99
  • 1
  • 2
  • 10
1
  1. Click on File, then select Project Structure
  2. Choose Modules "app"
  3. Click "Dependencies" tab
  4. Click on the + sign, choose Library Dependencies
  5. Select support-v4 or other libraries as needed
  6. OK
rainy
  • 1,577
  • 1
  • 19
  • 27
0

To add the Android Support Library to an existing Android Project:

Right click on your project Select Android Tools Select Add Support Library.

Usually even after you add through SDK manager,you have to individually add in this process.This will set up the jar files needed.

0

If you had installed all tools and configed it, Try to do like this in your IDE menu : Build-> Clean Project

Michael Yang
  • 1,403
  • 2
  • 18
  • 27
0

Do as written in SubinM's post

After that, change in all XML files

from

<androidx.core.view.ViewPager

to

<androidx.viewpager.widget.ViewPager

Magnus
  • 83
  • 1
  • 7