这是一个应该从手机中获取联系人并在列表视图中列出具有电话号码的应用程序。但是当 madaper.notifyDataSetChanged(); 时应用程序崩溃;被称为:/请帮助。
public class MainActivity extends Activity{
static int num = 0;
ListView lv;
ArrayAdapter<String> mAdaper;
ArrayList<String> contacts = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getContacts();
lv = (ListView)findViewById(R.id.listView1);
mAdaper = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, contacts);
lv.setAdapter(mAdaper);
}
private void getContacts(){
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, // projection,
null, // selection,
null, // selectionArgs,
"_ID DESC" // sortOrder
);
if (cur.getCount() > 0) {
while (cur.moveToNext() && num < 10) {
String id = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer
.parseInt(cur.getString(cur
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
// TableRow row = new TableRow(this);
TextView tv = new TextView(this);
String row = id + " - " + name;
contacts.add(row);
//mAdaper.add(row);
num++;
}
}
}
mAdaper.notifyDataSetChanged();
}