androidkotlinfragmentillegalstateexception

Did not return View from onCreateView


After adding the setFragmentResultListener which i use to add data that i get from another fragment to a table , i get the folllowing logcat error:

 2021-06-11 16:45:13.689 29090-29090/com.example.nlp_expense_tracker E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.nlp_expense_tracker, PID: 29090
java.lang.IllegalStateException: Fragment HistoryFragment{dcdb784} (1d63faf6-f003-4a57-a9ee-a3ea2331063e id=0x7f0901bb tag=android:switcher:2131296699:1) did not return a View from onCreateView() or this was called before onCreateView().
    at androidx.fragment.app.Fragment.requireView(Fragment.java:1964)
    at com.example.nlp_expense_tracker.fragments.HistoryFragment.onCreate(HistoryFragment.kt:30)

I also tried adding the code for the table part in the onCreate function, but i get the same error.

This is my Fragment's code: If there is a way to write this code simpler I'm always open for suggestions as well. Just started learning to programm.

class HistoryFragment : Fragment() {

private val dataStore = ArrayList<String>()
private val dataAmount = ArrayList<String>()
private val dataDate = ArrayList<String>()

private lateinit var textview2: TextView
private lateinit var textView3: TextView
private lateinit var textView4: TextView
private lateinit var store: TextView
private lateinit var amount: TextView
private lateinit var date: TextView

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

}

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,savedInstanceState: Bundle?):
        View {val view: View = inflater.inflate(R.layout.fragment_history, container, false)

    textview2 = view.findViewById(R.id.textView2)
    textView3 = view.findViewById(R.id.textView3)
    textView4 = view.findViewById(R.id.textView4)

    val table : TableLayout = requireView().findViewById(R.id.tableHistorie)
    val row : TableRow = requireView().findViewById(R.id.tableRowOne)

    // Use the Kotlin extension in the fragment-ktx artifact
    setFragmentResultListener("requestKey") { requestKey, bundle ->
        // We use a String here, but any type that can be put in a Bundle is supported
        val result = bundle.getString("bundleKey")
        dataStore.add(result.toString())
        for(i in dataStore.indices)
        {
            val storeName = dataStore [i]
            store.text = storeName

        }
        row.addView(store)
        table.addView(row)

    }
    setFragmentResultListener("requestKey2") { requestKey, bundle ->
        // We use a String here, but any type that can be put in a Bundle is supported
        val result2 = bundle.getString("bundleKey2")
        // Do something with the result
        dataAmount.add(result2.toString())
        for(i in dataAmount.indices)
        {
            val storeName = dataAmount [i]
            amount.text = storeName

        }
        row.addView(amount)
        table.addView(row)
    }
    setFragmentResultListener("requestKey3") { requestKey, bundle ->
        // We use a String here, but any type that can be put in a Bundle is supported
        val result3 = bundle.getString("bundleKey3")
        // Do something with the result
        dataDate.add(result3.toString())
        for(i in dataDate.indices)
        {
            val storeName = dataDate [i]
            date.text = storeName

        }
        row.addView(date)
        table.addView(row)
    }


    return view
}


}

Solution

  • Logcat shows, problem comes because of calling requireView() before onCreateView() returns. Replace requireView() with view in your code:

    val table : TableLayout = view.findViewById(R.id.tableHistorie)
    val row : TableRow = view.findViewById(R.id.tableRowOne)