首页 > 解决方案 > AndroidStudio] 布局文件中的对象未显示在代码中

问题描述

我对android studio很陌生,想制作一个播放视频的应用程序。所以我做了一个代码:

const val REQUEST_VIDEO_CAPTURE = 1

class MainActivity : AppCompatActivity() {

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

    // This is for Audio play
    //var mediaPlayer = MediaPlayer.create(this,R.raw.test_720)
    //mediaPlayer.start()

    var path_vid = "android.resource://${packageName}/${R.raw.test_720}"
    var mediaController = MediaController(this)
    **mediaController.setAnchorView(dancerView)**

}

而activity_main 如下。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<VideoView
    android:id="@+id/dancerView"
    android:layout_width="414dp"
    android:layout_height="738dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.666"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.571" />

<VideoView
    android:id="@+id/studentView"
    android:layout_width="133dp"
    android:layout_height="196dp"
    android:layout_marginEnd="20dp"
    android:layout_marginBottom="20dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent" />

<Button
    android:id="@+id/playbtn"
    android:layout_width="130dp"
    android:layout_height="91dp"
    android:text="Play Vid"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="@+id/dancerView"
    app:layout_constraintTop_toTopOf="@+id/dancerView" />

</androidx.constraintlayout.widget.ConstraintLayout>

我确定我有 dancerView:VideoView,但我无法构建代码并看到错误:未解决的参考。由于同样的错误,我也无法使用其他视图和按钮。我做错了什么?

标签: androidkotlin

解决方案


推荐阅读