0

我得到的聯繫人的名稱和電子郵件ID,它在HTC Desire中工作正常,但每當我嘗試在星系選項卡中運行代碼時,它顯示我memObj中沒有內存和空指針on ... while(emailCur.moveToNext())。內存問題/光標顯示空指針

這裏是我的代碼

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(); 

    } 


} 

我打電話使用的工作線程運行的這種方法。

如果需要任何信息,請告訴我。

這裏是我的日誌貓

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.) 
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 
+0

哪條線撞上了? – nickfox

+0

@Nic:while(emailCur.moveToNext())。 – PiyushMishra

回答

1

得到了答案:這個問題是我的ArrayList(mArraylist)目前,我認爲使用時,我也將這個數組列表與我的適配器一起傳遞,這就是爲數組列表過長對象的原因。因此,這裏的解決方案創建一個Dummy ArrayList將數據放入其中,並在將ArrayList傳遞到適配器時將其複製到原始ArrayList中。

這是代碼。

ArrayList<BindData> dummyArrayList= new ArrayList<BindData> 

內while循環

dummyArrayList.add(bindData); 

和創建適配器的實例之前複製此虛擬的ArrayList到原來的ArrayList(要傳遞到適配器列表)

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(); 

之後,您可以通過傳遞原始Arraylist來創建適配器實例。

MyAdapter myAdapter= new MyAdapter(this, R.layout.contactlistrow, mArrayList); 

它會正常工作。

謝謝

+1

發現多了一個答案:刪除此代碼 「cr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 空, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +」=「,新的String [] {ID},空? ); 「它再次正常工作:)。 – PiyushMishra

1

請您可以嘗試

if (emailCur.moveToFirst()) { 
do { 

} while (emailCur.moveToNext()); 
} 

,而不是emailCur.moveToNext()

+0

沒有工作..別的....我不這麼認爲,不會有問題...謝謝你。 – PiyushMishra

+0

可以請你過去你的logcat嗎? – Nikhil

+0

@Nick:在emailCur裏面我可以看到Log Log.d(「疑問:」,名稱+「//」+ email); 我能夠獲得名稱和電子郵件ID的,但在最後我得到了這個錯誤。 – PiyushMishra