我试图在片段中填充我的列表,但它返回空对象引用。(Function2
)
有我的代码:
Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
答案 0 :(得分:1)
您需要将需要视图的内容移动到onCreateView:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
person.add(new Gender("35AE111", false, "60 km/h"));
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_all_vehicles, container, false);
final ListView listview= (ListView)view.findViewById(R.id.liste);
CustomAdapter customAdapter=new CustomAdapter(getActivity(), kisiler);
listview.setAdapter(customAdapter);
return view;
}
答案 1 :(得分:0)
问题是Fragment
方法中尚未创建onCreate
的视图。您应该将代码从onCreate
移至onCreateView
:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_all_vehicles, container, false);
final ListView listview= (ListView)view.findViewById(R.id.liste);
CustomAdapter customAdapter=new CustomAdapter(getActivity(), kisiler);
listview.setAdapter(customAdapter);
return view;
}
答案 2 :(得分:0)
由于您在findviewbyid(int)
中呼叫onCreate()
并在onCreateView()
中充气布局而引起的异常....!
问题是片段的onCreate()
方法首先调用,之后片段生命周期调用onCreateView ......!
下面的代码可以帮助您,
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_all_vehicles, container, false);
person.add(new Gender("35AE111", false, "60 km/h"));
final ListView listview= (ListView)view.findViewById(R.id.liste);
CustomAdapter customAdapter=new CustomAdapter(getActivity(), kisiler);
listview.setAdapter(customAdapter);
return view;
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
答案 3 :(得分:0)
由于在onCreateView()之前调用onCreate()并且onCreate中的getView()返回null,因此发生了null ptr异常。试试下面的代码片段。我已将onCreate()中的代码移动到onCreateView()
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_all_vehicles, container, false);
person.add(new Gender("35AE111", false, "60 km/h"));
final ListView listview= (ListView)getView().findViewById(R.id.liste);
CustomAdapter customAdapter=new CustomAdapter(getActivity(), kisiler);
listview.setAdapter(customAdapter);
return view;
}
答案 4 :(得分:0)
要确保您的观看次要曝光,您可以覆盖onViewCreated
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
person.add(new Gender("35AE111", false, "60 km/h"));
final ListView listview= (ListView) view.findViewById(R.id.liste);
CustomAdapter customAdapter = new CustomAdapter(getActivity(), kisiler);
listview.setAdapter(customAdapter);
}
还要确保XML中的listview id正确,有时这是一个问题。