UdaraWanasinghe
UdaraWanasinghe

Reputation: 2852

Getting NullPointerException: Missing required view with ID for a custom view

Steps to reproduce my problem

  1. Create a custom view
class VideoTrimmerView(context: Context, attrs: AttributeSet) : View(context) {
    private val backgroundPaint: Paint = Paint().apply {
        color = Color.BLACK
    }
    override fun draw(canvas: Canvas?) {
        super.draw(canvas)
        canvas?.drawRect(Rect(0, 0, width, height), backgroundPaint)
    }
}
  1. Add <declare-styleable> resources to the project
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="VideoTrimmerView" />
</resources>
  1. Add layout xml file activity_video_trimmer.xml
<?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=".VideoTrimmerActivity">

    <com.udara.developer.myapp.videotrimmer.VideoTrimmerView
        android:id="@+id/video_trimmer_view"
        android:layout_width="match_parent"
        android:layout_height="64dp"
        app:layout_constraintBottom_toBottomOf="parent" />

    <com.google.android.material.button.MaterialButton
        android:id="@+id/open_video_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Open Video"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
  1. Enable view binding in module level build.gradle
android {
    buildFeatures {
        viewBinding true
    }
}
  1. Use view bindings to access views

class VideoTrimmerActivity : AppCompatActivity() {
    private lateinit var binding: ActivityVideoTrimmerBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityVideoTrimmerBinding.inflate(layoutInflater)
        val view = binding.root
        setContentView(view)
    }
}

I get the following error when running the app

Caused by: java.lang.NullPointerException: Missing required view with ID: com.udara.developer.my_app:id/video_trimmer_view

The problem only happens when custom views are used. How to solve this issue?

Upvotes: 0

Views: 2744

Answers (1)

UdaraWanasinghe
UdaraWanasinghe

Reputation: 2852

I have invoked super constructor without attrs parameter.

Previous constructor version

class VideoTrimmerView(context: Context, attrs: AttributeSet) : View(context) {
}

After adding attrs parameter

class VideoTrimmerView(context: Context, attrs: AttributeSet) : View(context, attrs) {
}

In the android documentation it just tells

To allow Android Studio to interact with your view, at a minimum you must provide a constructor that takes a Context and an AttributeSet object as parameters.

Now the problem is solved!

Upvotes: 4

Related Questions