I have an app on the market, but in some cases it force closes at the open. According to the crash error there is something wrong with the indexOf command (NullPointerException). Part of the code:
contactName = null;
Context context = getApplicationContext();
Cursor cursor = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
myArr.add("");
while (cursor.moveToNext())
{
contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
spaceIndex = contactName.indexOf(' '); //this is the bad row
spaceLastIndex = contactName.lastIndexOf(' ');
myArr.add(contactName);
}
I test my app on 3 different phones, app is working fine. So I cannot test the code if I change something, as I could not tell the difference. What can cause the error and why does it emerge on only a few phones? (5% of the downloads). A guy contacted me with this error, there are a couple of contacts with special characters in his phone (HTC Legend CM 7.1, and a Vodafone 845 Android 2.1). So I added the same characters to a contact of mine, put blank spaces before the name, tried everything to mess with the contact name, app runs smoothly, so this is not the problem. I am out of the options.
You should check the contactName, if it isn't null. I think that the column, from which are you trying to get the value, is just empty.
if (contactName != null) { ... and so on
contactName is clearly coming back null. You need some sort of null check before you call indexOf on it.
// snip
while (cursor.moveToNext())
{
contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (contactName == null) continue;
spaceIndex = contactName.indexOf(' ');
spaceLastIndex = contactName.lastIndexOf(' ');
myArr.add(contactName);
}
contactName = cursor.getString(cursor.getColumnIndexorThrow(ContactsContract.Contacts.DISPLAY_NAME));
Even the display name can be null for some cases... say blank contact..
so check whether the contactName is null or not first before getting index from it.
Related
I know similar question has been asked before,
my code was working before, and now it does not, my app is the default dialer and is also a system app,
the canCurrentUserBlockNumbers(this) returns true, however always an empty cursor is returned now,
any suggestion?
Thank you
if (canCurrentUserBlockNumbers(this))
Toast.makeText(this, "ok", Toast.LENGTH_SHORT).show(); // I see this "OK" Toast
else
Toast.makeText(this, "KO", Toast.LENGTH_SHORT).show();
Cursor c = getContentResolver().query(BlockedNumberContract.BlockedNumbers.CONTENT_URI,
new String[]{BlockedNumberContract.BlockedNumbers.COLUMN_ID, BlockedNumberContract.BlockedNumbers.COLUMN_ORIGINAL_NUMBER,
BlockedNumberContract.BlockedNumbers.COLUMN_E164_NUMBER}, null, null, null);
TextView tv = findViewById(R.id.textviewblockednumbers);
tv.setText("ciao");
//int numElements=(c.getColumnCount();
while (c.moveToNext()) {
String name = c.getString(c.getColumnIndexOrThrow(BlockedNumberContract.BlockedNumbers.COLUMN_ORIGINAL_NUMBER));
tv.append(name);
tv.append(",");
c.close();
}
}
update for everyone interested,
with another phone of the same brand (Xiaomi) the cursor is not empty and I can read blocked numbers,
looks like a platform trouble
I faced one issue randomly whenever try to get the getSelectedCustomerPhone() then randomly cursor index out of Bound Exception will appear.
Is there anything wrong with this code?. I could not find the bug.
private String getSelectedCustomerPhone() {
myCursor.moveToPosition(selectedCustPosition);
String phone =
myCursor.getString(myCursor.getColumnIndex("cust_phone"));
if (phone != null) return phone;
return "";
}
It seems that selectedCustPosition is outside of the cursor range [0, Cursor.getCount() - 1]. Try understanding why it happens.
As a workaround to prevent the crash, you can add a check
if (0 <= selectedCustPosition && selectedCustPosition < myCursor.getCount()) {
myCursor.moveToPosition(selectedCustPosition);
// ...
}
But this is only a workaround that will more likely return incorrect phone number. Better to understand the real problem: why selectedCustPosition is incorrect.
I want to get all the browser history records from different browsers in android mobile phone ?(Maybe as you know, there are usually more than one browser apps in a phone ).Is there anyone has done this successfully and give me a hint ? Example code is preferred.Any help will be helpful.. Thanks a lot in advance ! :)
Just look at this. I used it in my code and I am getting browser history through it(Default Browser).
String[] proj = new String[] { Browser.BookmarkColumns.TITLE, Browser.BookmarkColumns.URL };
String selection = Browser.BookmarkColumns.BOOKMARK + " = 0"; // 0 = history, 1 = bookmark
mCursor = this.managedQuery(Browser.BOOKMARKS_URI, proj, selection, null, null);
this.startManagingCursor(mCursor);
mCursor.moveToFirst();
String title = "";
String url = "";
if (mCursor.moveToFirst() && mCursor.getCount() > 0) {
while (mCursor.isAfterLast() == false && cont) {
title = mCursor.getString(mCursor.getColumnIndex(Browser.BookmarkColumns.TITLE));
url = mCursor.getString(mCursor.getColumnIndex(Browser.BookmarkColumns.URL));
// Do something with title and url
mCursor.moveToNext();
}
}
Hope this helps you.
Limitations :
Browser.BOOKMARKS_URI will, at most, work for the open source Browser app that is part of the Android Open Source Project. Device manufacturers are welcome to replace that app with something else that will not be recording its history, bookmarks, or anything else in that ContentProvider. Similarly, users are allowed to download third-party browsers, which may not be storing things in that ContentProvider.
Why doesnt my Android Cursor go all the way to the end of the original "promise"??
My cursor.getCount() differs from my last cursor.getPosition(). Check my while loop! It is all I do with it!
Notes:
1. it is about querying the Contacts content provider (android api >5)
2. I display only the esential code
Cursor cursor = mContext.getContentResolver().query(mUri, mProjections, null, null, null);
Logger.d(TAG, "*** cursor.getCount(): "+cursor.getCount());
while (cursor.moveToNext()) {
Logger.d(TAG, "| position: "+cursor.getPosition());
processMainCursor(serializer, cursor);
}
cursor.close();
processMainCursor() will display data from cursor + do another queries: one 4 phones, one 4 emails, one 4 IM accounts:
void processMainCursor(XmlSerializer serializer, Cursor main_cursor) {
writeCursorData(serializer, main_cursor); //writes cursor data, column by column
writePhoneEntities(serializer, main_cursor);
writeEmailEntities(serializer, main_cursor);
writeIMEntities(serializer, main_cursor);
}
In none of my writeXXX methods do i close my main_cursor or move next!!!..have to trust me on that.. i just do a new query, print data & close that cursor
So statistics:
cursor.getCount() = 695 (always)
commenting writePhoneEntities, writeEmailEntities, writeIMEntities: cursor.getCount() =
last cursor.getPosition() = 695 (so correct!)
leaving one/two/all of my writeXEntities shows randomness; example: leaving them all:
last cursor.getPosition() sometimes displays 254, 257, 253, etc; leaving just phone & IM: 514, 510, 511, etc (so different RUN -> different last cursor.getPosition() VALUE)
So oppinions.. Why is that? Is it memory related?
Update:
Leaving any of my writeXEntities displays at the end in logcat:
Removing dead content provider: contacts
Update 2
Adding cursor.moveToFirst(); & doing loop like
do {
//do whatever you want
} while (cursor.moveToNext());
didn't do the job..
So maybe the answer is in this logcat entries:
05-21 23:29:30.209: I/ActivityThread(7085): Removing dead content provider: contacts
05-21 23:29:30.209: I/ActivityThread(7085): Removing dead content provider: com.android.contacts
SAMPLE OF a writeXEntity REMOVED
SOLUTION .. i wasnt closing the cursors from writeXEntity corectly (probably leaving quite a lot of open cursor after main while)
in reality i was closing like this
if(phone_cursor!=null && phone_cursor.getCount() > 0)
{
//... stuff
phone_cursor.close();
}
i should have closed after if
if(phone_cursor!=null && phone_cursor.getCount() > 0)
{
//... stuff
}
phone_cursor.close();
I guess leaving a basilion cursor open ..was the answer?!?
You need to move the cursor to the first row. Try adding cur.moveToFirst() before the while loop.
You might also consider using a do-while loop. This will ensure that you never skip over the first row in the cursor:
if (cursor.moveToFirst()) {
do {
//do whatever you want
} while (cursor.moveToNext());
}
cursor.close();
Well, they won't be the same number as the getCount is the number of items, and the position is the position, ( the first one being 0). So the final position should always be one less than the count.
If it's something other than that, I guess I'm not understanding your question properly.
Use cursor as shown below:
if(cursor.getCount() == 0)
{
//No entry found
}
else {
cursor.moveToFirst();
do {
//do whatever you want
} while (cursor.moveToNext());
cursor.close();
After reading the answer you already found (problem with closing cursors) I think that the best way to ensure you close them all is with this code:
Cursor c = null;
try {
c = <your query>;
if (c.moveToFirst()) { // No point in doing more if empty.
do {
<process this cursor row>
} while (c.moveToNext());
}
}
finally {
if (c != null) c.close(); // Not sure the check needed, maybe if query was really wrong.
}
I want to display all my records that are stored in the database
c = db.DBhelper.getChamp1(c.getCount);
//startManagingCursor(c);
int j = 0;
stateNameArray = new String[c.getCount()];
c.moveToFirst();
while(!c.isAfterLast()) {
stateNameArray[j] = c.getString(0);
j++;
Log.i("DEBUG_SQL","" + c.getString(0)+ " "+c.getString(j));
c.moveToNext();
}
//String resultat = ;
Log.i("DEBUG_SQL","" + c.getColumnIndex("article"));
I get an error when I write c.getCount – why? When I write a number like 1 or 2 or 3... that works.
And if I write
c = db.rawQuery("SELECT * FROM loan", null);
I get an error, but if I write
db.rawQuery("SELECT * FROM loan WHERE _id=1", null);
That works. Why?
At the very least, I see a problem with this log statement, where c.getString(j) doesn't make sense. And it may trigger an error as j gets larger.
Log.i("DEBUG_SQL","" + c.getString(0)+ " "+c.getString(j));
What data did you intend to access with the statement c.getString(j)?
On the getCount error. I assumed the error in the following was a typo. But is this where the error associated with getCount was located?
c = db.DBhelper.getChamp1(c.getCount);
But I shouldn't assume - you never know. It should read (add brackets to the method call).
c = db.DBhelper.getChamp1(c.getCount());
And as #Barak mentioned, what is going on with this statement?
To answer the question about your getCount isuue, you get the error because of this:
c = db.DBhelper.getChamp1(c.getCount);
You're trying to get the count of the cursor before you have it (and you're missing ()).
This would work since you have a cursor to count before you pull the next one:
c = db.getSomeCursor;
c1 = db.DBhelper.getChamp1(c.getCount());
Let us know what you're trying to achieve (I can't figure it out from the code you posted) and maybe we can be more helpful.