سلام خسته نباشید ببخشید دوستان من به ی اروری برخوردم که هرچی خواستم برطرف کنم نتونستم حتی کدهای خود استادم کپی کردم و
توی پروژه خودم پیست کردم ولی بازم همون ارور داد ولی وقتی پروژه ی استاد ران میکنم اجرا میشه
این ارور هستش
Unable to start activity ComponentInfo{com.example.nike/com.example.nike.featuer.main.MainActivity}: java.lang.NullPointerException: latestProductsRv must not be null
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3303)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3411)
at android.app.ActivityThread.-wrap12(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1994)
at android.os.Handler.dispatchMessage(Handler.java:108)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7529)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
Caused by: java.lang.NullPointerException: latestProductsRv must not be null
at com.example.nike.featuer.main.MainFragment.onViewCreated(MainFragment.kt:43)
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="56dp"
android:layout_gravity="center_horizontal"
app:srcCompat="@drawable/ic_nike_logo" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="46dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:background="@drawable/backgraund_et_serach"
android:hint="جستجو"
android:paddingStart="54dp"
android:paddingEnd="16dp"
android:textColor="?colorOnSurface"
android:textColorHint="?android:textColorSecondary"
android:textSize="15sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start|center_vertical"
android:layout_marginStart="38dp"
app:srcCompat="@drawable/ic_baseline_search_24" />
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/bannerSliderViewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp" />
<com.tbuonomo.viewpagerdotsindicator.DotsIndicator
android:id="@+id/sliderIndicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:layout_marginBottom="12dp"
app:dotsColor="#E0DCDC"
app:dotsCornerRadius="8dp"
app:dotsSize="5dp"
app:dotsSpacing="3dp"
app:dotsWidthFactor="4"
app:progressMode="false"
app:selectedDotColor="@color/black" />
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="16dp"
android:paddingEnd="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/lasted"
android:layout_gravity="center_vertical|start"
android:textAppearance="?textAppearanceHeadline6"
android:textColor="?android:textColorSecondary" />
<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/viewAll"
style="@style/Widget.MaterialComponents.Button.TextButton"
android:layout_gravity="end|center_vertical"/>
</FrameLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/latestProductsRv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="12dp"
android:paddingEnd="12dp"
android:clipToPadding="false"/>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
package com.example.nike.featuer.main
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.observe
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.ViewPager2
import com.example.nike.R
import com.example.nike.common.MetricsUtil.convertDpToPixel
import com.example.nike.common.NikeFragment
import com.example.nike.data.Product
import com.example.nike.featuer.banner.BannerSliderAdapter
import com.example.nike.featuer.rvadpter.ProductListAdapter
import com.tbuonomo.viewpagerdotsindicator.DotsIndicator
import org.koin.android.ext.android.inject
import org.koin.android.viewmodel.ext.android.viewModel
import timber.log.Timber
class MainFragment : NikeFragment() {
val mainViewModel: MainViewModel by viewModel()
val productListAdapter: ProductListAdapter by inject()
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_main, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val latestProductsRv = view.findViewById<RecyclerView>(R.id.latestProductsRv)
val bannerSliderViewPager = view.findViewById<ViewPager2>(R.id.bannerSliderViewPager)
val sliderIndicator = view.findViewById<DotsIndicator>(R.id.sliderIndicator)
latestProductsRv.layoutManager =
LinearLayoutManager(requireContext(), RecyclerView.HORIZONTAL, false)
latestProductsRv.adapter = productListAdapter
mainViewModel.productsLiveData.observe(viewLifecycleOwner) {
Timber.i(it.toString())
productListAdapter.products= it as ArrayList<Product>
}
mainViewModel.progressBarLiveData.observe(viewLifecycleOwner) {
setProgressIndicator(it)
}
mainViewModel.bannersLiveData.observe(viewLifecycleOwner) {
Timber.i(it.toString())
val bannerSliderAdapter = BannerSliderAdapter(this, it)
bannerSliderViewPager.adapter = bannerSliderAdapter
val viewPagerHeight = (((bannerSliderViewPager.measuredWidth - convertDpToPixel(
32f,
requireContext()
)) * 173) / 328).toInt()
val layoutParams = bannerSliderViewPager.layoutParams
layoutParams.height = viewPagerHeight
bannerSliderViewPager.layoutParams = layoutParams
sliderIndicator.setViewPager2(bannerSliderViewPager)
}
}