缺少带有 ID 的必需视图。我有啥错?

Posted

技术标签:

【中文标题】缺少带有 ID 的必需视图。我有啥错?【英文标题】:Missing required view with ID. What is my fault?缺少带有 ID 的必需视图。我有什么错? 【发布时间】:2021-12-26 09:05:21 【问题描述】:

我是android studio 的新手。在recyclerview adapter 中使用binding 时,出现以下错误:缺少ID 的必需视图

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.co_investorux_ui, PID: 18057
    java.lang.NullPointerException: Missing required view with ID: com.example.co_investorux_ui:id/app_name
        at com.example.co_investorux_ui.databinding.ActivityMainBinding.bind(ActivityMainBinding.java:151)
        at com.example.co_investorux_ui.RecyclerViewAdapter.onCreateViewHolder(MainActivity.kt:205)
        at com.example.co_investorux_ui.RecyclerViewAdapter.onCreateViewHolder(MainActivity.kt:183)
        at androidx.recyclerview.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:7295)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6416)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6300)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6296)
        at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2330)
        at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1631)
        at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1591)
        at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:668)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4309)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:4012)
        at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4578)
        at android.view.View.layout(View.java:21912)
        at android.view.ViewGroup.layout(ViewGroup.java:6260)
        at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1873)
        at android.view.View.layout(View.java:21912)
        at android.view.ViewGroup.layout(ViewGroup.java:6260)
        at com.sothree.slidinguppanel.SlidingUpPanelLayout.onLayout(SlidingUpPanelLayout.java:863)
        at android.view.View.layout(View.java:21912)
        at android.view.ViewGroup.layout(ViewGroup.java:6260)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at android.view.View.layout(View.java:21912)
        at android.view.ViewGroup.layout(ViewGroup.java:6260)
        at androidx.appcompat.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:536)
        at android.view.View.layout(View.java:21912)
        at android.view.ViewGroup.layout(ViewGroup.java:6260)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at android.view.View.layout(View.java:21912)
        at android.view.ViewGroup.layout(ViewGroup.java:6260)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
        at android.view.View.layout(View.java:21912)
        at android.view.ViewGroup.layout(ViewGroup.java:6260)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at com.android.internal.policy.DecorView.onLayout(DecorView.java:779)
        at android.view.View.layout(View.java:21912)
        at android.view.ViewGroup.layout(ViewGroup.java:6260)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3080)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2590)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1721)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7598)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:966)
        at android.view.Choreographer.doCallbacks(Choreographer.java:790)
        at android.view.Choreographer.doFrame(Choreographer.java:725)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:951)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

当我不使用binding 时,它运行良好,没有错误。

没有binding的代码

    //recyclerview adapter
    class RecyclerViewAdapter(
        val itemList: ArrayList<CoinList>,
        val inflater: LayoutInflater
    ) : RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>(), Filterable 
        private var searchList: ArrayList<CoinList>? = null

        inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) 
            val name: TextView
            val count: TextView

            init 
                name = itemView.findViewById(R.id.coin_name)
                count = itemView.findViewById(R.id.coin_count)
            
        

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder 
            val view = inflater.inflate(R.layout.item_view, parent, false)
            return ViewHolder(view)
        

        override fun onBindViewHolder(holder: ViewHolder, position: Int) 
            holder.name.setText(itemList.get(position).name)
            holder.count.setText(itemList.get(position).count)
            holder.itemView.setOnClickListener 
            
        

        override fun getItemCount(): Int 
            return itemList.size
        

    

使用binding的代码

