Android Studio Can't resolve fragmentActivity and ViewPager imports

匿名 (未验证) 提交于 2019-12-03 02:27:02

问题:

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) { }  } 

回答1:

  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.+" }



回答2:

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.



回答3:

  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


回答4:

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.



回答5:

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



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!