此代码:Android应用程序编程 - 不知道为什么findViewByID执行以下操作:
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_detail, container, false);
Intent intent = getActivity().getIntent();
String weather = intent.getStringExtra(Intent.EXTRA_TEXT);
if(weather != null) {
TextView textViewWeather=(TextView)rootView.findViewById(R.id.weatherText);
textViewWeather.setText(weather);
,但如果你更换,在该行
(TextView)rootView.findViewById(R.id.weatherText);
rootView
通过findView()
或getActivity
,它返回null,所以下一行创建一个运行时间致命错误,它会暂停程序。
我不明白为什么。我认为背景是活动?!我很困惑,并且无法通过android开发人员找到一个好的答案,所以我在这里...
在此先感谢谁帮助!
你可以发布xml吗? –