gpt4 book ai didi

android - Activity 在后台被杀死后应用程序崩溃

转载 作者:太空宇宙 更新时间:2023-11-03 13:39:46 26 4
gpt4 key购买 nike

我对使用 ViewPager 显示 fragment 的应用程序有疑问。一切正常,直到应用程序进入后台并被操作系统杀死。似乎在恢复后我有 2 个处理事件的 IncidentScreenFragment,其中一个带有使我的应用程序崩溃的空演示者 (MVP)。

我的 HomeActivity 看起来像:

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

setContentView(R.layout.activity_main)
presenter.onViewCreated()
initViews(savedInstanceState)
}

private fun initViews(savedInstanceState: Bundle?){
mapView.onCreate(savedInstanceState)
mapView.getMapAsync(this)
initFragment()
initMenu()
}
private fun initFragment(){
homeFragment = HomeScreenFragment.newInstance()
incidentFragment = IncidentScreenFragment.newInstance()
chatFragment = ChatFragment.newInstance()
weatherFragment = WeatherFragment.newInstance()

viewPager.adapter = ViewPagerAdapter(supportFragmentManager, this)
viewPager.offscreenPageLimit = 4

viewPager?.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
override fun onPageScrollStateChanged(state: Int) {}
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}
override fun onPageSelected(position: Int) {bottom_navigation.currentItem = position}
})
}

override fun getFragmentByPos(pos: Int): Fragment {
return when(pos){
0 -> homeFragment
1 -> incidentFragment
2 -> chatFragment
3 -> weatherFragment
else -> {
homeFragment
}
}
}

还有我的适配器:

class ViewPagerAdapter internal constructor(fm: FragmentManager, activity:infinite_software.intelligence_center.intelligencecenter.ui.home.FragmentManager) : FragmentPagerAdapter(fm) {

private val COUNT = 4
private val activity = activity

override fun getItem(position: Int): Fragment{
var fragment: Fragment? = null
when (position) {
0 -> fragment = activity.getFragmentByPos(0)
1 -> fragment = activity.getFragmentByPos(1)
2 -> fragment = activity.getFragmentByPos(2)
3 -> fragment = activity.getFragmentByPos(3)
}

return fragment!!
}

override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
super.destroyItem(container, position, `object`)
}

override fun getCount(): Int {
return COUNT
}

override fun getPageTitle(position: Int): CharSequence? {
return "Section " + (position + 1)
}
}

每个 fragment 都有一个返回新 fragment 的静态方法:

    companion object {
fun newInstance(): HomeScreenFragment {
return HomeScreenFragment()
}
}

当应用程序在后台被杀死时,我发现有 2 个对象( fragment )监听事件,一个正确实例化了 Presenter,一个没有正确实例化。

在我的抽象 BaseFragment 类下面:

abstract class BaseFragment<P : BasePresenter<BaseView>> : BaseView,Fragment() {
protected lateinit var presenter: P

override fun getContext(): Context {
return activity as Context
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return super.onCreateView(inflater, container, savedInstanceState)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
presenter = instantiatePresenter()
}

override fun showError(error: String) {
(activity as BaseActivity<BasePresenter<BaseView>>).showError(error)
}

override fun showError(errorResId: Int) {
(activity as BaseActivity<BasePresenter<BaseView>>).showError(errorResId)
}

abstract fun onBackPressed(): Boolean

/**
* Instantiates the presenter the Fragment is based on.
*/
protected abstract fun instantiatePresenter(): P
abstract val TAG: String

事件 fragment 代码:

class IncidentScreenFragment: BaseFragment<IncidentScreenPresenter>(), BaseView, IncidentView, AlertFilterListener, AlertItemClickListener, IncidentDetailListener {

var rvAdapter : IncidentAdapter? = null

var state : Int = LIST_STATE

override fun instantiatePresenter(): IncidentScreenPresenter {
return IncidentScreenPresenter(this)
}

override val TAG: String
get() = "INCIDENT"

override fun getContext(): Context {
return activity as Context
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
super.onCreateView(inflater, container, savedInstanceState)
return inflater.inflate(R.layout.fragment_incident, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
initViews()
presenter.onViewCreated()
initObserve()
}

private fun initViews(){
//Reclycler view
alertRV.layoutManager = LinearLayoutManager(context)
rvAdapter = IncidentAdapter(ArrayList(), context, this)
alertRV.adapter = rvAdapter

//Apply Listeners
headerBox.setFilterListener(this)
incidentDetailView.setListener(this)
}

override fun initObserve() {
//Init observe presenter model
val alertObserver = Observer<ArrayList<AlertModel>> { alerts ->
Timber.d("Data received from Presenter [$alerts]")
showAlertList(alerts)
}
presenter.filteredAlertList.observe(context as BaseActivity<BasePresenter<BaseView>>,alertObserver)
}

override fun updateThisFilters(boxState: Boolean, level: Int) {
presenter.updateFilterList(boxState,level)
}

fun showOnlyThisLevel(level:Int){
presenter.showOnlyThisLevel(level)
headerBox.disableBoxExcept(level)
}

fun showAlertList(list: ArrayList<AlertModel>){
rvAdapter?.updateData(list)
}

override fun onItemClick(model: AlertModel) {
presenter.loadAlertDetail(model)
}

override fun showAlertDetail(model: AlertModel) {
incidentDetailView.setUpFromModel(model)
WhiteWizard.slideLeftEffect(incidentDetailView,incidentListRootElement)
state = DETAIL_STATE
}

override fun onbackFromDetailPressed() {
WhiteWizard.slideRightEffect(incidentListRootElement,incidentDetailView)
state = LIST_STATE
}

override fun showLoader() {
loaderIncident.visibility = View.VISIBLE
}

override fun hideLoader() {
loaderIncident.visibility = View.INVISIBLE
}

override fun onBackPressed(): Boolean {
when(state){
LIST_STATE -> return false
DETAIL_STATE -> {
onbackFromDetailPressed()
return true
}
else -> return false
}
}

fun newInstance(): IncidentScreenFragment {
return IncidentScreenFragment()
}

}

当我点击主页中的按钮显示我得到的 fragment 内容时:

