managedQuery() / getContentResolver() NullPointerException

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

managedQuery() / getContentResolver() NullPointerException

Postby sloth » Fri Jan 01, 2010 11:37 pm

Hey there!

I'm currently working on a component that's retrieving contact information and creating an RDF model (FOAF tbe) from the data provided. I started off with the HelloAndroid example and tried some code in there. Everything worked out fine, so I wanted to put everything in a seperate class which can be accessed via an interface.

However, now I'm getting a NullPointer Exception when I want to call a managedQuery().
So I tried to create a ContentResolver in order to perform a query(). But as soon as I call the getContentResolver() I'm getting a NullPointer Exception as well.

I think this could be a problem concerning the AndroidManifest.xml? I've been struggling with that for quite some hours now and think that I won't solve this on my own, so I'd like to get your opinions and ideas!

However, here's my code:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.example.helloandroid;
  3.  
  4.  
  5.  
  6. import it.polimi.elet.contextaddict.microjena.rdf.model.Model;
  7.  
  8. import it.polimi.elet.contextaddict.microjena.rdf.model.ModelFactory;
  9.  
  10. import it.polimi.elet.contextaddict.microjena.rdf.model.Resource;
  11.  
  12. import it.polimi.elet.contextaddict.microjena.vocabulary.RDF;
  13.  
  14. import android.app.Activity;
  15.  
  16. import android.database.Cursor;
  17.  
  18. import android.net.Uri;
  19.  
  20. import android.os.Bundle;
  21.  
  22. import android.provider.ContactsContract;
  23.  
  24.  
  25.  
  26. public class CPAddressbook extends Activity implements IBasicContextProvider {
  27.  
  28.  
  29.  
  30.         Model contextModel;
  31.  
  32.        
  33.  
  34.         public void onCreate(Bundle savedInstanceState) {
  35.  
  36.                 super.onCreate(savedInstanceState);
  37.  
  38.                 contextModel = ModelFactory.createDefaultModel();
  39.  
  40.         }
  41.  
  42.  
  43.  
  44.         @Override
  45.  
  46.         public void detectContext(Model model) {
  47.  
  48.                
  49.  
  50.                 Uri contactsURI = ContactsContract.Data.CONTENT_URI;
  51.  
  52.                
  53.  
  54.                 String[] projection = new String[] {
  55.  
  56.                                 ContactsContract.Data._ID,
  57.  
  58.                                 ContactsContract.Data.CONTACT_ID,
  59.  
  60.                                 ContactsContract.Data.MIMETYPE,
  61.  
  62.                                 ContactsContract.Data.DATA1,
  63.  
  64.                                 ContactsContract.Data.DATA2,
  65.  
  66.                                 ContactsContract.Data.DATA3,
  67.  
  68.                                 ContactsContract.Data.DATA4,
  69.  
  70.                                 ContactsContract.Data.DATA5,
  71.  
  72.                                 ContactsContract.Data.DATA_VERSION
  73.  
  74.                 };
  75.  
  76.                 String where =  ContactsContract.Data.MIMETYPE + " = ? OR " +
  77.  
  78.                                                 ContactsContract.Data.MIMETYPE + " = ? OR " +
  79.  
  80.                                                 ContactsContract.Data.MIMETYPE + " = ? OR " +
  81.  
  82.                                                 ContactsContract.Data.MIMETYPE + " = ?";
  83.  
  84.                
  85.  
  86.                 String[] whereArgs = new String[] {
  87.  
  88.                                 ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE,
  89.  
  90.                                 ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,
  91.  
  92.                                 ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE,
  93.  
  94.                                 ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE
  95.  
  96.                 };
  97.  
  98.                
  99.  
  100.                 Cursor contactsTable = managedQuery(contactsURI, projection, where, whereArgs, null);
  101.  
  102.                
  103.  
  104.                 writeToContextModel(contactsTable);
  105.  
  106.         }
  107.  
  108. ...
  109.  
  110.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


This is the class that calls the detectContext():
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.example.helloandroid;
  3.  
  4.  
  5.  
  6. import it.polimi.elet.contextaddict.microjena.rdf.model.Model;
  7.  
  8. import it.polimi.elet.contextaddict.microjena.rdf.model.ModelFactory;
  9.  
  10. import android.app.Activity;
  11.  
  12. import android.content.Intent;
  13.  
  14. import android.os.Bundle;
  15.  
  16. import android.provider.ContactsContract;
  17.  
  18. import android.widget.TextView;
  19.  
  20.  
  21.  
  22. public class HelloAndroid extends Activity {
  23.  
  24.    /** Called when the activity is first created. */
  25.  
  26.    @Override
  27.  
  28.    public void onCreate(Bundle savedInstanceState) {
  29.  
  30.            
  31.  
  32.            super.onCreate(savedInstanceState);
  33.  
  34.        TextView tv = new TextView(this);
  35.  
  36.        tv.setText("Hello Android!");
  37.  
  38.        
  39.  
  40.        IBasicContextProvider cpAddressbook = new CPAddressbook();
  41.  
  42.        cpAddressbook.detectContext(ModelFactory.createDefaultModel());
  43.  
  44.        Model model = cpAddressbook.getContextModel();
  45.  
  46.        
  47.  
  48.        setContentView(tv);
  49.  
  50.        
  51.  
  52.    }
  53.  
  54. }
  55.  
  56.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4

This is also where I had my query before. It was exactely the same code!

and finally my AndroidManifest.xml:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  5.  
  6.      package="com.example.helloandroid"
  7.  
  8.      android:versionCode="1"
  9.  
  10.      android:versionName="1.0">
  11.  
  12.     <application android:icon="@drawable/icon" android:label="@string/app_name">
  13.  
  14.         <activity android:name=".HelloAndroid"
  15.  
  16.                  android:label="@string/app_name">
  17.  
  18.             <intent-filter>
  19.  
  20.                 <action android:name="android.intent.action.MAIN" />
  21.  
  22.                 <category android:name="android.intent.category.LAUNCHER" />
  23.  
  24.             </intent-filter>
  25.  
  26.         </activity>
  27.  
  28.  
  29.  
  30.    
  31.  
  32.  
  33.  
  34. <activity android:name=".CPAddressbook" android:label="@string/app_name" android:permission="android.permission.READ_CONTACTS"></activity>
  35.  
  36. </application>
  37.  
  38.     <uses-sdk android:minSdkVersion="5" />
  39.  
  40.  
  41.  
  42. <uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
  43.  
  44. <uses-library android:name="it.polimi.elet.contextaddict.microjena"></uses-library>
  45.  
  46. </manifest>
  47.  
  48.  
Parsed in 0.004 seconds, using GeSHi 1.0.8.4

I tried it with and without the additional permission within the .CPAddressbook and it didn't make any difference.

Is it about something in the AndroidManifest.xml or do I handle Activities incorrectly? Or both?

Any help would be appreciated :)

Thanks for your time!
sloth
Freshman
Freshman
 
Posts: 2
Joined: Fri Jan 01, 2010 11:22 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Google Feedfetcher and 2 guests