0
我試圖用所有者的電子郵件填充ArrayList。我正在做一個片段。這裏是我的代碼:使用CursorLoader獲取主人的電子郵件地址
public class ItemDetailFragment extends Fragment implements
LoaderManager.LoaderCallbacks<Cursor> {
ArrayList<String> emails = new ArrayList<String>();
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getLoaderManager().initLoader(0, null, this);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myFragmentView = inflater.inflate(R.layout.formal_email_layout,
container, false);
return myFragmentView;
}
public Loader<Cursor> onCreateLoader(int id, Bundle arguments) {
return new CursorLoader(getActivity(),
// Retrieve data rows for the device user's 'profile' contact.
Uri.withAppendedPath(
ContactsContract.Profile.CONTENT_URI,
ContactsContract.Contacts.Data.CONTENT_DIRECTORY),
ProfileQuery.PROJECTION,
// Select only email addresses.
ContactsContract.Contacts.Data.MIMETYPE + " = ?",
new String[]{ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE},
// Show primary email addresses first. Note that there won't be
// a primary email address if the user hasn't specified one.
null);
}
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
List<String> emails = new ArrayList<String>();
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
emails.add(cursor.getString(ProfileQuery.ADDRESS));
// Potentially filter on ProfileQuery.IS_PRIMARY
cursor.moveToNext();
}
Log.d("test", cursor.getString(ProfileQuery.ADDRESS));
}
public void onLoaderReset(Loader<Cursor> cursorLoader) {
}
private interface ProfileQuery {
String[] PROJECTION = {
ContactsContract.CommonDataKinds.Email.ADDRESS,
ContactsContract.CommonDataKinds.Email.IS_PRIMARY,
};
int ADDRESS = 0;
int IS_PRIMARY = 1;
}
}
我基本上都採用給here的例子,我剛剛適應它的片段。
當片段共進午餐,應用程序崩潰,我得到這個錯誤:
07-05 12:27:55.235: W/dalvikvm(23217): threadid=1: thread exiting with uncaught exception (group=0x40c2f1f8)
07-05 12:27:55.235: E/AndroidRuntime(23217): FATAL EXCEPTION: main
07-05 12:27:55.235: E/AndroidRuntime(23217): android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
(我有我的設備上的一個Gmail帳戶)