error accessing contacts

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

error accessing contacts

Postby vallis » Sat Oct 11, 2008 1:01 pm

Hi all,
This is my first post here. I've been having some trouble accessing the Android Contacts list, if anyone has any suggestions I'd be very grateful.

The method is as follows:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public String checkContacts(String telNo)
  3.  
  4.     {  
  5.  
  6.         String[] projection = new String[] {People.NAME, People.NUMBER};
  7.  
  8.         Uri mContacts = People.CONTENT_URI;
  9.  
  10.        
  11.  
  12.         Cursor managedCursor = managedQuery(mContacts, projection, People.NUMBER + "=" + telNo, null, People.NAME + " ASC");
  13.  
  14.        
  15.  
  16.         if(managedCursor.getCount() != 0)
  17.  
  18.         {
  19.  
  20.                 managedCursor.moveToPosition(0);
  21.  
  22.                 return managedCursor.getString(0);
  23.  
  24.         }
  25.  
  26.         else
  27.  
  28.         {
  29.  
  30.                 return telNo;
  31.  
  32.         }
  33.  
  34.     }
  35.  
  36.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


which is being called from another class using
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. Functions func = new Functions();
  3.  
  4. func.checkContacts("01315551234");  //false phone number that I have added to android contacts
  5.  
  6.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


and the error from LogCat looks like this:

10-11 12:58:51.555: DEBUG/AndroidRuntime(402): Shutting down VM
10-11 12:58:51.555: WARN/dalvikvm(402): threadid=3: thread exiting with uncaught exception (group=0x40010e28)
10-11 12:58:51.565: ERROR/AndroidRuntime(402): Uncaught handler: thread main exiting due to uncaught exception
10-11 12:58:51.584: ERROR/AndroidRuntime(402): java.lang.RuntimeException: Unable to start activity ComponentInfo{napier.vallis/napier.vallis.SMSClient}: java.lang.NullPointerException
10-11 12:58:51.584: ERROR/AndroidRuntime(402): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
10-11 12:58:51.584: ERROR/AndroidRuntime(402): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2156)
10-11 12:58:51.584: ERROR/AndroidRuntime(402): at android.app.ActivityThread.access$1800(ActivityThread.java:112)
10-11 12:58:51.584: ERROR/AndroidRuntime(402): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1580)
10-11 12:58:51.584: ERROR/AndroidRuntime(402): at android.os.Handler.dispatchMessage(Handler.java:88)
10-11 12:58:51.584: ERROR/AndroidRuntime(402): at android.os.Looper.loop(Looper.java:123)
10-11 12:58:51.584: ERROR/AndroidRuntime(402): at android.app.ActivityThread.main(ActivityThread.java:3742)
10-11 12:58:51.584: ERROR/AndroidRuntime(402): at java.lang.reflect.Method.invokeNative(Native Method)
10-11 12:58:51.584: ERROR/AndroidRuntime(402): at java.lang.reflect.Method.invoke(Method.java:515)
10-11 12:58:51.584: ERROR/AndroidRuntime(402): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
10-11 12:58:51.584: ERROR/AndroidRuntime(402): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:497)
10-11 12:58:51.584: ERROR/AndroidRuntime(402): at dalvik.system.NativeStart.main(Native Method)
10-11 12:58:51.584: ERROR/AndroidRuntime(402): Caused by: java.lang.NullPointerException
10-11 12:58:51.584: ERROR/AndroidRuntime(402): at android.content.ContextWrapper.getContentResolver(ContextWrapper.java:89)
10-11 12:58:51.584: ERROR/AndroidRuntime(402): at android.app.Activity.managedQuery(Activity.java:1442)
10-11 12:58:51.584: ERROR/AndroidRuntime(402): at napier.vallis.Functions.checkContacts(Functions.java:68)
10-11 12:58:51.584: ERROR/AndroidRuntime(402): at napier.vallis.SMSClient.onCreate(SMSClient.java:36)
10-11 12:58:51.584: ERROR/AndroidRuntime(402): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1122)
10-11 12:58:51.584: ERROR/AndroidRuntime(402): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2103)
10-11 12:58:51.584: ERROR/AndroidRuntime(402): ... 11 more
10-11 12:58:51.604: INFO/Process(50): Sending signal. PID: 402 SIG: 3


