简体   繁体   English

findViewById()可能会产生NullPointerException

[英]findViewById() may produce NullPointerException

I have many of these calls: 我有很多这样的电话:

(ListView) getView().findViewById(R.id.main_list_view);
(TextView) getView().findViewById(R.id.items_no);
....

and AndroidStudio tells me that they may procude a NullPointerException : 和AndroidStudio告诉我他们可能会产生一个NullPointerException

Method invocation getView().findViewById(R.id.main_list_view) may produce java.lang.NullPointerException less... (Ctrl+F1) 方法调用getView().findViewById(R.id.main_list_view)可能会产生java.lang.NullPointerException less ...(Ctrl + F1)

This inspection analyzes method control and data flow to report possible conditions that are always true or false, expressions whose value is statically proven to be constant, and situations that can lead to nullability contract violations. 此检查分析方法控制和数据流,以报告始终为真或假的可能条件,静态证明其值为常量的表达式,以及可能导致可违反合同违约的情况。

Variables, method parameters and return values marked as @Nullable or @NotNull are treated as nullable (or not-null, respectively) and used during the analysis to check nullability contracts, eg report possible NullPointerException errors. 标记为@Nullable@NotNull变量,方法参数和返回值被视为可空(或分别为非空),并在分析期间用于检查可空性合同,例如报告可能的NullPointerException错误。

More complex contracts can be defined using @Contract annotation, for example: 可以使用@Contract注释定义更复杂的合同,例如:

@Contract("_, null -> null") — method returns null if its second argument is null @Contract("_, null -> null; _, !null -> !null") — method returns null if its second argument is null and not-null otherwise @Contract("_, null -> null") - 如果第二个参数为null,则方法返回null @Contract("_, null -> null; _, !null -> !null") - 如果第二个参数为第二个,则返回null参数为null,否则为null

@Contract("true -> fail") — a typical assertFalse method which throws an exception if true is passed to it @Contract("true -> fail") - 一个典型的assertFalse方法,如果将true传递给它,则抛出异常

The inspection can be configured to use custom @Nullable @NotNull annotations (by default the ones from annotations.jar will be used) 可以将检查配置为使用自定义@Nullable @NotNull注释(默认情况下将使用annotations.jar中的注释)

Luckily everithing works, but is there an improvement to this code I can made? 幸运的是,有作品,但是我可以对这段代码进行改进吗?

This is a known issue in android.support.v7.app.AppCompatActivity and it has been fixed in v24. 这是android.support.v7.app.AppCompatActivity中的一个已知问题,它已在v24中修复。

https://code.google.com/p/android/issues/detail?id=203345 https://code.google.com/p/android/issues/detail?id=203345

You won't have any issues with android.support.v4.app.FragmentActivity or android.app.Activity android.support.v4.app.FragmentActivity或android.app.Activity不会有任何问题

You should ignore the problem; 应该忽略这个问题;

As @DanDar3 wrote -> getView() can return null and AndroidStudio highlights that. 正如@DanDar3写的那样 - > getView()可以返回null并且AndroidStudio突出显示。

But if you really want to make AndroidStudio happy - sure you can...: 但是如果你真的想让AndroidStudio感到高兴 - 你可以......:
Just assert view is not null: 断言视图不是null:

View view = getView();
assert view != null;
(ListView) view.findViewById(R.id.main_list_view);
(TextView) view.findViewById(R.id.items_no);

That is cause getView() may return null and is annotated as @Nullable , check out the sources and its JavaDoc - CTRL+Click on getView() call in your code. 这是因为getView()可能返回null并注释为@Nullable ,请查看源代码及其JavaDoc - CTRL +单击代码中的getView()调用。

/**
 * Get the root view for the fragment's layout (the one returned by {@link #onCreateView}),
 * if provided.
 * 
 * @return The fragment's root view, or null if it has no layout.
 */
@Nullable
public View getView() {
    return mView;
}

You can wrap your code yourself and check for null to have the warning go away, or otherwise place the cursor anywhere inside findViewById() call, wait couple of seconds for the lightbulb to show up (or press Alt+Enter) and then choose one of the suggested solutions. 您可以自己包装代码并检查null以使警告消失,或者将光标放在findViewById()调用内的任何位置,等待几秒钟以显示灯泡(或按Alt + Enter)然后选择一个建议的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 findViewById() 可能会为 HomeFragment.java 中的 getView() 产生 NullPointerException - findViewById() may produce NullPointerException for getView() in HomeFragment.java ParcelableArraylist可能在片段中产生nullpointerexception - ParcelableArraylist may produce nullpointerexception in fragment 方法调用可能会产生“NullPointerException” - Method invocation may produce 'NullPointerException' 方法调用可能会产生 NullPointerException Retrofit Body - Method invocation may produce NullPointerException Retrofit Body 为什么ListView上的setAdapter()会产生NullPointerException? - Why may setAdapter() on my ListView produce a NullPointerException? 警告:方法调用getAssets可能会产生nullpointerexception - Warning : Method invocation getAssets may produce nullpointerexception 不推荐使用getDrawable(int),它可能会产生NullPointerException - getDrawable(int) is deprecated and may produce a NullPointerException springboot 对 redisTemplate 的拆箱。 可能产生 NullPointerException - springboot Unboxing of redisTemplate. may produce NullPointerException 方法调用等于可能会产生java NullpointerException - Method invocation equals may produce java NullpointerException editText.getText()可能会产生NullPointerException - editText.getText() may produce NullPointerException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM