You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android应用开发求助:字符串异常及Scrape方法未解析引用问题

Hey there, sorry you're stuck with these issues—let's break each one down and figure out fixes:

Issue 1: Suspected Uninitialized String

If you're getting errors related to uninitialized strings, here are a few things to check:

  • Make sure any string variables you're using are either assigned a default value (like var myText: String = "") or marked with lateinit if you plan to initialize them later (just don't access them before initialization!).
  • If you're setting text on your texx TextView, double-check that you're not passing a null value. Use the Elvis operator to handle nulls safely: texx.text = myNullableString ?: "Fallback text"
Issue 2: "words" Not Recognized as a String Resource

This usually happens when the resource doesn't exist or isn't properly referenced:

  1. First, verify you've added the string to your res/values/strings.xml file:
    <string name="words">Your desired text here</string>
    
  2. Check for typos in the resource name—capitalization matters! It should be R.string.words exactly as you named it in the XML.
  3. Ensure you're importing the correct R class (it should be from your app's package, not android.R). If you see import android.R at the top of your file, replace it with import com.yourpackage.name.R.
Issue 3: "Scrape" Method Unresolved Reference

The compiler can't find this method, so let's troubleshoot:

  • Did you define the Scrape method somewhere? If it's a custom method you wrote, make sure it's in the same class (or accessible from where you're calling it). If it's in another class, you'll need to create an instance of that class (e.g., val scraper = ScraperClass() then scraper.Scrape()) or mark it as a companion object method if it's static.
  • Check for typos in the method name—Kotlin is case-sensitive, so scrape vs Scrape are two different things.
  • If Scrape is from a third-party library, confirm you've added the library dependency to your build.gradle file and imported the correct class that contains the method.

Your Code Snippet for Reference

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    val texx: TextView = findViewById(R.id.text1) as TextView
    val button: Button = findViewById(R.id.button1) as Button
    button.setOnClickListener(object ...

Also, a quick general tip: Sometimes these errors stick around due to cached build files. Try cleaning and rebuilding your project (Build > Clean Project then Build > Rebuild Project in Android Studio) to refresh the R class and resolve any lingering reference issues.

内容的提问来源于stack exchange,提问作者Brane

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:52:07