简体   繁体   English

如何取消初始化警告

[英]How to suppress initialization warnings

Is there a way to suppress initialization due to coding conventions? 由于编码约定,有没有办法抑制初始化? For example line 17 has an initialization warning. 例如,第17行具有初始化警告。

 public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ArrayList<String> EntryText;
        ArrayAdapter<String> listviewAdapter;
        View rootView;

        try {
            rootView = inflater.inflate(R.layout.fragment_main, container, false);
        }
        catch (Exception e) {
            Log.e("Sunshine Exception(onCreateView)", Integer.toString(Thread.currentThread().getStackTrace()[2].getLineNumber()), e);
        }
        return rootView;
    }
}

Set it to null explicitly so the compiler knows you know what you're doing. 将其显式设置为null,以便编译器知道您在做什么。

View rootView = null;

This will at least get rid of your error. 这至少可以消除您的错误。

这不是警告,它会在编译期间产生错误,因为您尝试读取局部变量rootView但是如果inflater.inflate抛出异常,它可能最终没有值。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM