【问题标题】:id reference is showing error and not giving import optionid 参考显示错误并且不提供导入选项
【发布时间】:2021-11-07 17:15:38
【问题描述】:

我的图像视图 ID 显示错误,可能没有导入...这是我的图像视图 xml...

<ImageView
        android:id="@+id/abc"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0"
        tools:srcCompat="@tools:sample/avatars"
        android:contentDescription="TODO" />

这是 MainActivity 在滑行中,图像视图 ID 不接受...

val jsonObjectRequest =  JsonObjectRequest(
            Request.Method.GET, url, null,
            {response ->
                Glide.with(this).load(url).into(abc);
            },
            { })

这些是我的进口......

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.android.volley.Request
import com.android.volley.toolbox.JsonObjectRequest
import com.android.volley.toolbox.Volley
import com.bumptech.glide.Glide

请帮我解决这个问题...

【问题讨论】:

  • 那是 kotlin 合成的,现在已弃用。使用视图绑定或数据绑定来访问视图。

标签: android-studio kotlin android-developer-api


【解决方案1】:

如果您不使用 Kotlin Synthetics 或 View Binding,则可以使用传统方式执行此操作:

 val imageView = view.findViewById<ImageView>(R.id.abc)

然后在您想使用 abc 图像视图的地方执行此操作,如下所示:

 Glide.with(this).load(url).into(imageView);

如果你想使用 Kotlin Synthesis 方法,可以按照 link 在这里添加导入,但不再推荐,因为它已被弃用,为 View Binding 让路。

【讨论】:

  • 'view' 显示警告
  • 如果你使用了activity,删除视图,如果你使用fragment,只添加它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-16
  • 2019-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-18
相关资源
最近更新 更多