在Kotlin中的types转换错误:ArrayList

我正在创建一个音乐播放器应用程序,其中有一个回收视图和许多碎片。 我在MainScreenFragment.kt文件中遇到错误。 我检查了错误日志,错误的显示是:

******引起:kotlin.TypeCastException:null不能转换为非nulltypeskotlin.collections.ArrayList / * = java.util.ArrayList * /
在com.thepanku.musicplayer.Fragments.MainScreenFragment.onCreateView(MainScreenFragment.kt:60)at android.support.v4.app.Fragment.performCreateView(Fragment.java:2354)******

错误的原因是什么,任何帮助都是可观的。 我厌倦了这个错误。

这里是代码:

class MainScreenFragment : Fragment() { var getSongList : ArrayList? = null var nowPlayingBottomBar: RelativeLayout?=null var playPauseButton: ImageView?=null var songTitle: TextView?=null var visibleLayout: RelativeLayout?=null var noSongs: RelativeLayout?=null var recyclerView: RecyclerView?= null var myActivity:Activity?=null var _mainScreenAdapter : MainScreenAdapter?=null override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? { val view = inflater?.inflate(R.layout.content_main, container, false) setHasOptionsMenu(true) activity.title = "All songs" visibleLayout = view?.findViewById(R.id.visibleLayout) noSongs = view?.findViewById(R.id.noSongs) nowPlayingBottomBar = view?.findViewById(R.id.hiddenBarMainScreen) songTitle = view?.findViewById(R.id.songTitleMainScreen) playPauseButton = view?.findViewById(R.id.playpauseButton) (nowPlayingBottomBar as RelativeLayout).isClickable = false recyclerView = view?.findViewById(R.id.contentMain) visibleLayout?.visibility = View.INVISIBLE noSongs?.visibility = View.VISIBLE //getting error on this line-> _mainScreenAdapter = MainScreenAdapter(getSongList as ArrayList, activity) val mLayoutManager = LinearLayoutManager(activity) (recyclerView as RecyclerView).layoutManager = mLayoutManager (recyclerView as RecyclerView).itemAnimator = DefaultItemAnimator() (recyclerView as RecyclerView).adapter = _mainScreenAdapter return view } fun getSongsFromPhone(): ArrayList{ var arrayList = ArrayList() var contentResolver = myActivity?.contentResolver var songUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI var songCursor = contentResolver?.query(songUri, null, null, null, null) if(songCursor!=null && songCursor.moveToFirst()){ val songId = songCursor.getColumnIndex(MediaStore.Audio.Media._ID) val SongTitle = songCursor.getColumnIndex((MediaStore.Audio.Media.TITLE)) val songArtist = songCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST) val songData = songCursor.getColumnIndex(MediaStore.Audio.Media.DATA) val dateIndex = songCursor.getColumnIndex(MediaStore.Audio.Media.DATE_ADDED) while(songCursor.moveToNext()){ var currentId = songCursor.getLong(songId) var currentTitle = songCursor.getString(SongTitle) var currentArtist = songCursor.getString(songArtist) var currentData = songCursor.getString(songData) var currentDate = songCursor.getString(dateIndex) } } return arrayList } override fun onActivityCreated(savedInstanceState: Bundle?) { super.onActivityCreated(savedInstanceState) getSongList = getSongsFromPhone() } override fun onAttach(context: Context?) { super.onAttach(context) myActivity = context as Activity } override fun onAttach(activity: Activity?) { super.onAttach(activity) myActivity = activity } 

} //必须为空的公共构造函数

你有

 var getSongList : ArrayList? = null 

然后你说

 getSongList as ArrayList 

显然, getSongListnull ,但是你坚持把它转换成非空types的ArrayList 。 你的希望是它会在onCreateActivity被初始化,但显然这个回调还没有到达onCreateView被调用的时候。

我很遗憾听到你厌倦了这个错误,并希望你能够为你的getSongList提供一个非null值,例如调用getSongsFromPhone()