【问题标题】:Kotlin string replace function not working for me?Kotlin 字符串替换功能对我不起作用?
【发布时间】:2021-07-04 04:58:20
【问题描述】:

大家好,请帮助我,我正在尝试修改链接,但它不起作用。它正在使用 java,但最近我将 java 转换为 kotlin 并收到此错误。

我正在尝试使用当前登录用户电子邮件更改我在 [USER_ID] 中的链接 http://www.offertoro.com/click_track/api?offer_id=419393&pub_id=14&pub_app_id=5&USER_ID=[USER_ID]&GAID=[your_GAID],但出现错误。

检查屏幕截图 Screenshot

错误

None of the following functions can be called with the arguments supplied.
CharSequence.replace(Regex, String) defined in kotlin.text
String.replace(String, String, Boolean = ...) defined in kotlin.text

我的代码

  fun modifyOfferLink() {
       val id = mAuth!!.currentUser!!.email
        // Modifying Offer Link Acording to Offer Partner
        when (partner) {
            "ogads" -> Finallink = link + "&aff_sub5=" + mAuth!!.currentUser!!.email
            "offertoro" -> Finallink = link.replace("[USER_ID]", mAuth!!.currentUser!!.email)
            "none" -> {
                Finallink = link!!.replace("[USER_ID]", mAuth!!.currentUser!!.email)
            }
            else -> Finallink = link.replace("[USER_ID]", mAuth!!.currentUser!!.email)
        }
    }

OfferActivity.kt

package com.sgamer.creditsk.Activity

import android.content.Intent
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.Window
import android.view.WindowManager
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.google.firebase.auth.FirebaseAuth
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.loopj.android.http.AsyncHttpClient
import com.loopj.android.http.AsyncHttpResponseHandler
import com.loopj.android.http.RequestParams
import com.sgamer.creditsk.Activity.AndyConstants.ServiceType
import com.sgamer.creditsk.Activity.OfferDetailsActivity
import com.sgamer.creditsk.R
import com.sgamer.creditsk.Utils.*
import cz.msebera.android.httpclient.Header
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject

