How do I get the View in a Fragment?

0

I'm starting to program on Android with Kotlin and I'm a bit confused with the Null Safety.

The main problem comes when wanting to do a findViewById from the getView () (view in kotlin) in a fragment, I have been consulted and I have only found the following form:

 fun setUpRecyclerView() {
    mRecyclerView = view?.findViewById<View>(R.id.recyclerView) as RecyclerView
    mRecyclerView.setHasFixedSize(true)
    mRecyclerView.layoutManager = LinearLayoutManager(this.activity)
    mAdapter.RecyclerAdapter(getMeals(), this.activity!!)
    mRecyclerView.adapter = mAdapter
}

In theory if I'm not wrong with view? .findViewById I get the view of the Activity linked to this fragment.

Although the IDE forces me to put the? in front of the view and when I try to get the RecyclerView it gives me the following error:

  

E / AndroidRuntime: FATAL EXCEPTION: main                     Process: com.chdzma.changeproject, PID: 29649                     kotlin.TypeCastException: null can not be cast to non-null type android.support.v7.widget.RecyclerView                         at com.chdzma.changeproject.fragments.Meals.setUpRecyclerView (MealsFragment.kt: 51)                         at com.chdzma.changeproject.fragments.Meals.onCreate (MealsFragment.kt: 47)                         at android.support.v4.app.Fragment.performCreate (Fragment.java:2331)                         at android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManager.java:1386)                         at android.support.v4.app.FragmentTransition.addToFirstInLastOut (FragmentTransition.java:1188)                         at android.support.v4.app.FragmentTransition.calculateFragments (FragmentTransition.java:1071)                         at android.support.v4.app.FragmentTransition.startTransitions (FragmentTransition.java:115)                         at android.support.v4.app.FragmentManagerImpl.executeOpsTogether (FragmentManager.java:2380)                         at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute (FragmentManager.java:2338)                         at android.support.v4.app.FragmentManagerImpl.execPendingActions (FragmentManager.java:2245)                         at android.support.v4.app.FragmentManagerImpl $ 1.run (FragmentManager.java:703)                         at android.os.Handler.handleCallback (Handler.java:789)                         at android.os.Handler.dispatchMessage (Handler.java:98)                         at android.os.Looper.loop (Looper.java:164)                         at android.app.ActivityThread.main (ActivityThread.java:6809)                         at java.lang.reflect.Method.invoke (Native Method)                         at com.android.internal.os.Zygote $ MethodAndArgsCaller.run (Zygote.java:240)                         at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:767)

I've been looking for kotlin but I can not find the way to work for me or the best way to do it.

    
asked by chdzma 28.04.2018 в 17:36
source

1 answer

2

The problem is that at the time of the casting, the compiler interprets that the cast will be a non-null object:

view?.findViewById<View>(R.id.recyclerView) 
  as
     RecyclerView; //<-- No esta marcado como nullable por lo que si retorna null, lanza el error

Use the secure cast operator as? to avoid the error :

//...
 mRecyclerView = view?.findViewById<View>(R.id.recyclerView) as? RecyclerView
///
    
answered by 28.04.2018 / 21:28
source