RecyclerView调用setLayoutManager时触发NullPointerException,点击搜索页面Fragment应用崩溃求助
Hey there! Let's break down why your app is crashing when you navigate to the Search_Page_Fragment, and fix it step by step.
The Root Cause
Looking at your error log:
java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)' on a null object reference
at com.example.assignment_mad.Search_Page_Fragment.onViewCreated(Search_Page_Fragment.kt:153)
This crash happens because you're trying to call methods on a null RecyclerView reference. Here's the exact breakdown:
- You imported the wrong synthetic layout binding:
import kotlinx.android.synthetic.main.fragment_notification_.*— this is tied to a different fragment's layout, not your search page. - In
onViewCreated, you userecyclerView.apply { ... }, but your search page layout (fragment_search__page_.xml) has no RecyclerView with the idrecyclerView. Your actual RecyclerView id isrecyclerView_Search, which you correctly initialized asnewRecyclerViewusingfindViewById. - You're calling
super.onViewCreated(itemView, savedInstanceState)after initializing data, which is out of the recommended order (it should be the first line in the method to ensure proper fragment lifecycle setup).
Step-by-Step Fixes
1. Fix or Remove the Synthetic Import
First, delete the incorrect synthetic import (since you're already using findViewById to initialize your RecyclerView, this avoids confusion):
// Remove this line entirely: // import kotlinx.android.synthetic.main.fragment_notification_.*
2. Replace the Null RecyclerView Reference
In your onViewCreated method, replace all instances of recyclerView with newRecyclerView — this is the variable you correctly bound to your layout's recyclerView_Search component.
Here's the corrected onViewCreated method:
override fun onViewCreated(itemView: View, savedInstanceState: Bundle?) { // Move super call to the top (best practice for fragment lifecycle) super.onViewCreated(itemView, savedInstanceState) imageId= arrayOf( R.drawable.company_logo_1, R.drawable.company_logo_2, R.drawable.company_logo_3, R.drawable.company_logo_4, R.drawable.company_logo_5, R.drawable.company_logo_6, R.drawable.company_logo_7, R.drawable.company_logo_8, R.drawable.company_logo_9, R.drawable.company_logo_10 ) job_name= arrayOf( "Engineer", "Computer", "Coder", "Teacher", "Engineer", "Engineer", "Engineer", "Engineer", "Engineer", "Engineer" ) saveicon= arrayOf( R.drawable.ic_baseline_bookmark_added_24, R.drawable.ic_baseline_bookmark_added_24, R.drawable.ic_baseline_bookmark_added_24, R.drawable.ic_baseline_bookmark_added_24, R.drawable.ic_baseline_bookmark_added_24, R.drawable.ic_baseline_bookmark_added_24, R.drawable.ic_baseline_bookmark_added_24, R.drawable.ic_baseline_bookmark_added_24, R.drawable.ic_baseline_bookmark_added_24, R.drawable.ic_baseline_bookmark_added_24 ) company_name= arrayOf( "Phonix Sdn Bhd", "Computer Sdn Bhd", "Coder", "Teacher", "Engineer", "Engineer", "Engineer", "Engineer", "Engineer", "Engineer" ) place= arrayOf( "Kuala Lumpur", "Kuala Lumpur", "Selangor", "Rimbunan", "Kuala Lumpur", "Kuala Lumpur", "Kuala Lumpur", "Kuala Lumpur", "Kuala Lumpur", "Kuala Lumpur" ) salary= arrayOf( "RM3000", "RM4000", "RM3500", "RM2000", "RM1000", "RM3000", "RM3000", "RM3000", "RM3000", "RM3000" ) news= arrayOf( getString(R.string.news_a), getString(R.string.news_b), getString(R.string.news_c), getString(R.string.news_d), getString(R.string.news_e), getString(R.string.news_f), getString(R.string.news_g), getString(R.string.news_h), getString(R.string.news_i), getString(R.string.news_j) ) newArrayList= arrayListOf<Company_Search>() tempArrayList= arrayListOf<Company_Search>() getUserdata() // Use the properly initialized newRecyclerView here newRecyclerView.apply { layoutManager= LinearLayoutManager(activity) setHasFixedSize(true) adapter = Search_Page_Adapter(newArrayList) } }
3. Optional: Clean Up Redundant Code
In your getUserdata method, you're setting the adapter a second time — this is redundant since we're already setting it in onViewCreated. You can safely remove this line if you want:
// Remove this line from getUserdata() newRecyclerView.adapter=adapter
Why This Worked in Another Fragment
The other fragment where this code worked likely had a RecyclerView with the id recyclerView in its layout, paired with the correct synthetic import for that layout. When you copied the code over, you forgot to adjust the RecyclerView id reference and import to match your search page's layout.
That's all! These changes should fix the NullPointerException and get your search fragment working smoothly.
内容的提问来源于stack exchange,提问作者Alex ong




