Cursor window could not be created from binder - android

1 Cursor cursor = contentResolver.query(MY_URI, new String[] { "first" }, null, null, null);
2 if (cursor != null) {
3 if (cursor.moveToFirst()) {
4 first = cursor.getString(cursor.getColumnIndex("first"));
5 cursor.close();
6 }
7 }
Then on line #3 (according to the logs), I every now and then I come across this exception (excerpt below):
android.database.CursorWindowAllocationException: Cursor window could not be created from binder.
at android.database.CursorWindow.<init>(CursorWindow.java:134)
at android.database.CursorWindow.<init>(CursorWindow.java:41)
at android.database.CursorWindow$1.createFromParcel(CursorWindow.java:709)
at android.database.CursorWindow$1.createFromParcel(CursorWindow.java:707)
at android.database.CursorWindow.newFromParcel(CursorWindow.java:718)
at android.database.BulkCursorProxy.getWindow(BulkCursorNative.java:196)
...
Any ideas why it is throwing this exception? Thanks!

I suspect the error may be related to you not closing your cursors properly all the time. Try:
Cursor cursor = contentResolver.query(MY_URI, new String[] { "first" }, null, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
first = cursor.getString(cursor.getColumnIndex("first"));
}
cursor.close(); ///// Changed here
}
The cursor should always be closed (regardless of whether or not its empty). Make sure the rest of your app is doing this as well.

Try another thread
new Thread(new Runnable(){ public void run(){
...here all code
}});
. But by Android SDK source codes look like 4.0.2_r1
130 private CursorWindow(Parcel source) {131 mStartPos = source.readInt();132 mWindowPtr = nativeCreateFromParcel(source);133 if (mWindowPtr == 0) {134 throw new CursorWindowAllocationException("Cursor window could not be "135 + "created from binder.");136 }137 mName = nativeGetName(mWindowPtr);138 mCloseGuard.open("close");139 }
where mWIndowPtr is Int

Try out this way:
if (cursor != null) {
cursor.moveToFirst();
do {
first = cursor.getString(cursor.getColumnIndex("first"));
}while(cursor.moveToNext());
}

Related

Using WHERE Clause SQLite database in Android?

This is my code:
public int getIdMotChuDe(String tenChuDe) {
int IDChuDe = 0;
try
{
Cursor c = null;
c = database.rawQuery(
"SELECT ChuDeID FROM DanhSachChuDe WHERE TenChuDe = ?"
, new String[] {tenChuDe});
c.moveToFirst();
IDChuDe = c.getInt(c.getColumnIndex("ChuDeID"));
c.close();
}
catch(Exception e)
{
e.printStackTrace();
}
return IDChuDe;
}
I'm trying to get ChuDeID from DanhSachChuDe table with condition in WHERE clause. But i don't know why this function always return 0.
Help me please. Thanks! Sorry because my english.
This could be because an Exception is being thrown. The code you are using is not correctly checking the state of your Cursor - it attempts to moveToFirst() before any checking too see if the object is not null.
Your code also assumes a result is always returned. This is bad practise, and should be avoided. A much safer and more common solution is the following:
if (cursor != null) {
// If the cursor has results, move the cursor to first row
if (cursor.moveToFirst()) {
do {
// YOUR METHODS HERE
// then move to next row
} while (cursor.moveToNext());
}
}

Android Cursor wont Loop, Application Freezes

So here it goes, i have this application retrieves records from database, i have 5 entries from my database, and retrieved its using this code;
Cursor c = dbconnection.rawQuery("SELECT * from Patients", null);
after that i looped it to retrieve the data pero row in my Database as such;
c.moveToFirst();
while(!c.isAfterLast())
{
//Some code to put records per column in to a Patient Object
c.moveToNext();
}
So my problem is that as it enters the loop my Emulator freezes and as i tried to display per record into a Log, i won't cuz the emulator it self already frozed.
Can somebody enlighten me into this matter, This issue is really really new to me
EDIT
yes already tried what baya and ofir suggested.. they worked out but i have this null error during iteration with this loop code
Cursor c = dbHelper.retrieveAllData();
c.moveToFirst();
while(c.moveToNext())
{
Log.d("dbcheck",Integer.toString(c.getPosition()));
//Log.d("dbcheck",c.getString(c.getColumnIndex("firstname")));
//Log.d("dbcheck",c.getString(c.getColumnIndex("lastname")));
**p.setFname(c.getString(c.getColumnIndex("firstname")));**
p.setMi(c.getString(c.getColumnIndex("middleinitial")));
p.setLname(c.getString(c.getColumnIndex("lastname")));
p.setAddr(c.getString(c.getColumnIndex("address")));
p.setAge(c.getInt(c.getColumnIndex("age")));
p.setMed_history(c.getString(c.getColumnIndex("med_history")));
p.setPat_status(c.getString(c.getColumnIndex("status")));
patientList.add(p);
}
i have a null exception error on the p.setFname() line.. i don't know how it became null where in fact i already displayed it with Log using that code that is commented out..
Just try,
Cursor c = dbconnection.rawQuery("SELECT * from Patients", null);
if (c.getCount() > 0) {
Patient p;
while(c.moveToNext) {
//initialize ur object to store new patient info.
p = new Patient();
p.setFname(c.getString(c.getColumnIndex("firstname")));
//add newly created patient to ur list
patientList.add(p);
}
}
c.close();
Try do it like this:
// return all columns
Cursor cursor = mDb.query(Patients, null, null, null, null, null, null);
if ((cursor != null) && (cursor.getCount() > 0)) {
while(cursor.moveToNext()){
//Some code to put records per column in to a Patient Object
}
cursor.close();
}

