错误由logcat显示。我无法重新定位我要去的地方。让我知道我的错误

时间:2019-02-02 06:34:59

标签: android android-fragments kotlin

  1. 其示出了没有图找到。但是我不明白什么。

  2. 我认为问题是在OnCreateView()的功能仅存在其中视图传递的参数。 我应该使用try和catch方法?

MainScreenFragment的代码

    class MainScreenFragment : Fragment() {
     var getsongsList: ArrayList<Songs>?=null
   var nowPlayingButtonBar: RelativeLayout?=null
  var playPauseButton: ImageButton?=null
  var songTitle: TextView?=null
  var visibleLayout: RelativeLayout?=null
  var noSongs: RelativeLayout?=null
  var recyclerView: RecyclerView?=null
var myActivate: Activity?=null
var _mainScreenAdapter: MainscreenAdapter?=null

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    // Inflate the layout for this fragment
    val view= inflater.inflate(R.layout.main_screen_fragment,container, 
      false)
    visibleLayout=view?.findViewById<RelativeLayout>(R.id.visibleLayout)
    noSongs= view?.findViewById<RelativeLayout>(R.id.noSongs)
    nowPlayingButtonBar= view?.findViewById<RelativeLayout> 
   (R.id.hiddenMainScreen)
    songTitle = view?.findViewById<TextView>(R.id.songName)
    playPauseButton= view?.findViewById<ImageButton> 
  (R.id.playPauseButton)
    recyclerView=view?.findViewById<RecyclerView>(R.id.contentMain)
   return view    }


@RequiresApi(Build.VERSION_CODES.O)
override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)
    getsongsList = getSongsFromPhone()
    _mainScreenAdapter= MainscreenAdapter(getsongsList as 
    ArrayList<Songs>, myActivate as Context)
    val mLayoutManager = LinearLayoutManager(myActivate)
    recyclerView?.layoutManager = mLayoutManager
    recyclerView?.itemAnimator = DefaultItemAnimator()
    recyclerView?.adapter = _mainScreenAdapter

}

override fun onAttach(context: Context?) {
    super.onAttach(context)
     myActivate = context as Activity

}

override fun onAttach(activity: Activity?) {
    super.onAttach(activity)
    myActivate= activity
}
    @RequiresApi(Build.VERSION_CODES.O)
    fun getSongsFromPhone(): ArrayList<Songs> {
        val arrayList =ArrayList<Songs>()
        val contentResolver = myActivate?.contentResolver
        val songUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
        val songCursor = contentResolver?.query(songUri,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 songAdded = 
          songCursor.getColumnIndex(MediaStore.Audio.Media.DATE_ADDED)
            while (songCursor.moveToNext()){
                val currentID =songCursor.getLong(songId)
                val currentTitle =songCursor.getString(songTitle)
                val currentArtist =songCursor.getString(songArtist)
                val currentData =songCursor.getString(songData)
                val currentAdded =songCursor.getLong(songAdded)
                arrayList.add(Songs(currentID, 
           currentTitle,currentArtist,currentData,currentAdded))

       songCursor.close()
            }
        }
        return arrayList



    }

      }

1 个答案:

答案 0 :(得分:0)

当您错误地获取另一个XML文件上的FrameLayout的ID,并替换该FrameLayout中的片段时,几乎找不到任何视图。只需检查一下是否要在正确的FrameLayout中替换片段即可。