android - 无法解析类扩展 BaseAdapter 中的方法 'getBaseContext()'

标签 android firebase firebase-remote-config

我正在尝试在我的应用程序中使用 firebase 远程配置功能。但我收到错误 “无法解析方法‘getBaseContext()’”。我的类扩展了如下所示的 BaseAdapter。我仅限于使用方法,请帮助。

    public class CustomAdapter_new extends BaseAdapter {
.....
......
........
         private void initRemoteConfig() {
        mRemoteConfig = FirebaseRemoteConfig.getInstance();

        Resources res = context.getResources();

        HashMap<String, Object> defaults = new HashMap<>();
        defaults.put("claimStatusEnquiry", context.getResources().getString(R.string.claimStatusEnquiry));        defaults.put("locateYourPfOffice", context.getResources().getString(R.string.locateYourOffice));
        defaults.put("faq", context.getResources().getString(R.string.faq));

        mRemoteConfig.setDefaults(defaults);
        FirebaseRemoteConfigSettings remoteConfigSettings = new FirebaseRemoteConfigSettings.Builder()
                .setDeveloperModeEnabled(true)
                .build();
        mRemoteConfig.setConfigSettings(remoteConfigSettings);
        fetchRemoteConfigValues();
    }

    private void fetchRemoteConfigValues() {
        long cacheExpiration = 600;

        //expire the cache immediately for development mode.
        if (mRemoteConfig.getInfo().getConfigSettings().isDeveloperModeEnabled()) {
            cacheExpiration = 0;
        }

        mRemoteConfig.fetch(cacheExpiration)
                .addOnCompleteListener(this, new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(Task<Void> task) {
                        if (task.isSuccessful()) {
                            // task successful. Activate the fetched data
                            mRemoteConfig.activateFetched();

                        } else {
                            //task failed
                            Toast.makeText(getBaseContext(), "Please Connect To Internet!!", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }
    }

最佳答案

编译器不会说谎。您的类没有名为 getBaseContext() 的方法。 BaseAdapter不为您提供此方法。但是,您需要一个 Context 对象才能创建一个 Toast。有许多方法可以访问 Context。看起来你已经有了一个可用的,因为你在行中使用了它

Resources res = context.getResources();

很可能,您应该将 getBaseContext() 替换为 context:

Toast.makeText(context, ...).show();

我强烈建议您了解有关变量范围、类字段和方法参数的更多信息。这些都是 Java 中的基本概念。如果您了解所有这些是如何工作的,您将更容易编写 Android 应用程序。

关于android - 无法解析类扩展 BaseAdapter 中的方法 'getBaseContext()',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41654640/

相关文章:

android - 如何防止滚动到聚焦的 EditText

android - 应用在 Android 19 之前的 API 上显示 "NoClassDefFoundError: com.google.firebase.FirebaseOptions"

ios - 你如何制作一个必须对 firebase 数据库进行查询并将查询值作为 int 返回的函数?

swift - Firebase Remote Config/Analytics setUserProperty 条件不执行任何操作

android - 协程上下文自定义getter

android - 在 Android 上使用 omitField() 的 Proguard 和 XStream

ios - 如何将 Firebase 子条目关联到主条目? (评论中有解释)

android - Firebase 远程配置 - 检查值是否存在

firebase - "User in Random Percentile"如何在 Firebase 中工作

android - Android Studio 中的 key 存储路径是什么?