moved Code: managedQuery() / getContentResolver() NullPointerException

by Flo » Sun, 03 Jan 2010 00:22:16 GMT


Sponsored Links
 ey 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 or
lack of knowledge about how to handle Activities and the likes? 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:
--------------------------------------------------------------------------
package com.example.helloandroid;

import it.polimi.elet.contextaddict.microjena.rdf.model.Model;
import it.polimi.elet.contextaddict.microjena.rdf.model.ModelFactory;
import it.polimi.elet.contextaddict.microjena.rdf.model.Resource;
import it.polimi.elet.contextaddict.microjena.vocabulary.RDF;
import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;

public class CPAddressbook extends Activity implements
IBasicContextProvider {

Model contextModel;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
contextModel = ModelFactory.createDefaultModel();
}

@Override
public void detectContext(Model model) {

Uri contactsURI = ContactsContract.Data.CONTENT_URI;

String[] projection = new String[] {
ContactsContract.Data._ID,
ContactsContract.Data.CONTACT_ID,
ContactsContract.Data.MIMETYPE,
ContactsContract.Data.DATA1,
ContactsContract.Data.DATA2,
ContactsContract.Data.DATA3,
ContactsContract.Data.DATA4,
ContactsContract.Data.DATA5,
ContactsContract.Data.DATA_VERSION
};
String where = ContactsContract.Data.MIMETYPE + " = ?
OR " +
ContactsContract.Data.MIMETYPE + " = ?
OR " +
ContactsContract.Data.MIMETYPE + " = ?
OR " +
ContactsContract.Data.MIMETYPE + " = ?";

String[] whereArgs = new String[] {

ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE,

ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,

ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE,

ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE
};

Cursor contactsTable = managedQuery(contactsURI, projection,
where, whereArgs, null);

writeToContextModel(contactsTable);
}
...
--------------------------------------------------------------------------


This is the class that calls the detectContext():
--------------------------------------------------------------------------
package com.example.helloandroid;

import it.polimi.elet.contextaddict.mi



moved Code: managedQuery() / getContentResolver() NullPointerException

by Dmitri Plotnikov » Sun, 03 Jan 2010 09:12:17 GMT


 ould you post the exception stack trace?

Thank you,
Dmitri

On Saturday, January 2, 2010, Flo <flo.schw...@gmail.com> wrote:
>


Sponsored Links


moved Code: managedQuery() / getContentResolver() NullPointerException

by Dmitri Plotnikov » Sun, 03 Jan 2010 09:18:08 GMT


 ctually, no need for a stack trace.  You are manually allocating an
activity. This should never be done. It would be best to all the work
in the activity allocated by the system. Of course you can delegate
the work to a helper object like CPAddressbook, but you will need to
pass the parent activity to it.

Cheers,
Dmitri

On Saturday, January 2, 2010, Dmitri Plotnikov <dplotni...@google.com> wrote:

--



moved Code: managedQuery() / getContentResolver() NullPointerException

by Flo » Sun, 03 Jan 2010 18:47:07 GMT


 kay, so my suspicion was right!

How can I achieve passing the parent activity to it?
Does this involve Intents? Or should the CPAddressbook simply extend
the parent activity?

Thanks for your help so far,
Flo.

On 3 Jan., 02:17, Dmitri Plotnikov <dplotni...@google.com> wrote:

--



moved Code: managedQuery() / getContentResolver() NullPointerException

by Flo » Sun, 03 Jan 2010 18:47:16 GMT


 kay, so my suspicion was right!

How can I achieve passing the parent activity to it?
Does this involve Intents? Or should the CPAddressbook simply extend
the parent activity?

Thanks for your help so far,
Flo.

On 3 Jan., 02:17, Dmitri Plotnikov <dplotni...@google.com> wrote:

--



moved Code: managedQuery() / getContentResolver() NullPointerException

by Flo » Mon, 04 Jan 2010 16:49:53 GMT


 h, now I see what you meant by "passing the parent activity".. no
idea, why I didn't understand it first, guess I shouldn't program more
than 6h without a break :D

However, if anyone comes across the same problem as I did, just pass
the activity to the class constructor like this:

public class CPAddressbook implements IBasicContextProvider {

Activity callingActivity;

public CPAddressbook(Activity caller) {
callingActivity = caller;
}

//so you can query like that:

Cursor c = callingActivity.managedQuery(contactsURI, projection,
where, whereArgs, orderBy);
}


Thanks a lot for your help Dmitri!! :)

On Jan 3, 11:46am, Flo <flo.schw...@gmail.com> wrote:

--



Other Threads

1. And Not A Moment Too Late, the Samsung Galaxy S Has Been Rooted

sorry kl repost..leshak lg, leshak lg..galaxy s rooted !!

http://phandroid.com/2010/06/22/and-not-a-moment-too-late-the-samsung-galaxy-s-has-been-rooted/

-- 
"Indonesian Android Community [id-android]" 

2. android client, pc server (JAVA) can't open TCP connection

Although I personally haven't done this, I believe the solutions is to
forward the port using the redir command inside the emulator.
Some quick googling provides some examples like:
http://www.blog.droidweb.com/2009/08/developers-tip-7-android-emulator-networking/



> 

3. "20 Percent of Android Apps Can Threaten Privacy"

4. Date picker problem

5. Application crashing

6. apps

7. ada yang punya indonesian language pack untuk Swype ga