Android 为什么可以';我是否可以通过在getView()中使用LayoutFlater使BaseAdapter工作?它不断返回null指针异常
它以前工作过,但不知什么原因,即使一切正常,我现在仍然有nullpointer异常。如果您能提供帮助,请亲自查看:Android 为什么可以';我是否可以通过在getView()中使用LayoutFlater使BaseAdapter工作?它不断返回null指针异常,android,Android,它以前工作过,但不知什么原因,即使一切正常,我现在仍然有nullpointer异常。如果您能提供帮助,请亲自查看: public class TestActivity extends ListActivity { ArrayList<String> array; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setCo
public class TestActivity extends ListActivity {
ArrayList<String> array;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
array = getsms();
SMSadapter adapter = new SMSadapter(this, array);
getListView().setAdapter(adapter);
}
public class SMSadapter extends BaseAdapter {
Context mcontext;
ArrayList<String> marray;
LayoutInflater inflater;
public SMSadapter (Context context, ArrayList<String>array){
mcontext = context;
marray = array;
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return marray.size();
}
@Override
public Object getItem(int position) {
return marray.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public class ViewHolder {
TextView text;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = new ViewHolder();
if (convertView == null){
convertView = inflater.inflate(R.layout.items, null);
holder.text = (TextView)findViewById(R.id.text);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}
holder.text.setText(marray.get(position));
return convertView;
}
}
public ArrayList<String> getsms(){
Cursor cursor = getContentResolver().query(Uri.parse("content://sms/inbox"), new String[]{"address"}, null,null,null);
cursor.moveToFirst();
ArrayList<String> temp = new ArrayList<String>();
while(cursor.moveToNext()){
temp.add(cursor.getString(0));
}
return temp;
}
您有以下错误:
holder.text = (TextView)findViewById(R.id.text);
相反,您必须这样做:
holder.text = (TextView)convertView.findViewById(R.id.text);
希望这有帮助 您有以下错误:
holder.text = (TextView)findViewById(R.id.text);
相反,您必须这样做:
holder.text = (TextView)convertView.findViewById(R.id.text);
希望这有帮助 是的,它起作用了。非常感谢,我完全忘了这样做。是的,它起作用了。非常感谢,我完全忘了这样做。