使用SimpleCursorAdapter时,在onLoaderFinished中出现NullPointerException

11

我已经从使用了newViewbindViewResourceCursorAdapter切换到了使用仅有getView方法的SimpleCursorAdapter。现在我在onLoaderFinished中遇到了错误,尽管我的适配器和游标对象都不为null,但是在adapter.swapCursor(cursor)上给出了NullPointerException。下面是我所有的代码。非常感谢任何帮助(我没剩多少头发可供拔了)。

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.ResourceCursorAdapter;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

public class ContactSelect extends FragmentActivity implements LoaderManager.LoaderCallbacks<Cursor> {
private static final int LOADER_ID = 1;
private MyAdapter adapter;
private ListView list;
private View row;
private SparseBooleanArray checkedState = new SparseBooleanArray();

@SuppressLint({ "NewApi", "NewApi" })
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setContentView(R.layout.activity_contact_select);       

    adapter = new MyAdapter(this, R.layout.contacts_select_row, null, null, null, 0);     

    getSupportLoaderManager().initLoader(LOADER_ID, null, this);        

    list = (ListView)findViewById(R.id.list);                 

    list.setAdapter(adapter);
    list.setEmptyView(findViewById(R.id.empty));    

}   

@SuppressLint("NewApi")
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
    final String projection[] = new String[]{ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME};
    final Uri uri = ContactsContract.Contacts.CONTENT_URI;

    final String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1" + 
    " AND " + ContactsContract.Contacts.IN_VISIBLE_GROUP + " =1";

    final String order = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

    final CursorLoader loader = new CursorLoader(this, uri, projection, selection, null, order);

    return loader;      
}

public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
    for(int i=0;i<cursor.getCount();i++){
        checkedState.put(i, false);
    }

    adapter.swapCursor(cursor);                 
}

public void onLoaderReset(Loader<Cursor> loader) {
    adapter.swapCursor(null);       
}

private class MyAdapter extends SimpleCursorAdapter implements OnClickListener{
    private CheckBox markedBox;
    private TextView familyText;
    private Context context;
    private Cursor cursor;

    public MyAdapter(Context context, int layout, Cursor c, String[] from,
            int[] to, int flags) {
        super(context, layout, c, from, to, flags);

        this.context = context;
        this.cursor = getCursor();
    }

    @Override
    public View getView(int position, View view, ViewGroup group) {

        final LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = li.inflate(R.layout.contacts_select_row, group, false);

        view.setTag(cursor.getPosition());
        view.setOnClickListener(this);

        familyText = (TextView)view.findViewById(R.id.contacts_row_family_name);
        markedBox = (CheckBox)view.findViewById(R.id.contacts_row_check);
        familyText.setText(cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)));

        boolean currentlyChecked = checkedState.get(cursor.getPosition());
        markedBox.setChecked(currentlyChecked);     


        setProgressBarIndeterminateVisibility(false);

        return super.getView(position, view, group);
    }

    public void onClick(View view) {
        int rowId = (Integer)view.getTag();
        Log.d("OnClick", String.valueOf(rowId));
        boolean currentlyChecked = checkedState.get(rowId);
        markedBox.setChecked(!currentlyChecked);
        checkedState.put(rowId, !currentlyChecked);
        Log.d("checkedState", "checkedState(" + rowId + ") = " + checkedState.get(rowId));
    }       
      }     
}

请提供您的导入语句,以确定是否已导入准确的Cursor包。 - Mohammod Hossain
我已经将我的导入添加到原始问题中。 - Stephen
3
你能同时发布一下logcat吗?我只需要知道堆栈是在你提到的那个点结束还是继续往下走。 - nandeesh
2个回答

12
调用SimpleCursorAdapter类的swapCursor方法将触发一个函数,该函数将从构造函数提供的String数组(第四个参数)中将列名映射到表示列索引的整数数组。由于您在MyAdapter的构造函数中将代表光标列名的String数组设置为null,因此当swapCursor尝试进行映射时,它将在稍后抛出NullPointerException(NullPointerException应该出现在使用列名String数组的实际方法findColumns中)。
解决方案是传递有效的String数组,您也可以为表示要放置数据的视图的ID的int数组执行此操作:
String[] from = {ContactsContract.Contacts.DISPLAY_NAME};
int[] to = {R.id.contacts_row_family_name, R.id.contacts_row_check};
adapter = new MyAdapter(this, R.layout.contacts_select_row, null, from, to, 0);
我不知道你想要做什么,但是你对 getView 方法的实现不太正确:
你完成了 getView 方法的常规操作(创建布局、查找视图、绑定数据),然后简单地从超类返回视图(?!?)。这样做可能只会看到没有内容的默认布局。
你编写的 getView 方法的效率不是很高,你可能需要查看视图回收和视图持有者模式。 cursor.getPosition() 不能像你想的那样工作,因为你没有将游标移动到正确的位置。默认情况下,基于游标的适配器会在 getView 方法中为您执行此操作。但是,由于您重写了该方法,因此您需要移动游标的位置。
你应该保留 getView 方法,并使用两个方法 newViewbindView ,因为它们提供了更好的逻辑分离。

2

适配器=新的MyAdapter(this,R.layout.contacts_select_row,null,null,null,0);

在你的MyAdapter类中,你正在传递空游标。

  public MyAdapter(Context context, int layout, Cursor c, String[] from,
            int[] to, int flags) {
        super(context, layout, c, from, to, flags);

        this.context = context;
        this.cursor = getCursor();
    }   

你好,感谢回复。传递空游标的原因是因为在LoaderManager完成之前,我没有光标对象。我应该指出,在使用ResourceCursorAdapter时,这段代码完全正常运行。 - Stephen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接