Memory issue/ Cursor shows null pointer
I am getting the name and email id of the contacts and it works fine in HTC Desire, but whenever i tried to run the code in galaxy tab it shows me No memory in memObj and null pointer on... while (emailCur.moveToNext()) .
Here's my code
private void getPeopleList() {
String name, email;
Runtime.getRuntime().maxMemory();
c = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (c.getCount() > 0) {
while (c.moveToNext()) {
name = c.getString(c
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String id = c.getString(c
.getColumnIndex(BaseColumns._ID));
cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[] { id }, null);
emailCur = cr.query(Email.CONTENT_URI, null,
Email.CONTACT_ID + " = " + id, null, null);
int j = 0;
while (emailCur.moveToNext()) {
bindData = new BindData[emailCur.getCount()];
bindData = new BindData();
email = emailCur
.getString(emailCur
.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
bindData.setEmailId(email);
bindData.setName(name);
Log.d("doubt : ", name+"//"+email);
mArrayList.add(bindData);
j++;
}
emailCur.close();
}
c.close();
}
}
I am calling this method using worker thread runnable.
If any information is required let me know.
here's my log cat
06-29 16:13:26.854: ERROR/IMemory(6180): binder=0xdf3f68 transaction failed fd=-2147483647, size=0, err=-2147483646 (Unknown error: 2147483646)
06-29 16:13:26.854: ERROR/IMemory(6180): cannot dup fd=-2147483647, size=0, err=-2147483646 (Bad file number)
06-29 16:13:26.854: ERROR/IMemory(6180): cannot map BpMemoryHeap (binder=0xdf3f68), size=0, fd=-1 (Bad file number)
06-29 16:13:26.854: ERROR/JavaBinder(6180): *** Uncaught remote exception! (Exceptions are not yet supported across processes.)开发者_StackOverflow社区
06-29 16:13:26.854: ERROR/JavaBinder(6180): java.lang.RuntimeException: No memory in memObj
06-29 16:13:26.854: ERROR/JavaBinder(6180): at android.database.CursorWindow.native_init(Native Method)
06-29 16:13:26.854: ERROR/JavaBinder(6180): at android.database.CursorWindow.<init>(CursorWindow.java:518)
06-29 16:13:26.854: ERROR/JavaBinder(6180): at android.database.CursorWindow.<init>(CursorWindow.java:27)
06-29 16:13:26.854: ERROR/JavaBinder(6180): at android.database.CursorWindow$1.createFromParcel(CursorWindow.java:493)
06-29 16:13:26.854: ERROR/JavaBinder(6180): at android.database.CursorWindow$1.createFromParcel(CursorWindow.java:496)
06-29 16:13:26.854: ERROR/JavaBinder(6180): at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:103)
06-29 16:13:26.854: ERROR/JavaBinder(6180): at android.os.Binder.execTransact(Binder.java:288)
06-29 16:13:26.854: ERROR/JavaBinder(6180): at dalvik.system.NativeStart.run(Native Method)
06-29 16:13:26.858: ERROR/AndroidRuntime(6388): FATAL EXCEPTION: main
06-29 16:13:26.858: ERROR/AndroidRuntime(6388): java.lang.NullPointerException
06-29 16:13:26.858: ERROR/AndroidRuntime(6388): at com.meomyo.fanaticfaninit.utils.MyCustomList.getPeopleList(MyCustomList.java:172)
06-29 16:13:26.858: ERROR/AndroidRuntime(6388): at com.meomyo.fanaticfaninit.utils.MyCustomList.access$0(MyCustomList.java:144)
06-29 16:13:26.858: ERROR/AndroidRuntime(6388): at com.meomyo.fanaticfaninit.utils.MyCustomList$1.run(MyCustomList.java:121)
06-29 16:13:26.858: ERROR/AndroidRuntime(6388): at android.os.Handler.handleCallback(Handler.java:587)
06-29 16:13:26.858: ERROR/AndroidRuntime(6388): at android.os.Handler.dispatchMessage(Handler.java:92)
06-29 16:13:26.858: ERROR/AndroidRuntime(6388): at android.os.Looper.loop(Looper.java:123)
06-29 16:13:26.858: ERROR/AndroidRuntime(6388): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-29 16:13:26.858: ERROR/AndroidRuntime(6388): at java.lang.reflect.Method.invokeNative(Native Method)
06-29 16:13:26.858: ERROR/AndroidRuntime(6388): at java.lang.reflect.Method.invoke(Method.java:521)
06-29 16:13:26.858: ERROR/AndroidRuntime(6388): at
please can you try
if (emailCur.moveToFirst()) {
do {
} while (emailCur.moveToNext());
}
instead of emailCur.moveToNext()
Got the Answer: The Problem is with my ArrayList(mArraylist) that currently i am using, I am passing this arraylist with my adapter also, that is the reason of holding the objects too long for an arraylist. So here's the solution create one Dummy ArrayList Put your data in it and at the time of passing the ArrayList into an adapter copy it into the original ArrayList.
Here's the code.
ArrayList<BindData> dummyArrayList= new ArrayList<BindData>
inside while loop
dummyArrayList.add(bindData);
and before creating an instance of Adapter copy this Dummy ArrayList into the original ArrayList(The list that you want to pass into the Adapter)
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
getPeopleList();
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
mArrayList.clear();
for(int i = 0; i < dummyArrayList.size(); i++){
mArrayList.add(dummyArrayList.get(i));
}
dummyArrayList.clear();
myAdapter.notifyDataSetChanged();
progressDialog.cancel();
}
});
}
}).start();
after that you can create the adapter instance by passing the original Arraylist in it.
MyAdapter myAdapter= new MyAdapter(this, R.layout.contactlistrow, mArrayList);
it will work fine.
Thanks
精彩评论