Sorry for the massive post,
Thanks in advance
Cheers,
V.
vallis
Junior Developer
Junior Developer
 
Posts: 23
Joined: Sat Oct 11, 2008 12:52 pm
Location: Scotland

Top

Postby vallis » Mon Oct 13, 2008 11:32 am

Anyone?

I'm really struggling to get rid of this error.
Is there anything else I could post that would make it easier to debug?
Cheers,
V.
vallis
Junior Developer
Junior Developer
 
Posts: 23
Joined: Sat Oct 11, 2008 12:52 pm
Location: Scotland

Postby rmanromga » Tue Oct 14, 2008 12:57 am

Hello Vallis,

Please look at the following post on how to work with content providers... this may help.. (at least it helped me a great deal when learning about content providers)

http://code.google.com/android/devel/da ... iders.html

--D
rmanromga
Developer
Developer
 
Posts: 43
Joined: Thu Dec 20, 2007 6:21 am
Location: Baltimore, MD USA

Postby vallis » Tue Oct 14, 2008 3:04 pm

Hi rmanromga,
Thanks for the reply.
Unfortunately I've already had a look at that article, infact it was where I got the code I'm using from. It did work for a while and I'm really not sure what I've changed to stop it working. I guess I'll just have to keep working at it.

V.
Cheers,
V.
vallis
Junior Developer
Junior Developer
 
Posts: 23
Joined: Sat Oct 11, 2008 12:52 pm
Location: Scotland

Postby rmanromga » Sun Oct 26, 2008 6:17 am

Vallis did you ever fix this? Have you tried stepping through your code with the debugger... from the LogCat you posted, it seems like your error may be related to something else... I see an Exception that the Activity could not be started, that's why am asking if you have tried to step through that segment of your code to find our what/where the error is occurring...

--D
rmanromga
Developer
Developer
 
Posts: 43
Joined: Thu Dec 20, 2007 6:21 am
Location: Baltimore, MD USA

Postby vallis » Sun Oct 26, 2008 1:46 pm

Hey rmanromga,
I did manage to get this sorted in the end. It was nothing to do with accessing content providers at all.
I had the method that was accessing the contacts list in a class that had reusable functions in it, that class extended activity (first mistake). When I was calling the method it was trying to start a new activity and managedQuery under it, when what I should have done was pass an activity into that class and use that.

So I'd call the method with Functions.checkContacts(activity, phone number) rather than the way I was doing it before.

Thanks for checking back in on it and for your suggestion before.
Cheers,
V.
vallis
Junior Developer
Junior Developer
 
Posts: 23
Joined: Sat Oct 11, 2008 12:52 pm
Location: Scotland

Top

Postby frigo » Thu Oct 30, 2008 1:47 pm

have you added the read (or write) <uses-permission ...> to your manifest to access the content provider?
frigo
Freshman
Freshman
 
Posts: 5
Joined: Mon Jun 02, 2008 12:33 pm

Postby vallis » Thu Oct 30, 2008 1:59 pm

Thanks for the suggestion Frigo.
I managed to get the problem fixed.
It was pretty much because the method should have been declared as static and accessed in a static way but I hadn't declared it properly and was calling it in a non static way.
Cheers,
V.
vallis
Junior Developer
Junior Developer
 
Posts: 23
Joined: Sat Oct 11, 2008 12:52 pm
Location: Scotland

Postby Steph » Wed Nov 05, 2008 11:45 am

I have the same exception...
can you tell me more about fix it please?
Steph
Experienced Developer
Experienced Developer
 
Posts: 90
Joined: Mon Oct 20, 2008 1:48 pm
Location: France

Postby vallis » Wed Nov 05, 2008 1:38 pm

Hi Steph,
The problem (in my case a least) came from trying to instantiate a new instance of th functions class rather than calling it directly.

For example, if you have a method called getContacts within a class called Functions, rather than using
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. Functions func = new Functions();
  3.  
  4. func.getContacts();
  5.  
  6.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


you should declare the getContacts method as static and call it using
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. Functions.getContacts();
  3.  
  4.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


I also made the mistake of making the Functions class extend Activity so that I would have access to methods that only Activites can use (like managedCursor). Rather than extending Activity you should make your getContacts method take an Activity as one of it's parameters then call the methods you need through that. So if you're using managedCursor your method would look something like

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public static void getContacts(Activity act)
  3.  
  4. {
  5.  
  6.     act.managedCursor(...);
  7.  
  8. }
  9.  
  10.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


you would then call this method from your activity using
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. Functions.getContacts((Activity) this)
  3.  
  4.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4

which casts your class (that extends activity) as an activity and passes it to the getContacs method.

I hope that helps, it took me a while to get how it all worked.
If you've got any more questions or if you want to post some of your code just stick it in here and I can take a look.
Cheers,
V.
vallis
Junior Developer
Junior Developer
 
Posts: 23
Joined: Sat Oct 11, 2008 12:52 pm
Location: Scotland

Postby Steph » Wed Nov 05, 2008 1:51 pm

thanks for your reply and disponibility.

That's my source :

HttpParams parameters = new BasicHttpParams();
parameters.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, CONNECT_TIMEOUT);
HttpProtocolParams.setVersion(parameters, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(parameters, HTTP.DEFAULT_CONTENT_CHARSET);
HttpProtocolParams.setUseExpectContinue(parameters, true);
HttpProtocolParams.setUserAgent(parameters, "minisoap/1.0");

//add by SC


SSLUtilities.trustAllHostnames();
SSLUtilities.trustAllHttpsCertificates();
//default programm
//Create scheme registry
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));


//Create a http client using a threadsave connection manager
ThreadSafeClientConnManager connectionManager = new ThreadSafeClientConnManager(parameters, registry);
httpClient = new DefaultHttpClient(connectionManager, parameters);


and SSLUtilities is there : http://en.wikibooks.org/wiki/Programmin ... ertificate

But i don't see a link with you.
and my exception is not really the same : Uncaught handler : thread pool-1-thread-1 existing due to uncaught exception

I know my english is not very good but, i think i don't catch an exception.
I tryed to catch all the code ( :/ ) and nothing change ...

regards,
Steph
Steph
Experienced Developer
Experienced Developer
 
Posts: 90
Joined: Mon Oct 20, 2008 1:48 pm
Location: France

Postby vallis » Wed Nov 05, 2008 2:13 pm

I've got to admit I'm really not sure what the problem is.
Could you maybe post your code with a bit more detail about classes and methods etc. I'm finding it quite hard to work out what's going on. You could post your code at http://pastebin.com/ maybe

The only thing I can suggest from what I've seen would be to try using
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. Scheme https = new Scheme("https", new MySecureSocketFactory(), 443);
  3.  
  4. registry.register(https);
  5.  
  6.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4

rather than
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. registry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
  3.  
  4.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


I have no idea if that will make a difference but it may be worth a shot.
Cheers,
V.
vallis
Junior Developer
Junior Developer
 
Posts: 23
Joined: Sat Oct 11, 2008 12:52 pm
Location: Scotland

Postby Steph » Wed Nov 05, 2008 2:22 pm

I post on the other website.
i'll see.

i really need it to continu my application.

HTTPS is very difficult in Android

:/

Thanks Vallis
Steph
Experienced Developer
Experienced Developer
 
Posts: 90
Joined: Mon Oct 20, 2008 1:48 pm
Location: France

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 5 guests