嵌套枚举条目

时间:2019-04-06 09:57:21

标签: android kotlin

我编写了一个代码,该代码在我的应用程序中显示Eu同意书(最初使用Java而不是在我的kotlin项目中复制),在Java中效果很好,但在kotlin中却给了我“通过实例引用访问的嵌套枚举条目“ PERSONALIZED” ”和“通过实例引用访问的嵌套枚举条目'NON_PERSONALIZED'” val unit = if (consentStatus == consentStatus.PERSONALIZED || consentStatus == consentStatus.NON_PERSONALIZED) 我在网上搜索过,但没有找到任何解决方法

val consentInformation = ConsentInformation.getInstance(applicationContext)
        val publisherIds = arrayOf("pub-xxxxxxxxxxxxxx")
        consentInformation.requestConsentInfoUpdate(publisherIds, object : ConsentInfoUpdateListener {
            override fun onConsentInfoUpdated(consentStatus: ConsentStatus) {
                // User's consent status successfully updated.
                val inEEA = ConsentInformation.getInstance(applicationContext).isRequestLocationInEeaOrUnknown

                if (inEEA) {
                    Toast.makeText(this@FramesActivity, consentStatus.toString(), Toast.LENGTH_SHORT).show()
                    val unit = if (consentStatus == consentStatus.PERSONALIZED || consentStatus == consentStatus.NON_PERSONALIZED) {
                        val extras = Bundle()
                        extras.putString("npa", "1")

                        val request = AdRequest.Builder()
                                .addNetworkExtrasBundle(AdMobAdapter::class.java, extras)
                                .build()
                    } else {
                        var privacyUrl: URL? = null
                        try {
                            // TODO: Replace with your app's privacy policy URL.
                            privacyUrl = URL("https://www.your.com/privacyurl")
                        } catch (e: MalformedURLException) {
                            e.printStackTrace()
                            // Handle error.
                        }

                        form = ConsentForm.Builder(this@FramesActivity, privacyUrl)
                                .withListener(object : ConsentFormListener() {
                                    override fun onConsentFormLoaded() {
                                        // Consent form loaded successfully.
                                        form?.show()
                                    }

                                    override fun onConsentFormOpened() {
                                        // Consent form was displayed.
                                    }

                                    override fun onConsentFormClosed(
                                            consentStatus: ConsentStatus?, userPrefersAdFree: Boolean?) {
                                        // Consent form was closed.
                                        val extras = Bundle()
                                        extras.putString("npa", "1")

                                        val request = AdRequest.Builder()
                                                .addNetworkExtrasBundle(AdMobAdapter::class.java, extras)
                                                .build()
                                    }

                                    override fun onConsentFormError(errorDescription: String?) {
                                        // Consent form error.
                                    }
                                })
                                .withPersonalizedAdsOption()
                                .withNonPersonalizedAdsOption()
                                // .withAdFreeOption()
                                .build()
                        form?.load()

                    }
                } else {
                    //  Toast.makeText(StickerPackListActivity.this, "Not In EEA", Toast.LENGTH_SHORT).show();

                }
            }

            override fun onFailedToUpdateConsentInfo(errorDescription: String) {
                // User's consent status failed to update.
                //  Toast.makeText(StickerPackListActivity.this, errorDescription, Toast.LENGTH_SHORT).show();
            }
        })

2 个答案:

答案 0 :(得分:0)

您应该只使用静态类名称,例如:ClassName.ENUM,这样可以解决您的错误。

您无需实例化枚举类即可使用它们,在您的情况下,consentStatus == ConsentStatus.PERSONALIZED

答案 1 :(得分:0)

此行似乎是错误的:

val unit = if (consentStatus == consentStatus.PERSONALIZED || consentStatus == consentStatus.NON_PERSONALIZED) {

如果ConsentStatus是枚举,则可能要查找的是此变量,请引用 ConsentStatus 不是变量 consentStatus

val unit = if (consentStatus == ConsentStatus.PERSONALIZED || consentStatus == ConsentStatus.NON_PERSONALIZED) {