如何检查是否存在的片段? [英] How to check if the fragment exists?

查看:102
本文介绍了如何检查是否存在的片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想谈谈从活动中的片段,但我不知道,如果片段是可见的或没有。如果该片段不存在,我甚至不能做的空检查,因为它抛出由于铸造例外。

我如何检查是否存在碎片?

  PlayerFragment =(PlayerFragment)mManager.findFragmentById(R.id.bottom_container);
playerFragment.onNotificationListener.updateUI();


解决方案

不要在第一次施展它。

 片f = mManager.findFragmentById(R.id.bottom_container);
如果(F = NULL&放大器;!&安培;˚F的instanceof PlayerFragment){
    PlayerFragment playerFragment =(PlayerFragment)F;
    playerFragment.onNotificationListener.updateUI();
}

如果不交与你收到的异常堆栈跟踪工作。

I am trying to talk to the fragment from activity, but I am not sure if the fragment is visible or no. If the fragment does not exist, I cannot even do null check as it throws exception due to the casting.

How do I check if the fragment exists?

PlayerFragment = (PlayerFragment) mManager.findFragmentById(R.id.bottom_container);
playerFragment.onNotificationListener.updateUI();

解决方案

Don't cast it at first.

Fragment f = mManager.findFragmentById(R.id.bottom_container);
if(f != null && f instanceof PlayerFragment) {
    PlayerFragment playerFragment = (PlayerFragment) f;
    playerFragment.onNotificationListener.updateUI();
}

If that doesn't work post the stacktrace with the exception you are receiving.

这篇关于如何检查是否存在的片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