滚动时ListView空指针异常

时间:2011-03-01 10:44:32

标签: android

我的代码:
         ListContacts .java

public class ListContacts extends ListActivity {

  @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    /** Associate the xml with the activity */
    setContentView(R.layout.home);

              Cursor cursor = getContentResolver().query(
            ContactsContract.Contacts.CONTENT_URI, null,
            ContactsContract.Contacts.HAS_PHONE_NUMBER + " = 1", null,
            "UPPER(" + ContactsContract.Contacts.DISPLAY_NAME + ") ASC");

    startManagingCursor(cursor);

    /** start mapping */
    String[] columns = new String[] { ContactsContract.Contacts.DISPLAY_NAME };
    int[] names = new int[] { R.id.contact_name };
    lAdapter = new ImageCursorAdapter(this, R.layout.contact_listview,
            cursor, columns, names);

    setListAdapter(lAdapter);
}

ImageCursorAdapter .java

 public class ImageCursorAdapter extends SimpleCursorAdapter {

 public View getView(int pos, View inView, ViewGroup parent) {

//System.out.println("  ImageCursorAdapter : getView : ");
View v = inView;
String phoneNumber = null;
String contactID = null;

// Associate the xml file for each row with the view
if (v == null) {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    v = inflater.inflate(R.layout.main, null);
}
this.c.moveToPosition(pos);

/**
 * Get the strings with the name and number of the person that the
 * current row
 */

//
String lName = this.c.getString(this.c
    .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
//System.out.println("  ImageCursorAdapter : getView : lName " + lName);

/**
 * Get the person's id and from the id retrieve the contact's picture
 */

int contactId = c.getColumnIndex(ContactsContract.Contacts._ID);
/*System.out.println("  ImageCursorAdapter : getView : contactId "
    + contactId);*/

long contact_id = this.c.getLong(this.c
    .getColumnIndex(ContactsContract.Contacts._ID));

Cursor cr = context.getContentResolver().query(Phone.CONTENT_URI,
    new String[] { Phone.NUMBER, Phone.CONTACT_ID },
    Phone.CONTACT_ID + "=" + contact_id, null,
    Phone.IS_SUPER_PRIMARY + " DESC");

if (cr.moveToFirst()) {
   // System.out.println(" Phone number : " + cr.getString(0));
    phoneNumber = cr.getString(0);

    contactID = cr.getString(1);
    //System.out.println("contactID " + contactID);
}

}

ImageCursorAdapter.java strong>同时滚动listview 9-10次。例外情况如下:

03-01 16:04:23.007: ERROR/IMemory(9227): cannot dup fd=1023, size=1048576, err=0 (Too many open files)
03-01 16:04:23.007: ERROR/IMemory(9227): cannot map BpMemoryHeap (binder=0x3e5af0), size=1048576, fd=-1 (Bad file number)
03-01 16:04:23.007: ERROR/JavaBinder(9227): *** Uncaught remote exception!  (Exceptions are not yet supported across processes.)
03-01 16:04:23.007: ERROR/JavaBinder(9227): java.lang.RuntimeException: No memory in memObj
03-01 16:04:23.007: ERROR/JavaBinder(9227):     at android.database.CursorWindow.native_init(Native Method)
03-01 16:04:23.007: ERROR/JavaBinder(9227):     at android.database.CursorWindow.<init>(CursorWindow.java:518)
03-01 16:04:23.007: ERROR/JavaBinder(9227):     at android.database.CursorWindow.<init>(CursorWindow.java:27)
03-01 16:04:23.007: ERROR/JavaBinder(9227):     at android.database.CursorWindow$1.createFromParcel(CursorWindow.java:493)
03-01 16:04:23.007: ERROR/JavaBinder(9227):     at android.database.CursorWindow$1.createFromParcel(CursorWindow.java:496)
03-01 16:04:23.007: ERROR/JavaBinder(9227):     at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:108)
03-01 16:04:23.007: ERROR/JavaBinder(9227):     at android.os.Binder.execTransact(Binder.java:288)
03-01 16:04:23.007: ERROR/JavaBinder(9227):     at dalvik.system.NativeStart.run(Native Method)


03-01 16:04:23.017: ERROR/AndroidRuntime(9560): java.lang.NullPointerException
03-01 16:04:23.017: ERROR/AndroidRuntime(9560):     at com.test.ImageCursorAdapter.getView(ImageCursorAdapter.java:143)
03-01 16:04:23.017: ERROR/AndroidRuntime(9560):     at android.widget.AbsListView.obtainView(AbsListView.java:1294)
03-01 16:04:23.017: ERROR/AndroidRuntime(9560):     at android.widget.ListView.makeAndAddView(ListView.java:1727)
03-01 16:04:23.017: ERROR/AndroidRuntime(9560):     at android.widget.ListView.fillUp(ListView.java:682)
03-01 16:04:23.017: ERROR/AndroidRuntime(9560):     at android.widget.ListView.fillGap          (ListView.java:628)
03-01 16:04:23.017: ERROR/AndroidRuntime(9560):     at android.widget.AbsListView.trackMotionScroll(AbsListView.java:2944)
03-01 16:04:23.017: ERROR/AndroidRuntime(9560):     at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:2485)
03-01 16:04:23.017: ERROR/AndroidRuntime(9560):     at android.os.Handler.handleCallback(Handler.java:587)
03-01 16:04:23.017: ERROR/AndroidRuntime(9560):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-01 16:04:23.017: ERROR/AndroidRuntime(9560):     at android.os.Looper.loop(Looper.java:144)
03-01 16:04:23.017: ERROR/AndroidRuntime(9560):     at android.app.ActivityThread.main(ActivityThread.java:4937)
03-01 16:04:23.017: ERROR/AndroidRuntime(9560):     at java.lang.reflect.Method.invokeNative(Native Method)
03-01 16:04:23.017: ERROR/AndroidRuntime(9560):     at java.lang.reflect.Method.invoke(Method.java:521)
03-01 16:04:23.017: ERROR/AndroidRuntime(9560):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-01 16:04:23.017: ERROR/AndroidRuntime(9560):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-01 16:04:23.017: ERROR/AndroidRuntime(9560):     at dalvik.system.NativeStart.main(Native Method)

请帮助

1 个答案:

答案 0 :(得分:3)

确保每次在适配器中关闭cr光标

Cursor cr = ...
try {
    // use cr
} finally {
    cr.close();
}