В моем коде Android
Я хочу получить контакт имя, адрес электронной почты и номер телефона как json, а затем хочу отобразить.
Вот мой код:
public class MainActivity extends Activity {
public TextView outputText;
String[] phoneNumber;
String[] email;
String name;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
outputText = (TextView) findViewById(R.id.textView1);
try {
//fetchContacts();
outputText.setText(fetchContacts());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String fetchContacts() throws JSONException {
Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;
String _ID = ContactsContract.Contacts._ID;
String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME;
String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER;
Uri PhoneCONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String Phone_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;
Uri EmailCONTENT_URI = ContactsContract.CommonDataKinds.Email.CONTENT_URI;
String EmailCONTACT_ID = ContactsContract.CommonDataKinds.Email.CONTACT_ID;
String DATA = ContactsContract.CommonDataKinds.Email.DATA;
ContentResolver contentResolver = getContentResolver();
Cursor cursor = contentResolver.query(CONTENT_URI, null, null, null,
null);
List<Contact> contacts = new ArrayList<Contact>();
Gson gson = new Gson();
// Loop for every contact in the phone
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
String contact_id = cursor
.getString(cursor.getColumnIndex(_ID));
name = cursor.getString(cursor.getColumnIndex(DISPLAY_NAME));
int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor
.getColumnIndex(HAS_PHONE_NUMBER)));
if (hasPhoneNumber > 0) {
int p = 0;
// Query and loop for every phone number of the contact
Cursor phoneCursor = contentResolver.query(
PhoneCONTENT_URI, null, Phone_CONTACT_ID + " = ?",
new String[] { contact_id }, null);
while (phoneCursor.moveToNext()) {
phoneNumber[p] = phoneCursor.getString(phoneCursor
.getColumnIndex(NUMBER));
p++;
}
phoneCursor.close();
int q = 0;
// Query and loop for every email of the contact
Cursor emailCursor = contentResolver.query(
EmailCONTENT_URI, null, EmailCONTACT_ID + " = ?",
new String[] { contact_id }, null);
while (emailCursor.moveToNext()) {
email[q] = emailCursor.getString(emailCursor
.getColumnIndex(DATA));
q++;
}
emailCursor.close();
contacts.add(new Contact(name, phoneNumber, email));
}
}
}
return gson.toJson(contacts);
}
}
Но я получаю ошибку исключения nullpointer:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.contactdemo/com.example.contactdemo.MainActivity}: java.lang.NullPointerException
ошибка, найденная в следующей строке кода:
phoneNumber[p] = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER));
Здесь я храню phonenumber и адрес электронной почты как array.Is там проблема инициализации массива?? любая идея ребята?