findViewById returns 片段 class 中为空
findViewById returns null in fragment class
片断class中的findViewById()
returnsnull
。 ImageButton
的 Toast
显示了 Switch
的对象,即 mySwitch
returns null
...请帮忙....
public MyAdapter(Context context, List<ListItems> values) {
super(context, R.layout.list_item, values);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
LayoutInflater inflater = LayoutInflater.from(getContext());
if( v == null){
v = inflater.inflate(R.layout.list_item, parent, false);
}
ListItems currLoc = LocationList.get(position);
TextView theTextView = (TextView) v.findViewById(R.id.list_item_textview);
theTextView.setText(currLoc.getLocation());
TextView theTextView2 = (TextView) v.findViewById(R.id.list_item_textviewP);
theTextView2.setText(currLoc.getProfile());
ImageView theImageView = (ImageView) v.findViewById(R.id.imageview1);
Toast.makeText(getActivity(), "image view="+theImageView, Toast.LENGTH_SHORT).show();
theImageView.setImageResource(R.drawable.bullet);
Switch mySwitch=(Switch) v.findViewById(R.id.onoff);
Toast.makeText(getActivity(), ""+mySwitch, Toast.LENGTH_SHORT).show();
return v;
}
我明白了....
我在 onGetView 中定义了 findViewById
我必须在 OnCreateView 上声明它
对不起.....
片断class中的
findViewById()
returnsnull
。ImageButton
的Toast
显示了Switch
的对象,即mySwitch
returnsnull
...请帮忙....public MyAdapter(Context context, List<ListItems> values) { super(context, R.layout.list_item, values); } @Override public View getView(int position, View convertView, ViewGroup parent) { View v = convertView; LayoutInflater inflater = LayoutInflater.from(getContext()); if( v == null){ v = inflater.inflate(R.layout.list_item, parent, false); } ListItems currLoc = LocationList.get(position); TextView theTextView = (TextView) v.findViewById(R.id.list_item_textview); theTextView.setText(currLoc.getLocation()); TextView theTextView2 = (TextView) v.findViewById(R.id.list_item_textviewP); theTextView2.setText(currLoc.getProfile()); ImageView theImageView = (ImageView) v.findViewById(R.id.imageview1); Toast.makeText(getActivity(), "image view="+theImageView, Toast.LENGTH_SHORT).show(); theImageView.setImageResource(R.drawable.bullet); Switch mySwitch=(Switch) v.findViewById(R.id.onoff); Toast.makeText(getActivity(), ""+mySwitch, Toast.LENGTH_SHORT).show(); return v; }
我明白了.... 我在 onGetView 中定义了 findViewById 我必须在 OnCreateView 上声明它 对不起.....