Saurabh Agrawal Saurabh Agrawal - 1 month ago 16
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";
@Override
public boolean deliverSelfNotifications() {
// TODO Auto-generated method stub
Log.e(TAG, "Self notification called");
return true;

}

@Override
public void onChange(boolean selfChange) {
// TODO Auto-generated method stub
//super.onChange(selfChange);
Log.e(TAG, "Onchange Called");
people = ctx.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while(people.moveToNext())
{
// 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) {
super(handler);
Log.e(TAG, "Handler Called");
// TODO Auto-generated constructor stub
}
public void register(Context ctx)
{
Log.e(TAG, "Registering");
this.ctx=ctx;
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. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
B obj=new B(new Handler());
obj.register(getApplicationContext());
}
}


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(B.java:29)
02-29 21:01:35.281: ERROR/AndroidRuntime(330): at android.database.ContentObserver$NotificationRunnable.run(ContentObserver.java:43)
02-29 21:01:35.281: ERROR/AndroidRuntime(330): at android.os.Handler.handleCallback(Handler.java:587)
02-29 21:01:35.281: ERROR/AndroidRuntime(330): at android.os.Handler.dispatchMessage(Handler.java:92)
02-29 21:01:35.281: ERROR/AndroidRuntime(330): at android.os.Looper.loop(Looper.java:123)
02-29 21:01:35.281: ERROR/AndroidRuntime(330): at android.app.ActivityThread.main(ActivityThread.java:4363)
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(Method.java:521)
02-29 21:01:35.281: ERROR/AndroidRuntime(330): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
02-29 21:01:35.281: ERROR/AndroidRuntime(330): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
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)

Answer

try changing below line

 Context ctx;

with

 static Context ctx;

it will work.

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