【发布时间】:2021-12-31 11:31:08
【问题描述】:
package com.example.myapplication2
import android.content.Intent
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Parcelable
import android.widget.TextView
import android.widget.ImageView
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
when {
intent?.action == Intent.ACTION_SEND -> {
when {
intent.type == PLAIN_TEXT_MIME -> handleSendText(intent)
intent.type?.startsWith(MEDIA_IMAGE_MIME) == true ->
handleSendImage(intent) // Handle single image being sent
}
}
intent?.action == Intent.ACTION_SEND_MULTIPLE -> {
when {
intent.type?.startsWith(MEDIA_IMAGE_MIME) == true -> handleSendMultipleImages(intent)
}
}
}
}
private fun handleSendText(intent: Intent) {
intent.getStringExtra(Intent.EXTRA_TEXT)?.let {
val textView.text = it // Update UI to reflect text being shared
}
}
private fun handleSendImage(intent: Intent) {
(intent.getParcelableExtra<Parcelable>(Intent.EXTRA_STREAM) as? Uri)?.let {
val first_ImageView.setImageURI(it)// Update UI to reflect image being shared
}
}
private fun handleSendMultipleImages(intent: Intent) {
intent.getParcelableArrayListExtra<Parcelable>(Intent.EXTRA_STREAM)?.let {
first_imageView.setImageURI(it(0) as? Uri)
second_imageView.setImageURI(it(1) as? Uri)// Update UI to reflect multiple images being shared
}
}
companion object {
private const val PLAIN_TEXT_MIME = "text/plain"
private const val MEDIA_IMAGE_MIME = "image/"
}
}
我收到的错误信息:
Local extension properties are not allowed
Local extension properties are not allowed
This variable must either have a type annotation or be initialized
Unexpected tokens (use ';' to separate expressions on the same line)
Unresolved reference: first_imageView
<html>Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:<br/>public operator fun <T, R> DeepRecursiveFunction<TypeVariable(T), TypeVariable(R)>.invoke(value: TypeVariable(T)): TypeVariable(R) defined in kotlin
Unresolved reference: second_imageView
<html>Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:<br/>public operator fun <T, R> DeepRecursiveFunction<TypeVariable(T), TypeVariable(R)>.invoke(value: TypeVariable(T)): TypeVariable(R) defined in kotlin
Unused import directive
Unused import directive
Variable 'text' is never used
'when' with subject should be used
【问题讨论】:
标签: android kotlin android-activity