使用.text时出现错误,错误显示为“ Unresolved reference:text” 我尝试从build.gradle同步中删除“应用插件:'kotlin-android-extensions'”,将其添加回去,然后重新同步。
package com.example.myapplication
import android.content.pm.PackageManager
import android.os.Bundle
import android.os.WorkSource
import android.view.*
import com.google.android.material.snackbar.Snackbar
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import com.google.android.gms.vision.CameraSource
import com.google.android.gms.vision.Detector
import com.google.android.gms.vision.barcode.Barcode
import com.google.android.gms.vision.barcode.BarcodeDetector
import kotlinx.android.synthetic.main.activity_main.*
import java.util.jar.Manifest
class MainActivity : AppCompatActivity() {
private lateinit var svBarcode: SurfaceView
private lateinit var tvBarcode: TextureView
private lateinit var detector: BarcodeDetector
private lateinit var cameraSource: CameraSource
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
svBarcode = findViewById(R.id.sv_barcode)
tvBarcode = findViewById(R.id.tv_barcode)
detector = BarcodeDetector.Builder(this).setBarcodeFormats(Barcode.ALL_FORMATS).build()
detector.setProcessor(object : Detector.Processor<Barcode>{
override fun release() {}
override fun receiveDetections(detections: Detector.Detections<Barcode>?) {
val barcodes = detections?.detectedItems
if(barcodes!!.size() > 0){
tvBarcode.post{
tvBarcode.text = barcodes.valueAt(0).displayValue
}
}
}
})
cameraSource = CameraSource.Builder(this, detector).setRequestedPreviewSize(1024, 768)
.setRequestedFps(25f).setAutoFocusEnabled(true).build()
svBarcode.holder.addCallback(object : SurfaceHolder.Callback2 {
override fun surfaceRedrawNeeded(holder: SurfaceHolder?) {}
override fun surfaceChanged(holder: SurfaceHolder?, format: Int, width: Int, height: Int) {}
override fun surfaceDestroyed(holder: SurfaceHolder?) {
cameraSource.stop()
}
override fun surfaceCreated(holder: SurfaceHolder?) {
cameraSource.start(holder)
}
})
fab.setOnClickListener { view ->
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show()
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.menu_main, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
return when(item.itemId) {
R.id.action_settings -> true
else -> super.onOptionsItemSelected(item)
}
}
}
对于Kotlin和Android开发人员,我真的很陌生,希望有一个我忽略的简单解决方案。先感谢您!