简体   繁体   English

为什么即使没有错误,我的Android Studio代码也会随机带红色下划线?

[英]Why does my Android Studio code randomly get underlined in red even when there is no error?

Sometimes my code gets randomly underlined in red even when there is no error 有时即使没有错误,我的代码也会随机用红色下划线

图片在这里

For example, in my Kotlin code (screenshot above): 例如,在我的Kotlin代码中(上面的截图):

    nameTextView=findViewById(R.id.nameDashboardTextView)
    ageTextView=findViewById(R.id.ageDashboardTextView)
    emailIDTextView=findViewById(R.id.emailIDTextView)
    genderTextView=findViewById(R.id.genderTextView)
    weightTextView=findViewById(R.id.weightTextView)
    heightTextView=findViewById(R.id.heightTextView)
    progressBar3=findViewById(R.id.progressBar3)

findViewById of any random variables gets underlined in red. 任何随机变量的findViewById用红色加下划线。 When I copy the text, remove it, and paste it back, the "error" is removed. 当我复制文本,将其删除并粘贴回去时,“错误”将被删除。 Furthermore, the red line doesn't stop the code from compiling or executing, the application works perfectly, but in Android Studio, some random line like findViewById or other straightforward code will get underlined in red. 此外,红线不会阻止代码的编译或执行,该应用程序可以正常运行,但是在Android Studio中,某些随机行(如findViewById或其他简单代码)将以红色下划线。

The error for the above example reads: 上面示例的错误为:

Cannot infer type parameter T in 无法推断类型参数T in

fun findViewById ( id:Int ) :T! 有趣的findViewById(id:Int):T! None of the following substitutions ( Int) ( Int) ( Int) can be applied to ( Int) 以下替换(Int)(Int)(Int)均不能应用于(Int)

Any idea why this happens? 知道为什么会这样吗? I'm using Android Studio 3.1.3, 4th June 2018 build 我正在使用Android Studio 3.1.3,2018年6月4日构建

Because Kotlin compiler cannot infer the specific type for your variables. 因为Kotlin编译器无法推断变量的特定类型。 To dismiss this red line you could you 2 ways. 要消除这条红线,您可以选择2种方式。

The former: 前者:

private lateinit var nameTextView: TextView

nameTextView = findViewById(R.id.nameDashboardTextView)

The latter: 后者:

nameTextView = findViewById<TextView>(R.id.nameDashboardTextView)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的android工作室代码下划线全是红色 - My android studio code is underlined all red 尝试在Android Studio中打开输入流时,为什么会收到未处理的错误异常? (代码发布在说明中) - Why do I get the error unhandeled exception when trying to open my inputstream in Android studio? (code posted in description) 为什么编译时库模块中的包不存在,即使Android Studio在代码中没有显示错误? - Why do packages from library module does not exist upon compilation, even when Android Studio shows no errors in code? 为什么Android Studio会丢弃我的代码样式设置? - Why does Android Studio throw away my Code Style settings? 构建签名的APK时,为什么Android Studio会卡住? - Why does Android Studio get stuck when building the signed APK? 为什么我的Android gameloop中的deltatime随机变大? - Why does the deltatime in my android gameloop randomly gets very large? 为什么我的代码会先执行 OnComplete,然后再执行 OnSucces,即使 OnComplete 在代码上方也是如此? - Why does my code do the OnComplete first and the OnSucces later even when OnComplete is above in the code? Android Studio-为什么即使完全禁用ActionBar也总是显示? - Android Studio - Why does the ActionBar always show, even when fully disabled? 为什么Android Studio总是在应用程序设计中显示ActionBar,即使在禁用时也是如此 - Why does Android Studio always show ActionBar in app design, even when disabled? 为什么我的JFrame在Mac上启动时会随机缩小? - Why does my JFrame randomly shrink when starting on Mac?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM