简体   繁体   中英

Android: Null object reference

I am trying to have a list of contacts, each with a QuickContactBadge . When I tried to add the QuickContactBadge , my application crasher and the logcat says null object reference on mCursor when I call getColumnIndex(); . What am I doing wrong, and will this code work to correctly implement the QuickContactBadge ?

private CursorAdapter mAdapter;
private Cursor mCursor;
private String currentQuery;
private int mIdColumn;
private int mLookupKeyColumn;
private Uri mContactUri;
private QuickContactBadge mBadge;
private static final String[] PROJECTION = {
        Contacts._ID,
        Contacts.DISPLAY_NAME_PRIMARY,
        Contacts.LOOKUP_KEY,
        Contacts.PHOTO_THUMBNAIL_URI,
        Contacts.HAS_PHONE_NUMBER
};
private static final String[] FROM = { Contacts.DISPLAY_NAME_PRIMARY };
private static final int[] TO = { R.id.contact_text };
private static final String SELECTION = "(" + Contacts.IN_VISIBLE_GROUP +
        " = 1) AND (" + Contacts.HAS_PHONE_NUMBER + " != 0 )";


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mCursor = null;
    mAdapter = new SimpleCursorAdapter(getActivity(), R.layout.contacts_list_item, mCursor, FROM, TO, 0);

    mBadge = (QuickContactBadge) getActivity().findViewById(R.id.badge);
    mIdColumn = mCursor.getColumnIndex(Contacts._ID);
    mLookupKeyColumn = mCursor.getColumnIndex(Contacts.LOOKUP_KEY);
    mContactUri =
            Contacts.getLookupUri(
                    mCursor.getLong(mIdColumn),
                    mCursor.getString(mLookupKeyColumn)
            );
    mBadge.assignContactUri(mContactUri);
}
mCursor = null;
mAdapter = new SimpleCursorAdapter(getActivity(), R.layout.contacts_list_item, mCursor, FROM, TO, 0);

mBadge = (QuickContactBadge) getActivity().findViewById(R.id.badge);
mIdColumn = mCursor.getColumnIndex(Contacts._ID);

Check first and last line here... your mCursor is null, and you are calling getColumnIndex() on it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM