1

So what I am trying to do is display my database in another activity and add an onItemClickListener. Then I am sending my info by using a URI to another class. I am retrieving the uri in the recipient class and using it to create a Cursor. When I try to initialize my Cursor using getContentResolver().query(), I am getting a nullpointerexception within my log. The line of code looks like this:

Cursor cursor = getContentResolver().query(uri, null, null, null, null);

The uri variable I am using comes from this line of code:

Uri uri = getIntent().getData();

To make sure that some sort of uri is being returned, I output the uri in my log and got this: content://com.example.myfirstapp.ContactProvider/CONTACT/(The id of the item selected)

Here is my log:

01-06 21:31:43.913: E/AndroidRuntime(619): Caused by: java.lang.NullPointerException
01-06 21:31:43.913: E/AndroidRuntime(619):  at com.example.myfirstapp.ContactActivity.onCreate(ContactActivity.java:22)

The code at line 22:

Cursor cursor = getContentResolver().query(uri, null, null, null, null);
Lazy Ninja
  • 22,342
  • 9
  • 83
  • 103
Sai Valluri
  • 85
  • 1
  • 3
  • 6
  • please check the uri you are getting from getIntent().getData(); some thing is wrong in that. – Raj Jan 07 '13 at 03:39
  • http://stackoverflow.com/questions/3750903/how-can-getcontentresolver-be-called-in-android Could that be the reason? That the method needs to be called from a context.. or perhaps not if it's called inside an activity? – leenephi Jan 07 '13 at 03:43
  • well I know that the uri is successfully being transferred from one activity to another, but I am not exactly sure how to format a uri. I've looked at example at many sites and they all seem different. – Sai Valluri Jan 07 '13 at 03:43
  • @leenephi where and how would I make the instance of the activity or context? I am calling the method within my activity....where else would i call it? – Sai Valluri Jan 07 '13 at 03:47
  • Did you remember to add the proper permissions and register your `ContentProvider` in your Android Manifest? – Alex Lockwood Jan 07 '13 at 03:53
  • This is what I put in my manifest. – Sai Valluri Jan 07 '13 at 03:58
  • Nevermind; should be good in your activity..! – leenephi Jan 07 '13 at 04:00

3 Answers3

0

Your line should looks like this

Uri uri = getIntent().getData();
Cursor cursor = getContentResolver().query(uri, null, null, null, null);

instead of this

Cursor cursor = getContentResolver().query(uri, null, null, null, null);
Uri uri = getIntent().getData();

You have used uri before you have initialized. and also check for manifest permissions

android.permission.READ_CONTACTS 
Nirav Tukadiya
  • 3,367
  • 1
  • 18
  • 36
  • Actually, I should have put the code in order when posting this, but it is already in order like that. – Sai Valluri Jan 07 '13 at 04:28
  • what data are you passing with intent.?? and which activity you are opening with the intent.please put all your code here. – Nirav Tukadiya Jan 07 '13 at 04:33
  • list.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView> parent, View view, int position, long id) { Intent contactIntent = new Intent(view.getContext(), ContactActivity.class); Uri uri = Uri.withAppendedPath(ContactProvider.CONTENT_URI, String.valueOf(id)); contactIntent.setData(uri); startActivity(contactIntent); } }); – Sai Valluri Jan 07 '13 at 04:35
  • The code I posted originally is how I am retrieving the uri in my other activity. – Sai Valluri Jan 07 '13 at 04:37
  • so you want to retrieve phone number after it you clicked on contact?? – Nirav Tukadiya Jan 07 '13 at 04:40
  • After I click on the contact (I am using the name), I want to retrieve all the information about that contact. In my database, I have a name and email so far. – Sai Valluri Jan 07 '13 at 04:42
  • That is why in the getContentResolver().qeury() I passed the second argument as null, so that I can retrieve all the columns in the database – Sai Valluri Jan 07 '13 at 04:43
  • android.permission.READ_CONTACTS have you set this permission to your manifest??? – Nirav Tukadiya Jan 07 '13 at 04:56
  • yes I have, but maybe I formatted it wrong. But if I did set my permission right, then my app would let me extend a class using ContentProvider, which it is letting me do that. – Sai Valluri Jan 07 '13 at 04:58
  • can u possibly give me a link to a website that shows how to properly set permissions in my manifest, I felt like I did it wrong because i never put android.permission.READ_CONTACTS – Sai Valluri Jan 07 '13 at 05:00
  • http://developer.android.com/reference/android/Manifest.permission.html#READ_CONTACTS – Nirav Tukadiya Jan 07 '13 at 05:01
0

you can try this

Cursor cursor = getApplicationContext().getContentResolver().query(uri, null, null, null, null);
Jignesh Ansodariya
  • 12,583
  • 24
  • 81
  • 113
0

This code

Cursor cursor = getApplicationContext().getContentResolver().query(uri, null, null, null, null);

does the job. Do not call your function before oncreate.

Regent
  • 5,142
  • 3
  • 21
  • 35
Murat Aka
  • 41
  • 3