java getSupportFragmentManager().getFragments() 显示编译时错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42572249/
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
getSupportFragmentManager().getFragments() shows a compile time error
提问by Er. Kaushik Kajavadara
Calling getSupportFragmentManager().getFragments()
shows a compile time error with the message below:
调用getSupportFragmentManager().getFragments()
显示编译时错误,消息如下:
getSupportFragmentManager().getFragments() can only be called from within the same library group(groupId = com.android.support)
getSupportFragmentManager().getFragments() 只能从同一个库组内调用(groupId = com.android.support)
I have imported the following classes in MainActivity
:
我在 中导入了以下类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
extends AppCompatActivity
.
MainActivity
延伸AppCompatActivity
。
My project module level build.gradle
file is as follows:
我的项目模块级build.gradle
文件如下:
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'
}
This is the source code for the method getFragments
inside FragmentManager.java
.
这是getFragments
里面方法的源代码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();
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.
我最近将我的 Android Studio 更新到最新的稳定版本 (2.3) 并更新了 Android Gradle 插件。我认为这可能是相关的,因为我以前没有看到过这个错误。
采纳答案by ianhanniballake
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
annotationthat was added to prevent usage of private APIs.
正如FragmentManager
文档中所注意到的,getFragments()
不是应用程序可用的公共方法,而是支持库的内部实现细节,因此使用了添加的RestrictTo
注释以防止使用私有 API。
You'll want to change your code to not use getFragments
and only use the public APIs.
您需要将代码更改为不使用getFragments
而仅使用公共 API。
回答by Er. Kaushik Kajavadara
Alternative For those who may be using getFragments()
in their coding, I have replaced my code to get last fragment in backstack
to this code(I am using this code in onBackPressed()
to apply changes according to currentFragment
assumed all fragments are added to backstack):
替代方案对于那些可能getFragments()
在他们的编码中使用的人,我已经替换了我的代码以将最后一个片段backstack
添加到此代码中(我正在使用此代码onBackPressed()
根据currentFragment
假设所有片段都添加到后台堆栈来应用更改):
FragmentManager.BackStackEntry backStackEntryAt = getSupportFragmentManager().getBackStackEntryAt(getSupportFragmentManager().getBackStackEntryCount() - 1);
currentFragment = backStackEntryAt.getName();
回答by user8192276
You can use (make sure using 25.2.0or higher )
您可以使用(请确保使用25.2.0或更高版本)
supportFragmentManager.registerFragmentLifecycleCallbacks(object : FragmentManager.FragmentLifecycleCallbacks() {
override fun onFragmentAttached(fm: FragmentManager?, f: Fragment?, context: Context?) {
f?.let { fList.add(it) }
}
override fun onFragmentDetached(fm: FragmentManager?, f: Fragment?) {
f?.let { fList.remove(it) }
}
}, false)
Instead of using getFragments()
而不是使用 getFragments()