Saurabh Agrawal Saurabh Agrawal - 7 months ago 36
Android Question

Errors with ContentObserver for Contacts

I am a beginner and I'm developing an app where I want to get notified when a new contact is added.
I tried following code but I'm getting errors as in Logcat trace below.

One more query I have is once I have registered my ContentObserver from my app will it get notified even if user quits my app or should I write it in service and run it in background.
Plz help

public class B extends ContentObserver {
Context ctx;
String ContactId,DisplayName;
final String[] projection = new String[] { ContactsContract.Contacts._ID};
Cursor curval,people;
private static final String TAG = "NewContactTrackService";
public boolean deliverSelfNotifications() {
// TODO Auto-generated method stub
Log.e(TAG, "Self notification called");
return true;


public void onChange(boolean selfChange) {
// TODO Auto-generated method stub
Log.e(TAG, "Onchange Called");
people = ctx.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
// values.clear();

ContactId = people.getString(people.getColumnIndex(ContactsContract.Contacts._ID));
DisplayName = people.getString(people.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Log.e(TAG, "New Contact with contact id "+ContactId);
Log.e(TAG, "New Contact Displayname "+DisplayName);

public B(Handler handler) {
Log.e(TAG, "Handler Called");
// TODO Auto-generated constructor stub
public void register(Context ctx)
Log.e(TAG, "Registering");
curval = ctx.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, projection, null, null, null);
curval.registerContentObserver(new B(new Handler()));
Log.e(TAG, "Registered");

This is sample activity where I have created object of above class

public class TrackContactActivity extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
B obj=new B(new Handler());

Logcat Trace:

02-29 20:59:59.571: ERROR/AndroidRuntime(324): ERROR: thread attach failed
02-29 20:59:59.951: ERROR/NewContactTrackService(330): Handler Called
02-29 20:59:59.951: ERROR/NewContactTrackService(330): Registering
02-29 21:00:00.050: ERROR/NewContactTrackService(330): Handler Called
02-29 21:00:00.050: ERROR/NewContactTrackService(330): Registered
02-29 21:01:35.270: ERROR/NewContactTrackService(330): Onchange Called
02-29 21:01:35.281: ERROR/AndroidRuntime(330): Uncaught handler: thread main exiting due to uncaught exception
02-29 21:01:35.281: ERROR/AndroidRuntime(330): java.lang.NullPointerException
02-29 21:01:35.281: ERROR/AndroidRuntime(330): at com.sample.B.onChange(
02-29 21:01:35.281: ERROR/AndroidRuntime(330): at android.database.ContentObserver$
02-29 21:01:35.281: ERROR/AndroidRuntime(330): at android.os.Handler.handleCallback(
02-29 21:01:35.281: ERROR/AndroidRuntime(330): at android.os.Handler.dispatchMessage(
02-29 21:01:35.281: ERROR/AndroidRuntime(330): at android.os.Looper.loop(
02-29 21:01:35.281: ERROR/AndroidRuntime(330): at
02-29 21:01:35.281: ERROR/AndroidRuntime(330): at java.lang.reflect.Method.invokeNative(Native Method)
02-29 21:01:35.281: ERROR/AndroidRuntime(330): at java.lang.reflect.Method.invoke(
02-29 21:01:35.281: ERROR/AndroidRuntime(330): at$
02-29 21:01:35.281: ERROR/AndroidRuntime(330): at
02-29 21:01:35.281: ERROR/AndroidRuntime(330): at dalvik.system.NativeStart.main(Native Method)
02-29 21:01:35.361: ERROR/dalvikvm(330): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
02-29 21:01:38.641: ERROR/gralloc(65): [unregister] handle 0x3236c0 still locked (state=40000001)


try changing below line

 Context ctx;


 static Context ctx;

it will work.

Better solution will be to try passing application context in register method.