方法调用在Android的工作室高亮和错误 [英] Method invocation highlighed error in Android Studio
问题描述
我是一个有点困惑在Android的工作室。我didint见过这种错误在Eclipse之前。
Im a little bit confused in Android Studio. I didint seen this kind of errors in Eclipse before.
例如:
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction tf = fragmentManager.beginTransaction();
Fragment oldFragment = fragmentManager.findFragmentByTag(AddDialogFragment.TAG);
这code工作正常,但fragmentManager.beginTransaction();得到突出显示,并说: 方法调用fragmentManager.beginTransaction'可能会产生显示java.lang.NullPointerException少...(按Ctrl + F1)
this code is working fine, but fragmentManager.beginTransaction(); gets highlighted and says: Method invocation 'fragmentManager.beginTransaction' may produce 'java.lang.NullPointerException' less... (Ctrl+F1)
该检验报告在指定的检验范围总是为真或假的条件,以及所指出的,其中一个RuntimeException可能会抛出的基础上,在code数据流分析。本次检查还报告可空/ NOTNULL违反合同。注释支持合同的可配置(默认为@可空/ @ NOTNULL注释从annotations.jar将用)
This inspection reports those conditions in the specified inspection scope that are always true or false, as well as points out where a RuntimeException may be thrown, based on data flow analysis of the code. This inspection also reports Nullable/NotNull contract violations. Annotations to support the contract can be configured (by default @Nullable/@NotNull annotations from annotations.jar will be used)
我要检查空之前?
FragmentManager fragmentManager = getFragmentManager();
if(fragmentManager != null)
FragmentTransaction tf = fragmentManager.beginTransaction();
Fragment oldFragment = fragmentManager.findFragmentByTag(AddDialogFragment.TAG);
我从来没有见过这种在任何啧或实例。 如果这是一个愚蠢的问题比抱歉,但即时通讯仍然是一个新手:)
I have never seen this in any Tut or Example before. If this is a stupid question than sorry, but im still a beginner :) .
推荐答案
这是我所看到的,Android的工作室展示一些关于过分警告潜在的 NullPointerException异常
,甚至永远也不会返回null方法。我根本不理会他们中的一些,但要仔细检查所有的人是有用的,因为有时我错过了一个重要的问题。
From what I've seen, Android Studio shows a bit too much warnings about potential NullPointerExceptions
, even for methods that will never return null. I simply ignore some of them, but it's useful to carefully check all of them, because sometimes I missed an important one.
如果你看一下Android源$ C $ C,很容易看出, getFragmentManager()
不会返回NULL:
If you look at the android source code, it's easy to see that getFragmentManager()
will never return null :
public FragmentManager getFragmentManager() {
return mFragments;
}
在哪里mFragments是通过全班同学分配只有一次:
Where mFragments is assigned only once through the whole class :
final FragmentManagerImpl mFragments = new FragmentManagerImpl();
这篇关于方法调用在Android的工作室高亮和错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!