Issue
On the Java side, I get Famous error: on a null object object In the event that definition conditions "findVIewById" are correct . It is interesting that textView With id :@+id/txt_detail_pPrice is identified but @+id/txt_detail_Price NO and throw :on a null object why findVIewById does not work just for @+id/txt_detail_Price and "price" in java side become null??
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>
Solution
If you are quite certain the corresponding view that it can't find is in the layout you passed to setContentView
, I think the most likely cause for this issue is that you have multiple versions of this same layout for different screen configurations, and forgot to include a view with this ID in all of them. When the screen is in a configuration where the view is not included, it cannot be found.
Answered By - Tenfour04