【问题标题】:getSupportFragmentManager().getFragments() shows a compile time errorgetSupportFragmentManager().getFragments() 显示编译时错误
【发布时间】:2017-07-23 04:07:58
【问题描述】:

调用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 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 的源代码。

/**
 * 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 插件。我认为这可能是相关的,因为我以前没有看到过这个错误。

【问题讨论】:

  • 请在您使用getSupportFragmentManager().getFragments()的地方发布代码
  • 而不是 getSupportFragmentManager 尝试 getFragmentManager 因为在您的导入语句中片段管理器就在那里。试一试
  • @NiteshMishra,也就是 SupportFragmentManager,只是你可以看到它来自支持库 v4。 import android.support.v4.app.FragmentManager;

标签: java android compiler-errors


【解决方案1】:

正如FragmentManager documentation 中所注意到的,getFragments() 不是应用程序可用的公共方法,而是支持库的内部实现细节,因此使用添加的 RestrictTo annotation 以防止使用私有 API .

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

【讨论】:

  • 是的,但是这个限制背后的原因是什么?
  • 由于getFragments()现在受到限制,获取当前添加到FragmentManager的所有Fragments列表的“公共API”是什么?
  • @jenzz - 没有 API。您当然可以注册 FragmentLifecycleCallbacks 并保留自己的列表。
  • 这没什么意义。到目前为止还没有工作吗?
  • 为什么FragmentLifecycleCallbacks 是抽象的而不是接口?
【解决方案2】:

替代方案对于那些可能在他们的编码中使用getFragments() 的人,我已经替换了我的代码以获取backstack 中的最后一个片段到此代码(我在onBackPressed() 中使用此代码以根据@987654324 应用更改@假设所有片段都添加到backstack):

FragmentManager.BackStackEntry backStackEntryAt = getSupportFragmentManager().getBackStackEntryAt(getSupportFragmentManager().getBackStackEntryCount() - 1);
currentFragment = backStackEntryAt.getName();

【讨论】:

  • 如果你有一个浏览器,getBackStackEntryCount() 将是空的或者没有计算碎片。
【解决方案3】:

你可以使用(确保使用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) 

而不是使用getFragments()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-29
    • 2017-09-22
    • 2021-07-17
    • 1970-01-01
    • 1970-01-01
    • 2022-11-29
    相关资源
    最近更新 更多