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. Need help in calling a SAP library.

Hello all,

I'm facing a problem in connection of android and SAP.
SAP has provided a java connector to connect to SAP called JCO.
For this there are 2 steps that needs to be followed:
1.) add reference of sapjco.jar in your project.
2.) Load library sapjco3.so in unix.

I've already done the first step but for the second part i'm note sure
how to do it..
Can someone help to load an external library in android.

Thanks
Sandeep.

-- 

2. Twitter4j internet access

Hi guys, Started coding up a simple android app. Part of it pulls the
users last 20 tweets and saves them to a database. When I run this,
statuses = twitter.getUserTimeline(); throws an exception its handled
and prints out: failed to get timeline-permission denied. When I run
the same code non-android, it works fine. I'm a little bit baffled.
Could android be limiting the network connection? Im using some
database helper classes, haven't included the code because the
exception is thrown before any of these are even involved :(. Someone
suggested fixing my manifiest to allow net access so I added: <uses-
permission android:name="android.permission.INTERNET" /> Still giving
the same error though. Any help would be much appreciated. Heres the
code:



<pre><code>import twitter4j.Paging;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;


import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class  TwitterTest extends ListActivity{
 private ArrayList&lt;String&gt; queryString;
 private DatabaseQuery query;






 /* returns date in string format-without time */
 String getThisYearDate(Date d){
  String x = d.toString();
  String y= x.substring(0, 11);
  String z =x.substring(17);
  String a = y.concat(z);
  return a;
 }

 @Override
 public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);
  query = new DatabaseQuery(this);

  /*If file exists then tables in db created successfully*/

  File file = new File("exists.txt");




  boolean success = file.exists();

  if (success==false) {
   try {

    ArrayList&lt;Status&gt; statuses = new ArrayList&lt;Status&gt;();


    Twitter twitter = new TwitterFactory().getInstance("USERNAME",
"PASSWORD");



    statuses = twitter.getUserTimeline();

    /* adds status data to database*/
    for(Status s: statuses){

     query.appendData("Text", s.getText());
     query.appendData("Date", this.getThisYearDate(s.getCreatedAt()));
     query.appendData("ID",String.valueOf(s.getId()) );
     query.addRow();
    }



   }
   catch (TwitterException te) {
    System.out.println("Failed to get timeline: " + te.getMessage());

    System.exit( -1);
   }
   try{
    boolean created = file.createNewFile();
   }
   catch (IOException e) {

    System.out.println("error");




   }

   queryString = query.getData(new String[]
{"Text"},this.getThisYearDate(new Date()), null, null, null, "Date", "
ASC");
   try {
    query.destroy();
   } catch (Throwable e) {
    e.printStackTrace();
   }

  } else {
   // File already exists
   queryString = query.getData(new String[]
{"Text"},this.getThisYearDate(new Date()), null, null, null, "Date", "
ASC");
   try {
    query.destroy();
   } catch (Throwable e) {
    e.printStackTrace();
   }

  }





 // Set the ListView
 setListAdapter(new ArrayAdapter&lt;String&gt;(this,
   android.R.layout.simple_list_item_1, queryString));
 getListView().setTextFilterEnabled(true);
}
}
</code></pre>

-- 

3. build android.jar with all hidden and deprecated APIs

4. Can't Export Android Projects Anymore

5. Terminal Server access from an Android Tablet PC

6. Flash Player + PhoneGap

7. How much does an Nexus One cost?