Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7586975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:33:34+00:00 2026-05-30T19:33:34+00:00

I am a beginner and I’m developing an app where I want to get

  • 0

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T19:33:35+00:00Added an answer on May 30, 2026 at 7:33 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

iOS beginner here. I'm using the following code to save my facebook accessToken and
Beginner question: I want to debug some code that compiles. I pressed debug, it
another beginner problem. Why isn't the following code with an asp.net page not working?
Beginner Node question. I'm running a Node/Express app, and I want to send client-side
level: beginner the following code will print 'False' def function(x): if len(x) == 5:
I'm not a beginner at C# but I really need to increase my understanding,
Once again a very beginner-ish question, but here I go: I would like to
Beginner programmer here....hope it makes sense :) I have created a console app that
(Beginner to HTML) I have made a Photoshop mock-up of the website I want
Beginner in python, but been programming for about 5 years now. I suspect I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.