如何抑制初始化警告
How to suppress initialization warnings
有没有办法抑制由于编码约定而导致的初始化?例如第 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;
}
}
显式将其设置为 null,以便编译器知道您知道自己在做什么。
View rootView = null;
这至少会消除你的错误。
这不是警告,它会在编译期间产生错误,因为您正在尝试读取局部变量 rootView
但如果 inflater.inflate
抛出异常
有没有办法抑制由于编码约定而导致的初始化?例如第 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;
}
}
显式将其设置为 null,以便编译器知道您知道自己在做什么。
View rootView = null;
这至少会消除你的错误。
这不是警告,它会在编译期间产生错误,因为您正在尝试读取局部变量 rootView
但如果 inflater.inflate
抛出异常