Glide Layout Null Kotlin "must not be null"
Glide Layout Null Kotlin "must not be null"
我试图在使用 Glide 单击图像时使用 activity 在单独的布局上将图像显示为全屏。相同的 Glide 代码适用于在产品详细信息中显示 activity 但不适用于全屏显示 activity。保持值不变并传递,通过打印检查,它从 firebase 获取图像 url,例如 https://firebasestorage.googleapis.com/v0/b/xxx.appspot.com/o/Product_Image1625509993393.png?alt=media&token=xxx Glide 无法显示的 xml 布局有问题。
原因:java.lang.NullPointerException:showfullimage 不能为空
在 com.shop.ui.activities.ShowFullImageActivity.loadimg(ShowFullImageActivity.kt:36)
在 com.shop.ui.activities.ShowFullImageActivity.onCreate(ShowFullImageActivity.kt:29)
onclick intent on productdetails activity:
iv_product_detail_image.setOnClickListener{
val intent = Intent(this@ProductDetailsActivity, ShowFullImageActivity::class.java)
intent.putExtra(Constants.PRODUCT_IMAGE, product.image)
startActivity(intent)
}
ShowFullImageActivity.kt
class ShowFullImageActivity : BaseActivity(), View.OnClickListener {
private var mProductImage: String = ""
override fun onClick(p0: View?) {
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (intent.hasExtra(Constants.PRODUCT_IMAGE)) {
mProductImage =
intent.getStringExtra(Constants.PRODUCT_IMAGE)!!
}
println(mProductImage)
//full screen load
loadimg()
}
private fun loadimg() {
Glide
.with(this@ShowFullImageActivity)
.load(mProductImage) // Uri or URL of the image
.centerCrop() // Scale type of the image.
.placeholder(R.drawable.ic_user_placeholder) // A default place holder if image is failed to load.
.into(showfullimage)
}
}
show_full_image.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.activities.ShowFullImageActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:fillViewport="true"
android:scrollbars="none"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.github.chrisbanes.photoview.PhotoView
android:id="@+id/showfullimage"
android:layout_width="match_parent"
android:layout_height="@dimen/product_detail_image_height"
android:background="@color/colorImageViewBackground"
android:contentDescription="@string/content_description"
android:scaleType="fitXY"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
你错过了这个
setContentView(R.layout.show_full_image)
没有这个所有控件都是空的。
1)加载View前需要setContentView
2)showfullimage在使用前需要初始化
因此您可以尝试在 ShowFullImageActivity.kt 中添加一些代码:
override fun initData() {
super.onCreate(savedInstanceState)
setContentView(R.layout.show_full_image)
showfullimage = findViewById(R.id.showfullimage)
...
}
我试图在使用 Glide 单击图像时使用 activity 在单独的布局上将图像显示为全屏。相同的 Glide 代码适用于在产品详细信息中显示 activity 但不适用于全屏显示 activity。保持值不变并传递,通过打印检查,它从 firebase 获取图像 url,例如 https://firebasestorage.googleapis.com/v0/b/xxx.appspot.com/o/Product_Image1625509993393.png?alt=media&token=xxx Glide 无法显示的 xml 布局有问题。
原因:java.lang.NullPointerException:showfullimage 不能为空 在 com.shop.ui.activities.ShowFullImageActivity.loadimg(ShowFullImageActivity.kt:36) 在 com.shop.ui.activities.ShowFullImageActivity.onCreate(ShowFullImageActivity.kt:29)
onclick intent on productdetails activity:
iv_product_detail_image.setOnClickListener{
val intent = Intent(this@ProductDetailsActivity, ShowFullImageActivity::class.java)
intent.putExtra(Constants.PRODUCT_IMAGE, product.image)
startActivity(intent)
}
ShowFullImageActivity.kt
class ShowFullImageActivity : BaseActivity(), View.OnClickListener {
private var mProductImage: String = ""
override fun onClick(p0: View?) {
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (intent.hasExtra(Constants.PRODUCT_IMAGE)) {
mProductImage =
intent.getStringExtra(Constants.PRODUCT_IMAGE)!!
}
println(mProductImage)
//full screen load
loadimg()
}
private fun loadimg() {
Glide
.with(this@ShowFullImageActivity)
.load(mProductImage) // Uri or URL of the image
.centerCrop() // Scale type of the image.
.placeholder(R.drawable.ic_user_placeholder) // A default place holder if image is failed to load.
.into(showfullimage)
}
}
show_full_image.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.activities.ShowFullImageActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:fillViewport="true"
android:scrollbars="none"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.github.chrisbanes.photoview.PhotoView
android:id="@+id/showfullimage"
android:layout_width="match_parent"
android:layout_height="@dimen/product_detail_image_height"
android:background="@color/colorImageViewBackground"
android:contentDescription="@string/content_description"
android:scaleType="fitXY"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
你错过了这个
setContentView(R.layout.show_full_image)
没有这个所有控件都是空的。
1)加载View前需要setContentView
2)showfullimage在使用前需要初始化
因此您可以尝试在 ShowFullImageActivity.kt 中添加一些代码:
override fun initData() {
super.onCreate(savedInstanceState)
setContentView(R.layout.show_full_image)
showfullimage = findViewById(R.id.showfullimage)
...
}