[英]java.lang.NullPointerException: Missing required view with ID:
Android工作室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="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" />
和另一個 xml unclude prev。 xml:
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/bottonContainer"
android:layout_width="0dp"
android:layout_height="104dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<include
android:id="@+id/qrBottonContainer"
layout="@layout/qr_bottom_container"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
這是我的活動:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = QrBluetoothSwipeActivityBinding.inflate(layoutInflater)
setContentView(binding.root)
}
該應用程序已構建並運行。 好的。
現在我移動 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" />
應用程序已構建,但運行時我在這一行中收到運行時錯誤:
binding = QrBluetoothSwipeActivityBinding.inflate(layoutInflater)
錯誤:
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
我遇到了這個問題,但在我的情況下,問題是include
標志。 我找到的解決方法是使視圖 id 與包含布局的根視圖的 id 相同。
activity_layout.xml
<LinearLayout>
<include android:id="@+id/widget1" layout="@layout/my_widget" />
</LinearLayout>
my_widget.xml
<LinearLayout
android:id="@+id/widget1">
</LinearLayout>
如果有人在啟用TabLayout
和ViewBinding
的情況下得到這個,那么您可能已經為TabItem
設置了 id。 從TabItem
中刪除 id 為我解決了這個問題。
請檢查,也許您在不同的模塊中有相同的布局文件。
請注意,如果您在包含的布局 XML 中使用<merge>
,則邏輯不同,如下所示:
深入探索Android視圖綁定
如果我們嘗試給它一個 ID,視圖綁定將不會在綁定 class 中生成 ID,因此我們無法像在正常包含的情況下那樣訪問視圖。
在這種情況下,我們有PlaceholderBinding ,它是一個自動生成的 class,用於 placeholder.xml (我們的<merge>
布局文件)。 我們必須調用它的bind()
方法並傳遞包含它的布局的根視圖。
<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
}
更新
這應該在 Android Studio 的最新 Beta、Carnary 版本中修復
將視圖綁定與材料選項卡布局選項卡項目一起使用時仍然存在問題,此處報告但尚未修復。
這是 ViewBinding 中的錯誤,在以下位置的問題跟蹤器中報告。
如果您錯誤地將單元格綁定到 recyclerView 中的視圖,也會出現此錯誤。 如果您將視圖綁定到錯誤的布局,那么您將收到一條錯誤消息,指出它無法找到預期的子視圖。
這讓我難過了很久。
解決方案是確保您綁定到視圖的布局與 ViewBinding 的布局相同。
例如,確保:
R.layout.my_new_view
一定會:
MyNewViewBinding
否則你會得到這個錯誤。
您應該刪除 TabItem id 並使用 TabLayout API 來獲取該項目,如果您使用的是 Material Design,則此解決方法有效
檢查這個: Github 答案
解決方案是刪除主容器的 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>
就我而言,我刪除了包含布局中的 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
我遇到了同樣的錯誤。您可以在“外部容器”中添加包裝視圖以避免此錯誤。
<?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>
對我來說,問題是我仍在使用 Android 3.x 版本,但使用的是新的 Gradle 4.0.0 版本。
按“關於->檢查更新”,更新了Android Studio,錯誤消失了。
這正在發生在我身上。 I just noticed that this happened because in my xml files lets call them: Activity_main.xml / Activity_other.xml I used to have a widget (a button in this case) with the same id, (+id/bttNext)
(這意味着 activity_main.xml 和 Activity_other 曾經有一個 +id 相同的按鈕)
一旦我在設備中測試我的應用程序:
java.lang.NullPointerException:缺少所需的 ID 視圖:
出現了
一旦我更改了第二個活動的按鈕的 +id(可以說重命名為 bttNext1),aspp 工作正常
總結:避免在按鈕(等小部件)中使用相同的 id,如果它們在單獨的活動中無關緊要
只需刪除.gradle 目錄並invlidate緩存/重新啟動項目,保留項目備份。 我遇到了同樣的問題,用這個方法解決了。
如果您錯誤地對片段使用 xml 布局但對綁定使用不同的布局,也會發生此錯誤。我遇到錯誤時正在做
class LoginFragment :Fragment(R.layout.login_fragment) {
private val binding by viewBinding(LoginFragmentNewBinding::bind)//here I am referring to a new xml file called login_fragment_new which is different from the one in the Fragment's constructor (i.e login_fragment)
}
並使用相同的 xml 文件為我修復了它。
我遇到了同樣的問題,我通過在布局中刪除這一行來解決它。
tools:context=".ui.SplashScreenActivity
我的解決方案類似但略有不同,我的對話框中有一個包含
<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() 返回包含的布局並可以在其中進行更改
可能晚了,但是。 對於遇到此問題的每個人。 我首先嘗試了在這里看到的所有東西,但沒有運氣。 我發現使用蛇盒 ID 可以解決問題。 我有一個作為 tabContainer 的 ID,它導致了 NPE。 將其重命名為 tab_container 為我修復了該錯誤。
同樣的問題,但只是在自定義視圖活動中,我放置了正確的屬性。
在 kotlin 中:
class CanvasNew @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null):View(context,attrs){
對於我的基礎my_main_layout.xml
是 -
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<ScrollView style="@style/ScrollViewContainerStyle">
<include
android:id="@+id/include_1"
layout="@layout/layout"></include>
<include
android:id="@+id/include_1"
layout="@layout/layout"></include>
<include
android:id="@+id/include_1"
layout="@layout/layout"></include>
</include></layout>
</ScrollView>
而我包含layout.xml
是 -
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_title"
tools:text="This is a tile" />
</LinearLayout>
我剛剛通過用<layout>
包裝解決了我的問題 -
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:visibility="visible">
<TextView
android:id="@+id/tv_title"
tools:text="This is a tile" />
</LinearLayout>
</layout>
使用自定義視圖遇到了這個問題,在哪里錯誤地使用了this
而不是super
constructor(context: Context, attrs: AttributeSet?) : this(context,attrs) { //<-Change this to super
}
就我而言,另一個模塊中有另一個同名的文件。
我在模塊 A 中有 item_view.xml,在模塊 B 中有 item_view.xml,因此數據綁定無法區分它們。
我更改了其中一個的名稱並修復了它。
在我的情況下,我在onCreateView
方法中使用了不正確的FragmentBinding
class 來膨脹布局:
open class InfoFragment : BaseFragment() {
private val binding by viewBinding(FragmentInfoBinding::bind)
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
// NOTE: using incorrect binding class
return FragmentEducationBinding.inflate(inflater, container, false).root
}
...
}
為了解決這個問題,我必須在onCreateView
方法中使用正確的綁定 class ,在我的例子中是FragmentInfoBinding
:
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
return FragmentInfoBinding.inflate(inflater, container, false).root
}
一個解決方案可能是:
init {
id = context.obtainStyledAttributes(attrs, intArrayOf(android.R.attr.id)).let { typedArray ->
val value = typedArray.getResourceId(0, View.NO_ID)
typedArray.recycle()
value
}
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
binding = AnyBinding.inflate(inflater, this)
}
我遇到了錯誤,因為在我的情況下,主app
和module
具有相同的布局( layout_calling_waiting
)具有不同的視圖,這些視圖無法在應用程序的布局中找到。 所以錯誤是給的。
通過使用解決它: Invalidate Caches 和 Clean Project 。
在我的例子中,我將一些小部件從一個布局復制到另一個布局,然后我更改了 ID(因此沒有重復 ID)。
調試構建工作正常,但在嘗試運行發布構建時拋出異常。
清潔和重建項目幫助了我
在我的例子中,這個問題與不同模塊中 xml 文件的相同命名有關。
我在不同的模塊( app
、 landing
)中有 2 個同名的 xml 文件( item_tag.xml
)。 編譯器不知道需要使用哪個,並且我在運行時遇到錯誤(每次我用這個binding
打開一個屏幕時)。
我重命名了 xml 文件,其中一個變成了item_landing_tag.xml
(並生成了ItemLandingTagBinding
類而不是ItemTagBinding
)。
希望它能幫助某人:)
我也有這個問題,但顯然這正是錯誤指出的,一個 null 指針,該視圖被我的自定義視圖容器“消耗”了,該容器只接受了第一個子視圖后的第一個子視圖和下一個 ID 指出的視圖錯誤已從層次結構中刪除,因此 viewbinder 無法綁定它們。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.