class OfferDetailsActivity constructor() : AppCompatActivity() {
    var Finallink: String? = null
    var package_id: String? = null
    var uniq_id: String? = null
    var offerid: String? = null
    var app_name: String? = null
    var description: String? = null
    var icon_url: String? = null
    var bg_image_url: String? = null
    var amount: String? = null
    var OriginalAmount: String? = null
    var link: String? = null
    var partner: String? = null
    var insTitle: String? = null
    var first_text: String? = null
    var second_text: String? = null
    var third_text: String? = null
    var fourth_text: String? = null
    var webview: Boolean? = null
    var ClickId: String? = null
    var ctx: OfferDetailsActivity? = null
    var later: TextView? = null
    var status_image: ImageView? = null
    var mAuth: FirebaseAuth? = null
    private val bannerAdManager: BannerAdManager_SK? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_offer_details)
        val toolbar: Toolbar = findViewById<View>(R.id.toolbar) as Toolbar
        val adContainer: LinearLayout = findViewById<View>(R.id.adView) as LinearLayout
        setSupportActionBar(toolbar)
        val bannerAdManager_SK: BannerAdManager_SK = BannerAdManager_SK(this@OfferDetailsActivity, adContainer)
        bannerAdManager_SK.BannerAds()
        ctx = this
        mAuth = FirebaseAuth.getInstance()

        getSupportActionBar()!!.setTitle(R.string.offer_details)
        getSupportActionBar()!!.setDisplayHomeAsUpEnabled(true)
        getSupportActionBar()!!.setBackgroundDrawable(ColorDrawable(getResources().getColor(android.R.color.transparent)))
        getSupportActionBar()!!.setElevation(0f)
        if (Build.VERSION.SDK_INT >= 21) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
        }
        changeStatusBarColor()
        initViews()
        modifyOfferLink()
    }

    private fun changeStatusBarColor() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            val window: Window = getWindow()
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            window.setStatusBarColor(Color.TRANSPARENT)
        }
    }

    fun initViews() {
        val title: TextView = findViewById(R.id.title)
        val desc: TextView = findViewById(R.id.description)
        val instructionsTitle: TextView = findViewById(R.id.instructions)
        val first: TextView = findViewById(R.id.first)
        val second: TextView = findViewById(R.id.second)
        val third: TextView = findViewById(R.id.third)
        val fourth: TextView = findViewById(R.id.fourth)
        val des: TextView = findViewById(R.id.des)
        val complete_button: TextView = findViewById(R.id.complete_button)
        val button: TextView = findViewById(R.id.button)
        later = findViewById(R.id.later)
        val comSpace: LinearLayout = findViewById(R.id.comSpace)
        val offer_icon: ImageView = findViewById(R.id.offer_icon)
        val bg_image: ImageView = findViewById(R.id.bg_image)
        status_image = findViewById(R.id.status_image)
        uniq_id = getIntent().getStringExtra("uniq_id")
        offerid = getIntent().getStringExtra("offerid")
        app_name = getIntent().getStringExtra("app_name")
        package_id = getIntent().getStringExtra("package_id")
        description = getIntent().getStringExtra("description")
        icon_url = getIntent().getStringExtra("icon_url")
        bg_image_url = getIntent().getStringExtra("bg_image_url")
        amount = getIntent().getStringExtra("amount")
        OriginalAmount = getIntent().getStringExtra("OriginalAmount")
        link = getIntent().getStringExtra("link")
        partner = getIntent().getStringExtra("partner")
        first_text = getIntent().getStringExtra("first_text")
        insTitle = getIntent().getStringExtra("instructionsTitle")
        second_text = getIntent().getStringExtra("second_text")
        third_text = getIntent().getStringExtra("third_text")
        fourth_text = getIntent().getStringExtra("fourth_text")
        webview = getIntent().getBooleanExtra("webview", false)
        if (getIntent().hasExtra("description")) {
            des.setText(getIntent().getStringExtra("description"))
        } else {
            des.setText(getIntent().getStringExtra("description"))
        }
        title.setText(app_name)
        desc.setText(getString(R.string.earn) + " " + amount + " " + getString(R.string.app_currency) + " " + getString(R.string.on_this_offer))
        Glide.with(this).load(icon_url)
                .apply(RequestOptions().placeholder(R.drawable.placeholder_image).error(R.drawable.placeholder_image))
                .into(offer_icon)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            comSpace.setElevation(20f)
        }
        instructionsTitle.setText(insTitle)
        first.setText(first_text)
        second.setText(second_text)
        third.setText(third_text)
        fourth.setText(fourth_text)
        complete_button.setText(getResources().getString(R.string.complete_offer))
        if (!bg_image_url!!.isEmpty()) {
            Glide.with(this).load(bg_image_url).into(bg_image)
        } else {
        }

        // On click Listners
        later!!.setOnClickListener(object : View.OnClickListener {
            public override fun onClick(view: View) {
                finish()
            }
        })
        complete_button.setOnClickListener(object : View.OnClickListener {
            public override fun onClick(view: View) {
                if (!App.isVPNConnected()) {
                    addoffer(amount, app_name + " Offer Credit", offerid)
                }
                AppUtils.parse(this@OfferDetailsActivity, Finallink)
            }
        })
        button.setOnClickListener(object : View.OnClickListener {
            public override fun onClick(view: View) {
                val launchIntent: Intent? = getPackageManager().getLaunchIntentForPackage((package_id)!!)
                startActivity(launchIntent)
            }
        })
        isAppExist
        if (isAppExist) {
            complete_button.setVisibility(View.GONE)
            button.setVisibility(View.VISIBLE)
        } else {
            button.setVisibility(View.GONE)
            complete_button.setVisibility(View.VISIBLE)
        }
    }

    fun addoffer(points: String?, Activity: String?, offerid: String?) {
        val client: AsyncHttpClient = AsyncHttpClient()
        val params: RequestParams = RequestParams()
        val jsObj: JsonObject = Gson().toJsonTree(API()) as JsonObject
        jsObj.addProperty("method_name", "user_offeradd")
        jsObj.addProperty("offer_id", offerid)
        jsObj.addProperty("email", mAuth!!.getCurrentUser()!!.getEmail())
        jsObj.addProperty("points", points)
        jsObj.addProperty("firebase_id", mAuth!!.getCurrentUser()!!.getUid())
        jsObj.addProperty("Activity", Activity)
        params.put("data", API.toBase64(jsObj.toString()))
        client.post(Javaaescipher.decrypt(), params, object : AsyncHttpResponseHandler() {
            public override fun onSuccess(statusCode: Int, headers: Array<Header>, responseBody: ByteArray) {
                Log.d("Response", String(responseBody))
                val res: String = String(responseBody)
                try {
                    val jsonObject: JSONObject = JSONObject(res)
                    val jsonArray: JSONArray = jsonObject.getJSONArray("ANDROID_REWARDS_APP")
                    for (i in 0 until jsonArray.length()) {
                        val `object`: JSONObject = jsonArray.getJSONObject(i)
                        val success: String = `object`.getString("success")
                        val msg: String = `object`.getString("msg")
                        if ((success == "1")) {
//                            Toast.makeText(OfferDetailsActivity.this, msg, Toast.LENGTH_LONG).show();
                        } else {
//                            Toast.makeText(OfferDetailsActivity.this, msg, Toast.LENGTH_LONG).show();
                        }
                    }
                } catch (e: JSONException) {
                    e.printStackTrace()
                }
            }

            public override fun onFailure(statusCode: Int, headers: Array<Header>, responseBody: ByteArray, error: Throwable) {
                Log.d("error", error.toString())
            }
        })
    }

    private val isAppExist: Boolean
        private get() {
            val pm: PackageManager = getPackageManager()
            try {
                val info: PackageInfo = pm.getPackageInfo((package_id)!!, PackageManager.GET_META_DATA)
            } catch (e: PackageManager.NameNotFoundException) {
                return false
            }
            return true
        }

    fun modifyOfferLink() {
       val id = mAuth!!.currentUser!!.email
        // Modifying Offer Link Acording to Offer Partner
        when (partner) {
            "ogads" -> Finallink = link + "&aff_sub5=" + mAuth!!.currentUser!!.email
            "offertoro" -> Finallink = link!!.replace("[USER_ID]", mAuth!!.currentUser!!.email)
            "none" -> {
                Finallink = link!!.replace("[USER_ID]", mAuth!!.currentUser!!.email)
            }
            else -> Finallink = link!!.replace("[USER_ID]", mAuth!!.currentUser!!.email)
        }
    }
}

