本文介绍了getSupportFragmentManager().getFragments() 显示编译时错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

调用 getSupportFragmentManager().getFragments() 会显示编译时错误,并显示以下消息:

Calling getSupportFragmentManager().getFragments() shows a compile time error with the message below:

getSupportFragmentManager().getFragments() 只能从在同一个库组内(groupId = com.android.support)

我在 MainActivity 中导入了以下类:

I have imported the following classes in MainActivity:

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MenuItem;
import android.widget.Toast;

MainActivity 扩展 AppCompatActivity.

我的项目模块级build.gradle文件如下:

My project module level build.gradle file is as follows:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.mycompany.floatingdemo"
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.2.0'
    compile 'com.android.support:design:25.2.0'
    compile 'com.android.support:support-vector-drawable:25.2.0'
    testCompile 'junit:junit:4.12'
}

这是FragmentManager.java 中方法getFragments 的源代码.

This is the source code for the method getFragments inside FragmentManager.java.

/**
 * Get a list of all fragments that have been added to the fragment manager.
 *
 * @return The list of all fragments or null if none.
 * @hide
 */
@RestrictTo(LIBRARY_GROUP)
public abstract List<Fragment> getFragments();

我最近将我的 Android Studio 更新到最新的稳定版本 (2.3) 并更新了 Android Gradle 插件.我认为这可能是相关的,因为我以前没有见过这个错误.

I have recently updated my Android Studio to the latest stable version (2.3) and updated the Android Gradle plugin as well. I think this may be relevant because I have not previously seen this error.

推荐答案

FragmentManager 文档getFragments() 不是应用程序可用的公共方法,而是支持库的内部实现细节,因此RestrictTo 注释的使用添加的目的是防止使用私有 API.

As noticeable in the FragmentManager documentation, getFragments() is not a public method available to apps, but an internal implementation detail of the Support Library, hence the use of the RestrictTo annotation that was added to prevent usage of private APIs.

您需要将代码更改为不使用 getFragments 而仅使用公共 API.

You'll want to change your code to not use getFragments and only use the public APIs.

这篇关于getSupportFragmentManager().getFragments() 显示编译时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 06:55