title: Android Kotlin: Custom view causing crash on changing fragments
body: <p>In my project I have three fragments attached to an activity, and one of these fragments, again, fragments that display texts using custom views. So they are basically fragments in a fragment.</p>
  <p>In the main fragment I put these codes to switch between text-displaying fragments. It is triggered on codes passed by its child fragments.</p>
  <pre><code>private fun diagCommence(target: Fragment) {
      val transaction = activity?.supportFragmentManager?.beginTransaction()
      if (transaction != null) {
          transaction.replace(R.id.ig1_Diag_Layout, target)
          transaction.disallowAddToBackStack()
          transaction.commit()
      }
  }
  </code></pre>
  <p>And the text-displaying fragments has custom views - AppCompatTextView - that displays texts.</p>
  <pre><code>override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
      return inflater.inflate(R.layout.layoutx2, container, false)
  }
  override fun onStart() {
      super.onStart()
      val scene = getString(R.string.sceneX2)
      tv01.commenceOnLifecycle(SceneX2(), scene, textanimspeed.toLong(), textanim) // the custom view that displays texts, triggers NPE
  }
  </code></pre>
  <p>I have three identical text-displaying fragments for now and the first text-displaying fragment shows up okay. But when I try pass value to the main fragment and trigger diagCommence() the app crashes. What was strange is that when I try to get to the second text-displaying fragment it gives me Null Pointer Exception.</p>
  <p>According the log the error was invoked in commenceOnLifecycle() method, which calls the AppCompatTextView I made. Moving it to different lifecycles such as onCreate(), onViewCreated() seems not help this problem. I'm wondering what am I missing?</p>
  <p>Edit: The layout XML of the SceneX2, which is a text-displaying fragment:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      android:id=&quot;@+id/x2Layout&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      android:orientation=&quot;vertical&quot;&gt;
  
  &lt;com.example.textia1.TypeWriterView
      android:id=&quot;@+id/x2Twv01&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;wrap_content&quot;
      android:textColor=&quot;@color/colorAccent&quot; /&gt;
  
  &lt;TextView
      android:id=&quot;@+id/x2Choice01&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;wrap_content&quot;
      android:text=&quot;@string/X2toX1&quot; /&gt;
  
  &lt;TextView
      android:id=&quot;@+id/x2Choice02&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;wrap_content&quot;
      android:text=&quot;@string/X2toX3&quot; /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  <p>and the custom TextView that displays text with typewriting animation:</p>
  <pre><code>class TypeWriterView: AppCompatTextView {
    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
  
    suspend fun commence(txt: CharSequence?, delay: Long, animOnOff: Boolean) {
      if (txt != null) {
          if(animOnOff) {
              var index = 0
              while (index &lt; txt.length) {
                  text = txt.subSequence(0, ++index)
                  delay(delay)
              }
          } else {text = txt}
      } else return
    }
    fun commenceOnLifecycle(owner: LifecycleOwner, txt: CharSequence?, delay: Long, animOnOff: Boolean): Job =
      owner.lifecycleScope.launch { commence(txt, delay, animOnOff) }
  }
  </code></pre>