提问人:jass manak FaN 提问时间:10/17/2022 最后编辑:jass manak FaN 更新时间:10/21/2022 访问量:26
我正在使用viewpager,并且在viewpager的一个片段中,我附加了recylerview并得到了这个nullpointer异常
I am using a viewpager and in the one fragment of the viewpager i was attaching the recylerview and getting this nullpointer exception
问:
[Image of my code that is showing null pointer exception][1]
[1]: https://i.stack.imgur.com/z9C6o.png
the fragment code->
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
->the activity code
package com.example.mymu
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager.widget.ViewPager
import com.example.my.MusicInfo
import com.example.my.PageAdapter
import kotlinx.android.synthetic.main.activity_main2.*
class MainActivity2 : AppCompatActivity() {
companion object {
lateinit var mylist: MutableList<MusicInfo>
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
val song1= MusicInfo("Darshan Raval","Asal Main",R.drawable.asalmain,"3:10")
val song2=MusicInfo("Jass Manak","Prada", R.drawable.manak,"2:50")
val song3=MusicInfo("Darshan Raval","Asal Main",R.drawable.asalmain,"3:10")
val song4=MusicInfo("Jass Manak","Prada",R.drawable.manak,"2:50")
val song5=MusicInfo("Darshan Raval","Asal Main",R.drawable.asalmain,"3:10")
val song6=MusicInfo("Jass Manak","Prada",R.drawable.manak,"2:50")
val song7=MusicInfo("Darshan Raval","Asal Main",R.drawable.asalmain,"3:10")
val song8=MusicInfo("Jass Manak","Prada",R.drawable.manak,"2:50")
val song9=MusicInfo("Darshan Raval","Asal Main",R.drawable.asalmain,"3:10")
val song10=MusicInfo("Jass Manak","Prada",R.drawable.manak,"2:50")
val song11=MusicInfo("Darshan Raval","Asal Main",R.drawable.asalmain,"3:10")
val song12=MusicInfo("Jass Manak","Prada",R.drawable.manak,"2:50")
val song13=MusicInfo("Darshan Raval","Asal Main",R.drawable.asalmain,"3:10")
val song14=MusicInfo("Jass Manak","Prada",R.drawable.manak,"2:50")
val song15=MusicInfo("Darshan Raval","Asal Main",R.drawable.asalmain,"3:10")
val song16=MusicInfo("Jass Manak","Prada",R.drawable.manak,"2:50")
val song17=MusicInfo("Darshan Raval","Asal Main",R.drawable.asalmain,"3:10")
val song18=MusicInfo("Jass Manak","Prada",R.drawable.manak,"2:50")
val song19=MusicInfo("Darshan Raval","Asal Main",R.drawable.asalmain,"3:10")
val song20=MusicInfo("Jass Manak","Prada",R.drawable.manak,"2:50")
mylist= mutableListOf()
mylist.add(song1)
mylist.add(song2)
mylist.add(song3)
mylist.add(song4)
mylist.add(song5)
mylist.add(song6)
mylist.add(song7)
mylist.add(song8)
mylist.add(song9)
mylist.add(song10)
mylist.add(song11)
mylist.add(song12)
mylist.add(song13)
mylist.add(song14)
mylist.add(song15)
mylist.add(song16)
mylist.add(song17)
mylist.add(song18)
mylist.add(song19)
mylist.add(song20)
//this is the part where it is showing error//
val recyclerView=findViewById(R.id.rv) recyclerView.layoutManager = LinearLayoutManager(this@MainActivity2,LinearLayoutManager.VERTICAL,false) recyclerView.adapter = MyAdapter(我的列表) view_pager.adapter=PageAdapter(supportFragmentManager) tab_layout.setupWithViewPager(view_pager) tab_layout.setTabTextColors(Color.parseColor(“#7DC1C6”),resources.getColor(R.color.teal_700)) tab_layout.setBackgroundResource(R.drawable.drawable4)
}
}``
答:
0赞
Arsh
10/21/2022
#1
您说上面提供的代码是您的片段代码。您显然正在在此处访问您的 MainActivity2,因此 reyclerview 应该存在于您的 mainactivity2 xml 文件中,而不是您的片段中。如果你在片段中需要 recyclerview,你需要将你的 recyclerview 代码移动到你的 fragment 类,并且你的 mainactivity2 应该只包含视图寻呼程序
评论