我试图制作一个 RecyclerView,它将进入我 fragment 的两个页面之一。这些页面放在 NavigationDrawer Activity 中。目标是创建类似于 Play 商店应用主页的内容。
但是我在运行时发现这段代码有错误。它说:
java.lang.IllegalStateException: mainMenu must not be null
at com.example.MyApp.app.fragment.MainFragment.onCreate(MainFragment.kt:49)
我一直在查看一些 SO 线程,他们说布局未正确加载。这导致某些元素未按应有的方式链接。另一位在评论中说,问题在于上下文未正确初始化。对我来说不是这样(而是 RecyclerView)。
这是链接,希望它们能作为引用。
在对我的代码进行多次检查后,我发誓我已经放入了正确的布局。编辑:我导入了 kotlinx.android.synthetic.main.^ 我放这个标志的包:^${layout}。这是我的一些文件(如果这个线程变得太长请原谅我):
MainActivity.kt : AppCompatActivity() ^activity_main.*
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// preparing the app bars
setSupportActionBar(toolBar)
// getting ready for the pages
val pagerAdapter = MainPagerAdapter(
supportFragmentManager,
resources.getString(R.string.tab_main),
resources.getString(R.string.tab_chat)
)
pager.adapter = pagerAdapter
// activating tabs
tabLayout.setupWithViewPager(pager)
val toggle = ActionBarDrawerToggle(
this, mainDrawer, toolbar,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close)
mainDrawer.addDrawerListener(toggle)
navView.setNavigationItemSelectedListener(this)
toggle.syncState()
}
MainPagerAdapter.kt (fm: FragmentManager, private val page1: String, private val page2: String): FragmentPagerAdapter(fm)
override fun getItem(position: Int): Fragment? {
return when (position) {
0 -> MainFragment()
1 -> ChatFragment()
else -> null
}
}
override fun getCount() = 2
override fun getPageTitle(position: Int): CharSequence? {
return when (position) {
0 -> page1
1 -> page2
else -> null
}
}
MainFragment.kt : Fragment() ^content_main.*
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?)
: View? = inflater.inflate(R.layout.content_main, container)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// this is the error
mainMenu.layoutManager = LinearLayoutManager(this.context)
mainMenu.adapter = MyAdapter(itemList) {
toast("${it.name} selected")
}
}
MyAdapter.kt: RecyclerView.Adapter<MyAdapter.MyHolder>() ^item_custom.view.*(由 Antonio Leiva 提供)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int)
= GoodsHolder(parent.inflate(R.layout.item_custom))
override fun getItemCount()
= itemList.size
override fun onBindViewHolder(holder: MyHolder, position: Int)
= holder.bind(itemList[position], listener)
class MyHolder(v: View): RecyclerView.ViewHolder(v){
private val item: Item? = null
private val view = v
fun bind(item: Item, listener: (Item) -> Unit)
= with (itemView) {
imgPic.setImageResource(item.pictureId)
txtName.text = item.name
txtPrice.text = item.price.toString()
setOnClickListener { listener(item) }
}
}
content_main.xml
<android.support.constraint.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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.MyApp.app.activity.MainActivity"
>
<!-- the RecyclerView that caused the runtime error -->
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mainMenu"/>
</android.support.constraint.ConstraintLayout>
item_custom.xml(这些代码位于 CardView 内的 LinearLayout 内)
<ImageView
android:id="@+id/imgPic"
android:layout_width="match_parent"
android:layout_height="128dp"
app:srcCompat="@drawable/ic_menu_gallery" />
<TextView
android:id="@+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get it while it's hot!"
android:layout_margin="@dimen/margin_small"
android:layout_marginTop="@dimen/margin_medium"
android:maxLines="2"
android:ellipsize="end"
android:textStyle="bold"
/>
<TextView
android:id="@+id/txtPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="$3.000.000"
android:layout_marginLeft="@dimen/margin_small"
android:layout_marginStart="@dimen/margin_small"
android:layout_marginRight="@dimen/margin_small"
android:layout_marginEnd="@dimen/margin_small"
android:layout_marginBottom="@dimen/margin_medium"/>
ChatFragment.kt:Fragment()(仅包含 onCreateView 膨胀 content_main_chat.xml)
content_main_chat.xml(只包含一个 TextView)
最佳答案
您还没有在 MainFragment.kt 文件中初始化 RecyclerView。您必须在以下行之前对其进行初始化:
mainMenu.layoutManager = LinearLayoutManager(this.context)
您可以通过以下行初始化 RecyclerView:
var mainMenu = findViewById(R.id.mainMenu) as RecyclerView
您必须根据需要更改它。
关于android - 非法状态异常 : RecyclerView is null inside of Fragment within NavigationDrawer activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48459010/