Android java.lang.NullPointerException:缺少ID为的必需视图:
安卓工作室3.6 在app/build.gradle中:Android java.lang.NullPointerException:缺少ID为的必需视图:,android,nullpointerexception,android-viewbinding,Android,Nullpointerexception,Android Viewbinding,安卓工作室3.6 在app/build.gradle中: android { viewBinding.enabled = true 这里是我的xml: <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="
android {
viewBinding.enabled = true
这里是我的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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/bluetoothBottonMainContainer"
android:layout_width="0dp"
android:layout_height="104dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<View
android:id="@+id/viewPointNotSelect"
android:layout_width="16dp"
android:layout_height="16dp"
android:background="@drawable/circle_transparent"
app:layout_constraintBottom_toBottomOf="@+id/separator"
app:layout_constraintEnd_toStartOf="@+id/separator"
app:layout_constraintTop_toTopOf="parent" />
该应用程序正在构建和运行。很好
现在我移动id-android:id=“@+id/bluetoothBottonMainContainer”
到外部容器,如下所示:
<?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"
android:id="@+id/bluetoothBottonMainContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="0dp"
android:layout_height="104dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<View
android:id="@+id/viewPointNotSelect"
android:layout_width="16dp"
android:layout_height="16dp"
android:background="@drawable/circle_transparent"
app:layout_constraintBottom_toBottomOf="@+id/separator"
app:layout_constraintEnd_toStartOf="@+id/separator"
app:layout_constraintTop_toTopOf="parent" />
错误:
10-25 11:11:51.290 E/AndroidRuntime(14128): FATAL EXCEPTION: main
10-25 11:11:51.290 E/AndroidRuntime(14128): Process: com.myproject.debug, PID: 14128
10-25 11:11:51.290 E/AndroidRuntime(14128): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myproject.debug/com.myproject.ui.actviity.QRBluetoothSwipeActivity}: java.lang.NullPointerException: Missing required view with ID: bluetoothBottonMainContainer
10-25 11:11:51.290 E/AndroidRuntime(14128): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
10-25 11:11:51.290 E/AndroidRuntime(14128): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
10-25 11:11:51.290 E/AndroidRuntime(14128): at android.app.ActivityThread.-wrap11(ActivityThread.java)
10-25 11:11:51.290 E/AndroidRuntime(14128): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
10-25 11:11:51.290 E/AndroidRuntime(14128): at android.os.Handler.dispatchMessage(Handler.java:102)
10-25 11:11:51.290 E/AndroidRuntime(14128): at android.os.Looper.loop(Looper.java:148)
10-25 11:11:51.290 E/AndroidRuntime(14128): at android.app.ActivityThread.main(ActivityThread.java:5417)
10-25 11:11:51.290 E/AndroidRuntime(14128): at java.lang.reflect.Method.invoke(Native Method)
10-25 11:11:51.290 E/AndroidRuntime(14128): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
10-25 11:11:51.290 E/AndroidRuntime(14128): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-25 11:11:51.290 E/AndroidRuntime(14128): Caused by: java.lang.NullPointerException: Missing required view with ID: bluetoothBottonMainContainer
10-25 11:11:51.290 E/AndroidRuntime(14128): at com.myproject.databinding.BluetoothBottomContainerBinding.bind(BluetoothBottomContainerBinding.java:114)
10-25 11:11:51.290 E/AndroidRuntime(14128): at com.myproject.databinding.QrBluetoothSwipeActivityBinding.bind(QrBluetoothSwipeActivityBinding.java:76)
10-25 11:11:51.290 E/AndroidRuntime(14128): at com.myproject.databinding.QrBluetoothSwipeActivityBinding.inflate(QrBluetoothSwipeActivityBinding.java:62)
10-25 11:11:51.290 E/AndroidRuntime(14128): at com.myproject.databinding.QrBluetoothSwipeActivityBinding.inflate(QrBluetoothSwipeActivityBinding.java:52)
10-25 11:11:51.290 E/AndroidRuntime(14128): at com.myproject.ui.actviity.QRBluetoothSwipeActivity.onCreate(QRBluetoothSwipeActivity.kt:31)
10-25 11:11:51.290 E/AndroidRuntime(14128): at android.app.Activity.performCreate(Activity.java:6251)
10-25 11:11:51.290 E/AndroidRuntime(14128): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
10-25 11:11:51.290 E/AndroidRuntime(14128): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
10-25 11:11:51.290 E/AndroidRuntime(14128): ... 9 more
10-25 11:11:51.291 W/ActivityManager( 780): Force finishing activity com.myproject.debug/com.myproject.ui.actviity.QRBluetoothSwipeActivity
10-25 11:11:51.307 I/Icing (11529): Indexing done com.google.android.gms-apps
更新 这应该在Android Studio的最新测试版和狂欢版中修复 在对“材质”选项卡“布局”选项卡项使用视图绑定时仍然存在一个问题,该问题已报告,但尚未修复
这是ViewBinding中的错误,问题跟踪器在以下位置报告了该错误
- 我遇到了相同的错误。您可以在“外部容器”中添加包装器视图以避免此错误
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/bluetoothBottonMainContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="0dp"
android:layout_height="104dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<View
android:id="@+id/viewPointNotSelect"
android:layout_width="16dp"
android:layout_height="16dp"
android:background="@drawable/circle_transparent"
app:layout_constraintBottom_toBottomOf="@+id/separator"
app:layout_constraintEnd_toStartOf="@+id/separator"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
我遇到了这个问题,但在我的例子中,问题是
include
标志。我发现的解决方法是使视图id与包含布局的根视图id相同
activity\u layout.xml
<LinearLayout>
<include android:id="@+id/widget1" layout="@layout/my_widget" />
</LinearLayout>
<LinearLayout
android:id="@+id/widget1">
</LinearLayout>
请检查,可能您在不同的模块中有相同的布局文件。在我的情况下,我删除了包含的布局中的id,它工作正常 main.xml
<RelativeLayout>
....
<include
include="@layout/included_layout"
android:id="@+id/view_included_layout"/>
</RelativeLayout>
....
包含的_layout.xml
` <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/top_level_layout"> <!-- Remove this id -->
....
</LinearLayout>
`
`
....
`
我的Android Studio版本是3.6.2,build.gradle版本是3.6.1我的解决方案类似,但略有不同,我的对话框中有一个include
<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:minHeight="@dimen/complete_programme_min_height"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include
android:id="@+id/include" <!-- Removing this line worked -->
layout="@layout/background_triangle_medium_violet_gradient" />
getRootView()返回包含的布局并可以对其进行更改。对于我来说,问题是我仍然使用Android 3.x版本,但使用的是新的Gradle 4.0.0版本
按下“关于->检查更新”,更新了Android Studio,错误不再出现。解决方案是删除主容器的id。并将其放入父容器内的其他容器中
1.布局
<include
android:id="@+id/example"
layout="@layout/account_current_payment_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
2.-包括布局
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/example2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent">
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
如果将单元格错误地绑定到recyclerView中的视图,也会出现此错误。如果将视图绑定到错误的布局,则会出现一个错误,说明它找不到预期的子视图
这让我难堪了很长时间
解决方案是确保绑定到视图的布局与视图绑定的布局相同。
例如,确保:
R.layout.my_new_view
必须:
MyNewViewBinding
否则您将出现此错误。请注意,如果在包含的布局XML中使用
,则逻辑不同,如下所述:
如果我们尝试给它一个ID,视图绑定将不会在绑定类中生成ID,因此我们无法像在普通include中那样访问视图。
在本例中,我们有占位符绑定,它是为placeholder.xml自动生成的类(我们的
布局文件)。我们必须调用它的bind()
方法,并传递包含它的布局的根视图
这正发生在我身上。我只是注意到发生了这种情况,因为在我的xml文件中,让我们调用它们:Activity_main.xml/Activity_other.xml我以前有一个小部件(在本例中是一个按钮),具有相同的id(+id/bttNext)
(这意味着activity_main.xml和activity_other过去有一个具有相同+id的按钮)
一旦我在设备中测试我的应用程序:
java.lang.NullPointerException:缺少ID为的必需视图:
出现
一旦我更改了第二个活动的按钮的+id(比如重命名为bttNext1),aspp就工作得很好
总结:避免在按钮(等小部件)中使用相同的id,无论它们是否在单独的活动中如果有人在启用了TabLayout
和ViewBinding
的情况下获得此功能,那么您可能已经为TabItem
设置了id。从TabItem
r中删除id为我解决了这个问题。只需删除.gradle目录和invlidate缓存/重新启动项目,保留项目备份。我也遇到了同样的问题,用这种方法解决了。您应该删除TabItem id并使用TabLayout API来获取该项目,如果您使用的是材质设计,这种解决方法会起作用
选中此项:重命名正在生成NPE的视图的id对我来说很有用。从DroperLayout转到DroperLayout_main,这解决了我的问题。您好,在使用合并标记时,您需要格外小心,我已经写了一篇博客文章,完全解释了视图绑定。为什么合并标签不那么容易工作,请在11m0结账。请创建一个答案,这样我就可以投票了。这是唯一对我们有用的答案!状态固定?当然不是。我仍然有这个问题。我从2020年12月2日开始使用android studio 3.6,但我的一些活动中这个问题并没有消失。我有一个linearLayout,它有tabLayout、viewPager等作为它的孩子。我的表格本身有3个选项卡项。我缺少选项卡项ID为的必需视图。当我删除他们的id时,这个错误就消失了。我正在使用简单的id,如@id/tabitem等。此活动还具有带导航的抽屉布局。通过使用viewbinding,即使我调用.setDisplayHomeAsUpEnabled(true),它的切换也会消失。最后,我添加了工具:viewBindingIgnore=“true”,并使用了好的旧findviewbyid。@Paradoxy我也遇到了这个问题,这个问题还没有解决。在使用动态添加视图的
布局时,我仍然出现了这个错误,例如:parent\u layout.xml
->addView forparent\u layout.xml
child使用MyMergeLayoutBinding.inflate(充气器、视图组、false)
->my\u merge\u layout.xml
使用
作为父标记,这在as v3.6.1和AGP v3.6.1中仍然是一个问题。我同意,这是愚蠢的。我的解决方案是保留include标记的id,并在include标记的布局参考中删除constraintLayout顶层的id
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/example2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent">
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
R.layout.my_new_view
MyNewViewBinding
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/placeholder" />
</androidx.constraintlayout.widget.ConstraintLayout>
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
binding = FragmentOrderBinding.inflate(layoutInflater, container, false)
placeholderBinding = PlaceholderBinding.bind(binding.root)
placeholderBinding.tvPlaceholder.text = getString(R.string.please_wait)
return binding.root
}