繁体   English   中英

如何修复“预期的 id 类型资源”错误?

[英]How can I fix "Expected resource of type id" error?

我在 android studio 4.1.1 中有一个错误我想让图像“student_bg”可拖动,所以我添加了这段代码但它没有用。

这是我的教程.xml

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="bottom"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/character"
        android:layout_width="35dp"
        android:layout_height="100dp"
        android:clickable="true"
        app:srcCompat="@drawable/student_bg" />
</LinearLayout>

这是我的教程.kt

var moveX = 0f
var moveY = 0f

class tutorial : AppCompatActivity(), SensorEventListener {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    getWindow().setFlags(
        WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
    setContentView(R.layout.tutorial)

    val st = findViewById<ImageView>(R.drawable.student_bg)

    st.setOnTouchListener { v, event ->
        when(event.action) {
            MotionEvent.ACTION_DOWN -> {
                moveX = v.x - event.rawX
                moveY = v.y - event.rawY
            }
            MotionEvent.ACTION_MOVE -> {
                v.animate()
                        .x(event.rawX + moveX)
                        .y(event.rawY + moveY)
                        .setDuration(0)
                        .start()
            }
        }
        true
    }
}
}

我发现 create ids.xml 会帮助解决这个问题,但它没有用。 我做了 ids.xml

这是我的 .xml 文件: tutorial.xml

这是我的 .kt 文件: turotial.kt

我错过了什么吗? 为什么它不起作用?

编辑:这是我的进口:

import android.animation.ObjectAnimator
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Bitmap
import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import android.media.MediaPlayer
import android.os.Build
import android.os.Bundle
import android.view.KeyEvent
import android.view.KeyEvent.*
import android.view.MotionEvent
import android.view.View
import android.view.WindowManager
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import java.util.Random
import kotlin.properties.Delegates

导入(图像)

在tutorials.kt中,用“R.id.character”代替“R.drawable.student_bg”,因为findViewById需要一个id,但是你传递了一个drawable并删除了ids.xml

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM