Android findViewById不是由片段定义的
给我一个错误 未为CityFragment类型定义findViewById方法 与: 我基本上只是从Android findViewById不是由片段定义的,android,android-fragments,android-pageradapter,Android,Android Fragments,Android Pageradapter,给我一个错误 未为CityFragment类型定义findViewById方法 与: 我基本上只是从 你知道我为什么会出错吗?那是因为片段确实没有这样的方法findViewById()。 相反,您应该使用rootView来访问它 public class CityFragment extends Fragment { public CityFragment() {} @Override public View onCreateView(LayoutInflater i
你知道我为什么会出错吗?那是因为
片段
确实没有这样的方法findViewById()
。
相反,您应该使用rootView
来访问它
public class CityFragment extends Fragment {
public CityFragment() {}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.city,
container, false);
AutoCompleteTextView autoCompView =
(AutoCompleteTextView) findViewById(R.id.autocomplete_city);
autoCompView.setAdapter(
new PlacesAutoCompleteAdapter(this, R.layout.list_item)
);
return rootView;
}
}
更改为:
AutoCompleteTextView autoCompView =
(AutoCompleteTextView)rootView.findViewById(R.id.autocomplete_city);
AutoCompleteTextView autoCompView =
(AutoCompleteTextView) rootView.findViewById(R.id.autocomplete_city);
更改:
AutoCompleteTextView autoCompView =
(AutoCompleteTextView) rootView.findViewById(R.id.autocomplete_city);
致:
这里rootView是AutoCompleteTextView的父级。因此,用以下方法进行更改:
AutoCompleteTextView autoCompView =
(AutoCompleteTextView) findViewById(R.id.autocomplete_city);
AutoCompleteTextView autoCompView =
(AutoCompleteTextView) rootView.findViewById(R.id.autocomplete_city);
AutoCompleteTextView autoCompView =
(AutoCompleteTextView) rootView.findViewById(R.id.autocomplete_city);