问题 使用SimpleCursorAdapter在onLoaderFinished中使用NullPointerException


我换了一个 ResourceCursorAdapter 我用过的地方 newView 和 bindView 到了 SimpleCursorAdapter 我只使用的地方 getView 方法。

现在我有一个错误 onLoaderFinished。虽然它给了我 NullPointerException 上 adapter.swapCursor(cursor) 我的适配器和游标对象都是 不为空。我将在下面发布我的所有代码。非常感谢任何帮助(没有多余的头发留下来)。

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

10325
2017-09-02 11:38


起源

提供您的import语句,因为精确的Cursor包是否被压缩 - Mohammod Hossain
我已将我的导入添加到原始问题中 - Stephen
你也可以发布logcat吗?我只需要知道堆栈是否在您提到的位置结束或进一步 - nandeesh


答案:


一个电话 swapCursor 的方法 SimpleCursorAdapter class将触发一个映射列名的函数 String 数组提供给构造函数(第4个参数)到表示列索引的整数数组。当你通过 null 在构造函数中 MyAdapter 为了 String 表示光标中列名的数组,这将抛出一个 NullPointerException 以后的时候 swapCursor 将尝试进行映射( NullPointerException 应该出现在一个方法中 findColumns,这是使用列名的实际方法 String 数组)。

解决方案是传递有效的 String 数组,你可能也想这样做 int 表示放置数据的视图的id的数组:

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  方法并使用这两种方法 newView 和 bindView 因为它们提供了更好的逻辑分离。


12
2017-09-08 12:48





adapter = new 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();
    }   

2
2017-09-02 11:51



您好,感谢您的回复。传入null游标的原因是因为在LoaderManager完成之前我不会有游标对象。我应该指出,使用ResourceCursorAdapter时,此代码完全正常 - Stephen