找到所需的生命周期所有者 Activity

Required lifecycle owner found Activity

我正在使用 android 库 androidx.appcompat:appcompat:1.0.2。处理 Codelabs 工作管理器的示例。我需要从 ViewModel 获取实时数据,我正在使用这个

    mViewModel!!.getOutputWorkInfo()?.observe(this, Observer<List<WorkInfo>> {
 })

但是这个变量显示错误- 类型不匹配。 Required:Lifecycle 楼主。 Found:BlurActivity

我用谷歌搜索了所有说不需要扩展生命周期所有者,默认情况下 appcompact activity 实现了生命周期所有者。

我也在另一个项目中使用过这个,没有发现问题。我不知道为什么我在这个项目中遇到这个错误。

`

import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.RadioGroup

import com.bumptech.glide.Glide

import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.work.WorkInfo

class BlurActivity : AppCompatActivity() {

    private var mViewModel: BlurViewModel? = null
    private var mImageView: ImageView? = null
    private var mProgressBar: ProgressBar? = null
    private var mGoButton: Button? = null
    private var mOutputButton: Button? = null
    private var mCancelButton: Button? = null

    /**
     * Get the blur level from the radio button as an integer
     * @return Integer representing the amount of times to blur the image
     */
    private val blurLevel: Int
        get() {
            val radioGroup = findViewById<RadioGroup>(R.id.radio_blur_group)

            return when (radioGroup.checkedRadioButtonId) {
                R.id.radio_blur_lv_1 ->  1
                R.id.radio_blur_lv_2 ->  2
                R.id.radio_blur_lv_3 ->  3
                else -> 1
            }
        }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_blur)

        // Get the ViewModel
        mViewModel = ViewModelProviders.of(this).get(BlurViewModel::class.java)

        // Get all of the Views
        mImageView = findViewById(R.id.image_view)
        mProgressBar = findViewById(R.id.progress_bar)
        mGoButton = findViewById(R.id.go_button)
        mOutputButton = findViewById(R.id.see_file_button)
        mCancelButton = findViewById(R.id.cancel_button)

        // Image uri should be stored in the ViewModel; put it there then display
        val intent = intent
        val imageUriExtra = intent.getStringExtra(Constants.KEY_IMAGE_URI)
        mViewModel!!.setImageUri(imageUriExtra)
        if (mViewModel!!.imageUri != null) {
            Glide.with(this).load(mViewModel!!.imageUri).into(mImageView!!)
        }

        mViewModel!!.getOutputWorkInfo()?.observe(this, Observer<List<WorkInfo>> {
            // If there are no matching work info, do nothing
            if (it == null || it.isEmpty()) return@Observer

            // We only care about the first output status.
            // Every continuation has only one worker tagged TAG_OUTPUT
            val workInfo = it[0]

            val finished = workInfo.state.isFinished
            if (!finished) showWorkInProgress() else showWorkFinished()
        })


        // Setup blur image file button
        mGoButton!!.setOnClickListener { view -> mViewModel!!.applyBlur(blurLevel) }
    }

    /**
     * Shows and hides views for when the Activity is processing an image
     */
    private fun showWorkInProgress() {
        mProgressBar!!.visibility = View.VISIBLE
        mCancelButton!!.visibility = View.VISIBLE
        mGoButton!!.visibility = View.GONE
        mOutputButton!!.visibility = View.GONE
    }

    /**
     * Shows and hides views for when the Activity is done processing an image
     */
    private fun showWorkFinished() {
        mProgressBar!!.visibility = View.GONE
        mCancelButton!!.visibility = View.GONE
        mGoButton!!.visibility = View.VISIBLE
    }
}

`

在 Main Activity 中实施 LifeCycleOwner 后,出现错误并正常工作

已更新

使用最新的 androidx 库,您不需要实现 LifecycleOwner。现在它默认在 ComponentActivity 中实现,AppcompatActivity 实现

同样的问题,所以我不得不更新我的 androidx.appcompat 依赖项,如下所示:

implementation 'androidx.appcompat:appcompat:1.1.0-alpha04'

无需实现 LifecycleOwner(因为现在默认实现了{如 Darthcow 所述})

appcompatLifecycle 依赖项不匹配时会出现此问题。

要么使用这组依赖项:

implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version
implementation 'androidx.appcompat:appcompat:1.1.0-alpha04'

或者:

implementation 'com.android.support:appcompat-v7:28.0.0'
implementation "android.arch.lifecycle:extensions:$lifecycle_version"