// recyclerview adapter
class RecyclerViewAdapter(
    val itemList: ArrayList<MainActivity.CoinList>,
    val inflater: LayoutInflater
) : RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>(), Filterable 
    private var searchList: ArrayList<MainActivity.CoinList>? = null

    class ViewHolder(val binding: ActivityMainBinding) : RecyclerView.ViewHolder(binding.root) 
        val name: TextView
        val count: TextView

        init 
            name = itemView.findViewById(R.id.coin_name)
            count = itemView.findViewById(R.id.coin_count)
        
    

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder 
        val view = inflater.inflate(R.layout.item_view, parent, false)
        return ViewHolder(ActivityMainBinding.bind(view))
    

    override fun onBindViewHolder(holder: ViewHolder, position: Int) 
        holder.name.setText(itemList.get(position).name)
        holder.count.setText(itemList.get(position).count)
        val slidePanel = holder.binding.mainFrame

        holder.itemView.setOnClickListener 
            val state = slidePanel.panelState
            // Open slidePanel when it closed.
            if (state == SlidingUpPanelLayout.PanelState.COLLAPSED) 
                slidePanel.panelState = SlidingUpPanelLayout.PanelState.ANCHORED
            
            // Close slidePanel when it open
            else if (state == SlidingUpPanelLayout.PanelState.EXPANDED) 
                slidePanel.panelState = SlidingUpPanelLayout.PanelState.COLLAPSED
            
        
    

    override fun getItemCount(): Int 
        return itemList.size
    


xml代码

<com.sothree.slidinguppanel.SlidingUpPanelLayout 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:id="@+id/mainFrame"
    android:layout_
    android:layout_
    android:gravity="bottom"
    app:umanoDragView="@id/slide_layout"
    app:umanoPanelHeight="16dp"
    tools:context=".MainActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/first_layout"
        android:layout_
        android:layout_>

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerview"
            android:layout_
            android:layout_
            android:paddingBottom="15dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/search_view" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/slide_layout"
        android:layout_
        android:layout_
        android:background="#ffffff"></androidx.fragment.app.FragmentContainerView>


</com.sothree.slidinguppanel.SlidingUpPanelLayout>

我正在实现一个函数,当单击recyclerviewitemview 时调用sildinguppanellayout,但我无法解决错误。

你能告诉我我做错了什么吗?

【问题讨论】:

【参考方案1】:

ActivityMainBinding 只会绑定从activity_main.xml 创建的视图(注意命名)。要绑定从item_view.xml 创建的视图,您需要使用ItemViewBinding.bind(view)

【讨论】:

感谢您的友好回复。但我已经用过ItemViewBinding。要调用slidinguppanellayout,我需要获取mainFrame 的状态,mainFrame 是在activity_main.xml 中创建的。这种情况有什么解决办法吗? val view = inflater.inflate(R.layout.item_view, parent, false) return ViewHolder(ActivityMainBinding.bind(view))这部分 确保您使用的绑定类与用于创建视图的布局相匹配。此外,您将项目 ID 与绑定一起使用,这不是绑定的正确使用。 感谢您的回答。谢谢你,我意识到我的问题不仅仅是因为binding。无论是使用binding还是findviewbyid,我都需要在recyclerview adapter中引用两个xmls(activity_main, item_view)。需要activity_main 才能获得mainFrame 的状态。从code without binding 可以看出,我只是引用item_view。有没有办法在recyclerview adapter 中同时引用activity_mainitem_view?如果有办法,我想我可以解决问题。我不够好,但谢谢你的帮助。 我对那个库不熟悉,库有没有在状态改变时提供回调?还是每次都要手动检查?无论哪种方式,更有效的方法是提供 Slidepanel 作为适配器的参数,然后通过它检查状态。每次都绑定和重新绑定视图会导致混乱。

以上是关于缺少带有 ID 的必需视图。我有啥错?的主要内容,如果未能解决你的问题,请参考以下文章

[Route: bill] [URI: bill/id] 缺少必需的参数

没有路由匹配缺少必需的键:[:id]。破坏

缺少必需的道具:<AddProduct> 处的“id” - Laravel / Vue

Vue.js 缺少必需的道具

错误:invalid_request 缺少必需参数:golang 中的 client_id

元素链接缺少必需的属性属性