CursorLoader causes Contacts.DISPLAY_NAME to be set to NULL

349 views Asked by At

I'm having a problem when creating a CursorLoader in a FragmentActivity implementing LoaderManager.LoaderCallbacks.

When creating the CursorLoader my android.provider.ContactsContract.Contacts.DISPLAY_NAME is set to NULL.

i can still get the data from my cursor if i use the "display_name" string but not if i use Contacts.DISPLAY_NAME then it just throws a NullPointerException.

the log messages in the code still prints the correct "display_name" string but when debugging the DISPLAY_NAME is null and i still get the NullPointer exceptions, Could it have something to do with the classloader for the thread used in background tasks?

Here is my FragmentActivity.

package com.example.test2;

import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.provider.ContactsContract.Contacts;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;

public class MainActivity extends FragmentActivity implements LoaderManager.LoaderCallbacks<Cursor> {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String test = Contacts.DISPLAY_NAME;
        Log.d(ACTIVITY_SERVICE, test);

        getSupportLoaderManager().initLoader(0, null, this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
        final String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND ("
                + Contacts.DISPLAY_NAME + " != '' ))";

        final String[] projection = new String[] {
            Contacts._ID,
            Contacts.DISPLAY_NAME,
            Contacts.CONTACT_STATUS,
            Contacts.CONTACT_PRESENCE,
            Contacts.PHOTO_ID,
            Contacts.LOOKUP_KEY,
        };

        CursorLoader cursorLoader = new CursorLoader(
            this, 
            Contacts.CONTENT_URI,
            projection, 
            select, 
            null, 
            Contacts.DISPLAY_NAME);

        return cursorLoader;
    }

    @Override
    public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
        Log.d(ACTIVITY_SERVICE, Contacts.DISPLAY_NAME);

        arg1.moveToFirst();
        arg1.getString(arg1.getColumnIndex("display_name"));
        arg1.getString(arg1.getColumnIndex(Contacts.DISPLAY_NAME));
    }

    @Override
    public void onLoaderReset(Loader<Cursor> arg0) {
        // TODO Auto-generated method stub

    }

}
1

There are 1 answers

0
Javier On

maybe its late but should be

IS NOT NULL 

instead

NOTNULL