getSupportFragmentManager()。getFragments()显示编译时错误

浏览:41日期:2024-02-22
如何解决getSupportFragmentManager()。getFragments()显示编译时错误?

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

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

解决方法

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

getSupportFragmentManager()。getFragments()只能在同一库组中调用(groupId =com.android.support)

我在中导入了以下类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文件如下:

apply plugin: ’com.android.application’android { compileSdkVersion 25 buildToolsVersion '25.0.2' defaultConfig {applicationId 'com.mycompany.floatingdemo'minSdkVersion 16targetSdkVersion 25versionCode 1versionName '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’}

这是getFragmentsinside 方法的源代码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),并且还更新了AndroidGradle插件。我认为这可能是相关的,因为我之前从未见过此错误。

相关文章: