在 Java 方面,我收到 Famous error: on a null object object 如果定義條件“findVIewById”是正確的。有趣的是,帶有 id 的 textView :@ id/txt_detail_pPrice 被識別,但 @ id/txt_detail_Price NO 并拋出:在空物件上為什么 findVIewById 不適用于 @ id/txt_detail_Price 和 java 端的“價格”變為空??
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_details)
val imgProduct=findViewById< MyImageView >(R.id.img_details_showpicProduct)
val txt_title=findViewById<TextView >(R.id.txt_detail_showTitle)
val waranty=findViewById< TextView >(R.id.txt_details_waranty)
val pPrice=findViewById< TextView >(R.id.txt_detail_pPrice)
val price=findViewById<TextView>(R.id.txt_detail_Price)
val color=findViewById< TextView >(R.id.txt_details_color)
val detailIntroduction=findViewById<TextView>(R.id.txt_details_introduction)
val ratingBar=findViewById<RatingBar>(R.id.details_ratingBar_showratingProduct)
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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=".Details.DetailsActivity">
<com.google.android.material.appbar.AppBarLayout...>
<androidx.core.widget.NestedScrollView...>
<RelativeLayout
android:layout_width="match_parent"
android:layout_gravity="bottom"
android:background="@color/purple_500"
android:layout_height="75dp">
<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_centerVertical="true"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:backgroundTint="@color/greenIcon"
android:text="?????? ?? ??? ????"
android:textColor="@color/white" />
<TextView
android:id="@ id/txt_detail_pPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="10000000??????"
android:textColor="@color/teal_700" />
<TextView
android:id="@ id/txt_detail_Price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="20000000??????"
android:textColor="@color/black" />
</RelativeLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
uj5u.com熱心網友回復:
如果您非常確定它無法找到的相應視圖在您傳遞給的布局中setContentView,我認為導致此問題的最可能原因是您針對不同的螢屏配置擁有相同布局的多個版本,而忘記包含在所有視圖中都帶有此 ID。當螢屏處于不包含視圖的配置中時,將無法找到它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/354787.html
