java Android Studio 无法解析 fragmentActivity 和 ViewPager 导入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27012147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:03:51  来源:igfitidea点击:

Android Studio Can't resolve fragmentActivity and ViewPager imports

javaandroidandroid-fragmentsandroid-studio

提问by FuCloud Sam

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.

Android Studio 显示我无法解析符号“ViewPager”和“FragmentActivity”。如何解决这个问题?谢谢。

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

}

回答by SubinM

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

  1. 确保您已使用 SDK Manager 下载了 Android Support Repository。
  2. 打开应用程序的 build.gradle 文件。
  3. 将支持库添加到依赖项部分。例如,要添加 v4 支持库,请添加以下几行:

    依赖{ ...编译“com.android.support:support-v4:18.0.+”}

回答by FuCloud Sam

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.

我已经解决了。我安装了所有东西,但我没有将外部库导入到我的库中。在创建新项目期间它没有自动安装。所以我只是打开了项目结构并导入了依赖项 -> 添加 support-v4 库。

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

顺便说一句,感谢你们帮助我很多并将建议发布给我。

回答by beginning

  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
  1. 单击文件,然后选择项目结构
  2. 选择模块“应用程序”
  3. 单击“依赖项”选项卡
  4. 单击 + 号,选择库依赖项
  5. 根据需要选择 support-v4 或其他库

回答by Michael Yang

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

如果您已经安装了所有工具并对其进行了配置,请尝试在您的 IDE 菜单中这样做:Build-> Clean Project

回答by Ashwin Narayanan

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

要将 Android 支持库添加到现有的 Android 项目:

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

右键单击您的项目 选择 Android 工具 选择添加支持库。

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

通常即使通过SDK管理器添加后,您也必须在此过程中单独添加。这将设置所需的jar文件。