 Process: XXXXXX, PID: 3192
kotlin.UninitializedPropertyAccessException: lateinit property presenter has not been initialized
at infinite_software.intelligence_center.intelligencecenter.base.BaseFragment.getPresenter(BaseFragment.kt:11)
at XXXXXX.ui.home.incidentScreen.IncidentScreenFragment.showOnlyThisLevel(IncidentScreenFragment.kt:78)
at XXXXXX.ui.home.HomeActivity.filterDataWithSeverity(HomeActivity.kt:110)
at XXXXXX.ui.home.homeScreen.HomeScreenFragment.filterBy(HomeScreenFragment.kt:76)
at XXXXXX.ui.home.homeScreen.HomeScreenFragment$initViews$5.onClick(HomeScreenFragment.kt:56)

如果我尝试打印 fragment 的 ID,我会从方法调用 showOnlyThisLevel() 和 onBackPressed() 获得 2 个不同的 ID。我想念什么?

最佳答案

经过一些研究,问题似乎源于 FragmentPagerAdapter 的方法命名错误 - 被命名为 getItem(),但没有明确指定抽象getItem(int position) 方法应该返回一个 fragment 的新实例,而不仅仅是“获取一个 fragment 的实例”。

当然,对于已经出现 7 年的错误名称,我们无能为力,但至少我们可以在您的代码中修复由此问题引起的错误;)


事不宜迟,您的 NPE 的原因是 onCreateView(您的 Presenter 被实例化的地方)从未被调用。

发生这种情况是因为您正在此处创建 fragment :

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

setContentView(R.layout.activity_main)
...
homeFragment = HomeScreenFragment.newInstance()
incidentFragment = IncidentScreenFragment.newInstance()
}

您从 FragmentPagerAdapter 中的 getItem(int position) 内部返回此 fragment :

override fun getItem(position: Int): Fragment = when(position) {
...
1 -> activity.incidentFragment
...
}

所以我们对 activity.incidentFragment 的了解是,在其中,onCreateView() 从未被调用。

这是因为它从未真正添加到 FragmentManager 中,也从未显示在屏幕上。

那是因为 Activity 中的 super.onCreate(savedInstanceState) 通过反射使用它们的无参数构造函数重新创建所有 Fragment,同时保留它们的标记(请参阅 findFragmentByTag )

正如您在 this answer 中看到的那样,或者我可以在这里引用:

    // Do we already have this fragment?
String name = makeFragmentName(container.getId(), itemId);
Fragment fragment = mFragmentManager.findFragmentByTag(name);
if (fragment != null) {
if (DEBUG) Log.v(TAG, "Attaching item #" + itemId + ": f=" + fragment);
mCurTransaction.attach(fragment);
} else {
fragment = getItem(position);
if (DEBUG) Log.v(TAG, "Adding item #" + itemId + ": f=" + fragment);
mCurTransaction.add(container.getId(), fragment,
makeFragmentName(container.getId(), itemId));

getItem(position) 方法只有在 FragmentPagerAdapter 为 fragment 设置的 fragment 标签找不到 fragment 时才会调用,在低内存条件杀死您的应用程序后会自动重新创建 fragment 。

因此,您的新 fragment (您在 Activity 中手动创建的 fragment )从未被使用过,因此它没有 View ,从未被初始化,从未被添加到 FragmentManager,它与您的 ViewPager 中的实际实例不同,并且当你调用它时它会崩溃。轰!



解决方案是在 FragmentPagerAdapter 的 getItem(position) 方法中实例化 fragment 。要获取 fragment 的实例,请使用 this answer .

关于android - Activity 在后台被杀死后应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55753099/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com