【问题标题】:What am I missing in the code? Is it the import or is it extra line of code?我在代码中遗漏了什么?是导入还是额外的代码行?
【发布时间】: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 &lt;T, R&gt; DeepRecursiveFunction&lt;TypeVariable(T), TypeVariable(R)&gt;.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 &lt;T, R&gt; DeepRecursiveFunction&lt;TypeVariable(T), TypeVariable(R)&gt;.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


    【解决方案1】:

    我认为问题出在这里

     val first_ImageView.setImageURI(it)
    

    你需要删除'val'

    编辑:就像Gabe Sechan 提到的那样,您需要将布局绑定到您的 MainActivity(但前提是这些 TextViews 实际上在您的 activity_main.xml 中):

    将此字段添加到您的活动中:

    private lateinit var binding: ActivityMainBinding
    

    并在 onCreate() 中添加:

    binding = ActivityMainBinding.inflate(layoutInflater)
    setContentView(binding.root)
    

    如果你想在 TextView 上调用 setImageUri(),请执行以下操作:

    binding.yourTextView.setImageURI(it)
    

    或者你可以写:

    val first_ImageView = findViewById<View>(R.id.first_ImageView)
    

    然后对找到的ImageView进行操作

    编辑2:

    您需要将方法更改为:

    private fun handleSendText(intent: Intent) {
        intent.getStringExtra(Intent.EXTRA_TEXT)?.let {
           findViewById<View>(R.id.textView)?.text = it // Update UI to reflect text being shared
        }
    }
    
        private fun handleSendImage(intent: Intent) {
            (intent.getParcelableExtra<Parcelable>(Intent.EXTRA_STREAM) as? Uri)?.let {
               findViewById<View>(R.id.first_ImageView)?.setImageURI(it)// Update UI to reflect image being shared
            }
        }
        
        private fun handleSendMultipleImages(intent: Intent) {
            intent.getParcelableArrayListExtra<Parcelable>(Intent.EXTRA_STREAM)?.let {
                findViewById<View>(R.id.first_ImageView)?.setImageURI(it(0) as? Uri)
                findViewById<View>(R.id.second_imageView)?.setImageURI(it(1) as? Uri)// Update UI to reflect multiple images being shared
            }
        }
    

    【讨论】:

    • 不仅如此——他还没有创建过 firstImageView。他需要类中的私有 val first_ImageView。或者他需要使用 ViewBinding,这似乎是他想要的
    • 谢谢,但我很困惑。我会将 val first_ImageView = findViewById(R.id.first_ImageView) 放在哪里。另外,我会用 textView 和 second_ImageView 来做吗
    • 我再次编辑了答案,以便更清楚您需要在哪里应用更改
    • 还是同样的问题
    • 我需要“创建id值资源“textView”吗?和我的小部件有关吗?
    【解决方案2】:

    没有视图绑定。 您需要在活动文件中定义视图并在 setContentView 方法之后分配它们的值,然后您可以使用它来更新您的 UI。

    `package com.example
    
    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import android.content.Intent
    import android.net.Uri
    import android.os.Parcelable
    import android.widget.TextView
    import android.widget.ImageView
    
     class MainActivity : AppCompatActivity() {
    
      private lateinit var firstImage: ImageView;
      private lateinit var secondImage: ImageView;
      private lateinit var textView: TextView;
    
     override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        textView = findViewById(R.id.textView)
        firstImage = findViewById(R.id.firstImage)
        secondImage = findViewById(R.id.secondImage)
        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 {
            textView.text = it // Update UI to reflect text being shared
        }
    }
    
    private fun handleSendImage(intent: Intent) {
        (intent.getParcelableExtra<Parcelable>(Intent.EXTRA_STREAM) as? Uri)?.let {
            firstImage.setImageURI(it)// Update UI to reflect image being shared
        }
    }
    
    private fun handleSendMultipleImages(intent: Intent) {
        intent.getParcelableArrayListExtra<Parcelable>(Intent.EXTRA_STREAM)?.let {
            firstImage.setImageURI(it[0] as? Uri)
            secondImage.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/"
      }
    }
    `
    

    XML 文件

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical"
     tools:context=".MainActivity">
    
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
    
    <ImageView
        android:id="@+id/firstImage"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    
    <ImageView
        android:id="@+id/secondImage"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    

    您可以使用 ViewBindings 删除 findViewById 调用。 [https://developer.android.com/topic/libraries/view-binding]

    【讨论】:

    • 谢谢。我已经解决了问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-14
    • 2013-03-12
    • 1970-01-01
    • 2023-02-02
    • 1970-01-01
    相关资源
    最近更新 更多