【问题讨论】:

  • 很难弄清楚您提供的信息发生了什么。请添加更多信息,例如什么是链接、示例输入格式等。
  • offers.in/open.php?id=[USER_ID]&offer_id=123 我想用电子邮件 ID 替换 [USER_ID]
  • mAuth!!.currentUser!!.email 返回什么?我认为它不是一个字符串。
  • mAuth!!.currentUser!!.email 当前登录用户与firebase google
  • Edit 你的问题包括你在哪里声明了link

标签: android-studio kotlin


【解决方案1】:

很可能没有完全是[USER_ID] 的子字符串。你确定不是["USER_ID"]

您能否在调用替换之前打印link 的值。这可能会让我们和你看到问题。

【讨论】:

  • 应该可以。 Finallink 的值是多少?
  • 编译时出错:F:\23\new_app\app\src\main\java\com\sgamer\creditsk\Activity\OfferDetailsActivity.kt: (238, 40): 没有可以使用提供的参数调用以下函数:@InlineOnly public inline fun CharSequence.replace(regex: Regex, replacement: String): String defined in kotlin.text public fun String.replace(oldValue: String, newValue: String, ignoreCase: Boolean = ...): kotlin.text 中定义的字符串
猜你喜欢
  • 2015-08-22
  • 2021-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-23
  • 2019-01-08
相关资源
最近更新 更多