【问题标题】:Android Kotlin fragment's onCreate not called未调用 Android Kotlin 片段的 onCreate
【发布时间】:2021-07-24 18:03:08
【问题描述】:

我正在尝试显示Fragment,但我不知道为什么我不能这样做。这是我的代码:

main_activity.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".view.MainActivity">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/fragment_container" />

</androidx.constraintlayout.widget.ConstraintLayout>

后跟MainActivity.kt

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        super.onCreate(savedInstanceState, persistentState)
        setContentView(R.layout.main_activity)

        supportFragmentManager.beginTransaction().apply {
            replace(R.id.fragment_container, CurrencyListFragment())
            addToBackStack(null)
            commit()
        }
    }
}

布局currency_list_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".view.CurrencyListFragment">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rvItem"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"/>

    <ProgressBar
        android:id="@+id/progress_bar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="visible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

另一个班级CurrencyListFragment.kt

class CurrencyListFragment : Fragment(), MainContract.View {

    private val restModel: RestModel = RestModel()
    lateinit var mainPresenter: MainPresenter
    var isLoading: Boolean = false
    var apiResponseList: MutableList<ApiResponse> = arrayListOf()
    lateinit var itemAdapter: ItemAdapter
    var handler: Handler = Handler()
    lateinit var _layoutManager: LinearLayoutManager

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.e("a","a")
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        Log.e("a","a")
        return inflater.inflate(R.layout.currency_list_fragment,container,false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        Log.e("a1","a1")
        _layoutManager = LinearLayoutManager(activity)
        mainPresenter = activity?.let { MainPresenter(this, it.getPreferences(Context.MODE_PRIVATE)) }!!
        val currentDate = mainPresenter.convertDate()
        mainPresenter.makeACall("2021-07-22")
        addScrollerListener()
    }


    private fun addScrollerListener() {
        rvItem.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrollStateChanged(_rvItem: RecyclerView, newState: Int) {
                super.onScrollStateChanged(_rvItem, newState)
                if (!isLoading) {
                    if (!_rvItem.canScrollVertically(1)) {
                        loadMore()
                        isLoading = true
                    }
                }
            }
        })
    }


    private fun loadMore() {
        //notify adapter using Handler.post() or RecyclerView.post()
        handler.post {
            apiResponseList.add(ApiResponse("", "", listOf(Currency2("", 0f)), true))
            itemAdapter.notifyItemInserted(apiResponseList.size - 1)
        }
        handler.postDelayed({
            apiResponseList.removeAt(apiResponseList.size - 1)
            val listSize = apiResponseList.size
            itemAdapter.notifyItemRemoved(listSize)
            val nextLimit = listSize + 1
            for (i in listSize until nextLimit) {
                apiResponseList.add(
                    ApiResponse("", "2020-06-11", listOf(Currency2("a", 2f)), false)
                )
            }
            itemAdapter.notifyDataSetChanged()
            isLoading = false
        }, 2000)
    }

    override fun onDestroy() {
        super.onDestroy()
        restModel.cancelJob()
    }
}

onCreate()onCreateView()onViewCreated() 都没有被调用(我想这是因为 Logs 文本没有显示在 Logcat 中。我想我添加了解决我的问题所需的一切,但如果我错了,你需要更多的东西就问吧。为什么会这样?

非常感谢您的帮助!

【问题讨论】:

  • 你确定没有logcat的登录错误部分吗?
  • 是的,我 100% 确定我也切换到 Logcat 以显示错误日志(因为我已经添加了此类)你知道可能是什么原因吗?

标签: android kotlin android-fragments


【解决方案1】:

原来我在扩展 AppCompatActivity 的类上覆盖了错误的 onCreate 方法。

【讨论】:

  • 那就这么定了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多