how to resolve ArrayIndexOutOfBoundsException [duplicate]

This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed 1 year ago.
I am trying to access all the data from database "listOfFolder" table "folder" and want to store the data in a string array folders[] but i am getting how to resolve ArrayIndexOutOfBoundsException.
try{
mydb = openOrCreateDatabase("listOfFolder", MODE_WORLD_WRITEABLE, null);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
int count = 0;
String folders[] = null;
Cursor folderCursor = mydb.query("folder", null, null, null, null, null, null);
while(folderCursor.moveToNext()) {
folders[count] = folderCursor.getString(folderCursor.getColumnIndex("name"));
count++;
}
ListView list = (ListView)findViewById(R.id.lv);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(DropboxActivity.this, android.R.layout.simple_list_item_1,folders);
list.setAdapter(adapter);
setContentView(R.layout.listoffolder);
Change
String folders[] = null;
Cursor folderCursor = mydb.query("folder", null, null, null, null, null, null);
while(folderCursor.moveToNext()){
folders[count] = folderCursor.getString(folderCursor.getColumnIndex("name"));
count++;
}
to
Cursor folderCursor = mydb.query("folder", null, null, null, null, null, null);
if (folderCursor.moveToFirst()) {
String folders[] = new String[folderCursor.getCount()];
do {
folders[count] = folderCursor.getString(folderCursor.getColumnIndex("name"));
count++;
} while(folderCursor.moveToNext());
}
Here's your problem:
String folders[] = null;
What did you expect to happen?
This code is a bad idea, because you have no idea how large a set the query will bring back. You have to call new to allocate a large enough array.
I'd prefer a collection like a List if it's available to you. Check this one out, too.
Like this:
ArrayList<datatypehere> namehere=new ArrayList<datatypehere>();
Then you can add elements to the list by:
String example1 = "this is a string";
namehere.add(example1);
Theres quite abit on lists in the JavaDocs and Java Tutorials if you search for them.

Is there any library to track call time and sms time in android?

I want to a software to calculate call time and sms time usage in Android,
anyone has idea about any build-in library which handle such things ????
any way I can extract call logs in Android???
You could build the call usage data from the content provider CallLog.
The following code should work:
Cursor c = null;
try {
c = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, null);
if (c != null && c.moveToFirst()) {
do {
int duration = c.getInt(c.getColumnIndex(CallLog.Calls.DURATION));
// do something with duration
} while (c.moveToNext());
}
} finally {
if (c != null) {
c.close();
}
}

Idiom to close a cursor

Which of the following two should I be using to make sure that all the cursors are closed?
Cursor c = getCursor();
if(c!=null && c.getCount()>0){
try{
// read values from cursor
}catch(..){}
finally{
c.close();
}
}//end if
OR
Cursor c = getCursor();
try{
if(c!=null && c.getCount()>0){
// read values from cursor
}//end if
}catch(..){
}finally{
c.close();
}
Please advise.
Neither, but the second one was closest.
Option 1 doesn't properly close the
Cursor when getCount() == 0
Option 2 leaves the finally block exposed to a null pointer exception
I would use:
Cursor c = getCursor();
try {
if(c!=null && c.getCount()>0){
// do stuff with the cursor
}
}
catch(..) {
//Handle ex
}
finally {
if(c != null) {
c.close();
}
}
... or if you expect the cursor to be null frequently, you could turn it on its head a little bit:
Cursor c = getCursor();
if(c != null) {
try {
if(c.getCount()>0) {
// do stuff with the cursor
}
}
catch(..) {
//Handle ex
}
finally {
c.close();
}
}
This is even better:
does not use c.getCount() - counting might require extra work for the database and is not needed
initialize the cursor before the query block, so failure to create the query is not followed by the finally block
The code:
Cursor c = query(....);
if (c != null) {
try {
while (c.moveToNext()) { // If empty or after last record it returns false.
// process row...
}
}
finally {
c.close();
}
}
Note that c might be null in case of error or empty cursor. See https://stackoverflow.com/a/16108435/952135. I would report null return value in case of empty cursor as a bug, though.
Best practice is the one below:
Cursor c = null;
try {
c = query(....);
while (c.moveToNext()) { // If empty or next to last record it returns false.
// do stuff..
}
} finally {
if (c != null && !c.isClosed()) { // If cursor is empty even though should close it.
c.close();
c = null; // high chances of quick memory release.
}
Depends on what you're catching, but I'd say the second one, just in case c.getCount() throws an exception.
Also, some indentation wouldn't go amiss :)
I'd say the first one, mainly because the second one will try to call c.close() even if c is null. Also, according to the docs, getCount()doesn't throw any exceptions, so there's no need to include it in the try block.
I think my answer is the best one :
Cursor cursor = null;
try {
cursor = rsd.rawQuery(querySql, null);
if (cursor.moveToFirst()) {
do {
// select your need data from database
} while (cursor.moveToNext());
}
} finally {
if (cursor != null && !cursor.isClosed()) {
cursor.close();
cursor = null;
}
}
I think #skylarsutton's is a right answer for the question. However, I want to leave codes for the question (any codes in answers seems to have some flaws). Please consider to use my code.
Cursor c = query(....);
if (c != null) {
try {
//You have to use moveToFirst(). There is no quarantee that a cursor is located at the beginning.
for(c.moveToFirst();!c.isAfterLast();c.moveToNext()) {
// process row...
}
}
finally {
c.close();
}
}

Categories

Resources