0
  1. Its showing that no view found. But what does that I am not able to understand.

  2. I think problem is in OnCreateView() function as there is only the parameter where view is passed. Should I use try and catch method?

code for 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



    }

      }
AskNilesh
  • 58,437
  • 15
  • 99
  • 129
  • `No view found` is usually the error you get when the `R.id` you pass in a `FragmentTransaction` doesn't exist in the host's layout. That is, the problem likely isn't in the `Fragment` itself, but where you're calling `add()`/`replace()` with the `Fragment`. – Mike M. Feb 02 '19 at 06:48
  • my new problem is that its showing java.lang.ClassNotFoundException: Didn't find class "android.support.v4.app.CoreComponentFactory" on path: DexPathList[[],nativeLibraryDirectories=[/data/app/com.example.admin.ashishapp-jwIATJTsXnuSxdnk2IkgEw==/lib/x86, /system/lib]] – Ashish Yadav Feb 08 '19 at 05:50

1 Answers1

0

No view found mostly comes when you are taking the wrong id of the FrameLayout which is on another XML file and replacing the fragment in that FrameLayout. Just check once that you are replacing your fragment in the correct FrameLayout.

Deeksha
  • 358
  • 2
  • 10