没有 return 从 onCreateView 查看

Did not return View from onCreateView

添加 setFragmentResultListener 后,我用它来将从另一个片段获取的数据添加到 table,我得到以下 logcat 错误:

 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)

我也尝试在 onCreate 函数中添加 table 部分的代码,但我得到了同样的错误。

这是我的片段代码: 如果有一种方法可以更简单地编写此代码,我也随时欢迎您提出建议。 刚开始学编程。

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
}

}

Logcat 显示,问题是因为在 onCreateView() returns 之前调用了 requireView()。在您的代码中将 requireView() 替换为 view

val table : TableLayout = view.findViewById(R.id.tableHistorie)
val row : TableRow = view.findViewById(R.id.tableRowOne)
private lateinit var table : TableLayout = view.findViewById(R.id.tableHistorie)
private lateinit var row : TableRow = view.findViewById(R.id.tableRowOne)
val view = inflater.inflate(R.layout.fragment_history, container, false)