3

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
}


}

4 Answers 4

6

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)
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, it worked. But i get a new logcat error now when i want to submit the data from the other fragment to this one. The specified child already has a parent. You must call removeView() on the child's parent first: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. at android.view.ViewGroup.addViewInner(ViewGroup.java:5235) at android.view.ViewGroup.addView(ViewGroup.java:5064)
I think, calling table.addView(row) multiple times is giving this exception.
Do you know any solution for that?
If you want to add View to the same row for every result, then no need to add it inside Listeners since you've already added in xml. Just remove table.addView(row) from your listeners.
2

If you use ViewBinding and Navigation, move your code from onCreateView to onViewCreated. In my case savedStateHandle required View.

class SomeFragment : Fragment(R.layout.fragment_some) {
    private val args: SomeFragmentArgs by navArgs()
    private val binding: FragmentSomeBinding by viewBinding()

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        binding.repeatButton.setOnClickListener {
            findNavController().previousBackStackEntry
                ?.savedStateHandle
                ?.set(args.resultKey, 1)
        }

Comments

-1
private lateinit var table : TableLayout = view.findViewById(R.id.tableHistorie)
private lateinit var row : TableRow = view.findViewById(R.id.tableRowOne)

Comments

-1
val view = inflater.inflate(R.layout.fragment_history, container, false)

1 Comment

Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.