错误:使用CursorAdapter从列表的onItemClick启动活动

时间:2012-01-20 08:17:37

标签: android android-listview android-contacts android-cursoradapter

我正在尝试构建联系人应用程序

在主要活动中,我显示所有联系人的列表。

但是,当我尝试在项目之后启动活动时 单击列表,我收到错误:

  

致命例外:主01-20 09:54:48.407:E / AndroidRuntime(8459):   java.lang.IllegalStateException:只应在调用时调用   光标有效

似乎不知何故,适配器方法(getView,getCount)一直在调用

我的代码如下,

public void onCreate(Bundle icicle)
{
    super.onCreate(icicle);

    setContentView(R.layout.main);

    initComponents();

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

    c = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, sortOrder);

    startManagingCursor(c);

    final MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(mContext, c);

    list.setAdapter(adapter);

    list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id)
        {
             Intent intent  = new Intent (mContext, SingleContact.class);
                         startActivity(intent);
        }
    });

}

/**
 * An adapter which handle the contacts
 * 
 * @author intel
 * 
 */
public class MySimpleArrayAdapter extends CursorAdapter
{
    private LayoutInflater mInflater;
    private Cursor cur;

    public MySimpleArrayAdapter(Context context, Cursor c)
    {
        super(context, c);
        this.mInflater = LayoutInflater.from(context);
        this.cur = c;
    }

    public MySimpleArrayAdapter(Context context, Cursor c, boolean a)
    {
        super(context, c, a);
        this.mInflater = LayoutInflater.from(context);
        this.cur = c;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor)
    {
        TextView tvName = null;

        tvName = (TextView) view.findViewById(R.id.contact_item_text_view_name);

        tvName.setText(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent)
    {
        TextView tvName = null;

        ImageView ivPhoto;

        mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View convertView = mInflater.inflate(R.layout.contact_item, parent, false);

        tvName = (TextView) convertView.findViewById(R.id.contact_item_text_view_name);

        tvName.setText(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));

        return convertView;
    }


    @Override
    public int getCount()
    {
        Thread.dumpStack();

        return cur.getCount();
    }

}

这是日志:

  

01-20 09:54:48.407:E / AndroidRuntime(8459):致命异常:主要   01-20 09:54:48.407:E / AndroidRuntime(8459):java.lang.IllegalStateException:只应在游标有效时调用   01-20 09:54:48.407:E / AndroidRuntime(8459):在android.widget.CursorAdapter.getView(CursorAdapter.java:175)   01-20 09:54:48.407:E / AndroidRuntime(8459):在android.widget.AbsListView.obtainView(AbsListView.java:1409)   01-20 09:54:48.407:E / AndroidRuntime(8459):在android.widget.ListView.measureHeightOfChildren(ListView.java:1307)   01-20 09:54:48.407:E / AndroidRuntime(8459):在android.widget.ListView.onMeasure(ListView.java:1127)   01-20 09:54:48.407:E / AndroidRuntime(8459):在android.view.View.measure(View.java:8510)   01-20 09:54:48.407:E / AndroidRuntime(8459):在android.widget.RelativeLayout.measureChild(RelativeLayout.java:566)   01-20 09:54:48.407:E / AndroidRuntime(8459):在android.widget.RelativeLayout.onMeasure(RelativeLayout.java:381)   01-20 09:54:48.407:E / AndroidRuntime(8459):在android.view.View.measure(View.java:8510)   01-20 09:54:48.407:E / AndroidRuntime(8459):在android.widget.RelativeLayout.measureChildHorizo​​ntal(RelativeLayout.java:581)   01-20 09:54:48.407:E / AndroidRuntime(8459):在android.widget.RelativeLayout.onMeasure(RelativeLayout.java:365)   01-20 09:54:48.407:E / AndroidRuntime(8459):在android.view.View.measure(View.java:8510)   01-20 09:54:48.407:E / AndroidRuntime(8459):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3202)   01-20 09:54:48.407:E / AndroidRuntime(8459):在android.widget.FrameLayout.onMeasure(FrameLayout.java:250)   01-20 09:54:48.407:E / AndroidRuntime(8459):在android.view.View.measure(View.java:8510)   01-20 09:54:48.407:E / AndroidRuntime(8459):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3202)   01-20 09:54:48.407:E / AndroidRuntime(8459):在android.widget.FrameLayout.onMeasure(FrameLayout.java:250)   01-20 09:54:48.407:E / AndroidRuntime(8459):在android.view.View.measure(View.java:8510)   01-20 09:54:48.407:E / AndroidRuntime(8459):在android.view.ViewRoot.performTraversals(ViewRoot.java:871)   01-20 09:54:48.407:E / AndroidRuntime(8459):在android.view.ViewRoot.handleMessage(ViewRoot.java:1921)   01-20 09:54:48.407:E / AndroidRuntime(8459):在android.os.Handler.dispatchMessage(Handler.java:99)   01-20 09:54:48.407:E / AndroidRuntime(8459):在android.os.Looper.loop(Looper.java:143)   01-20 09:54:48.407:E / AndroidRuntime(8459):在android.app.ActivityThread.main(ActivityThread.java:4196)   01-20 09:54:48.407:E / AndroidRuntime(8459):at java.lang.reflect.Method.invokeNative(Native Method)   01-20 09:54:48.407:E / AndroidRuntime(8459):at java.lang.reflect.Method.invoke(Method.java:507)   01-20 09:54:48.407:E / AndroidRuntime(8459):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)   01-20 09:54:48.407:E / AndroidRuntime(8459):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)   01-20 09:54:48.407:E / AndroidRuntime(8459):at dalvik.system.NativeStart.main(Native Method)

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:2)

但是你怎么能说错误来自你的粘贴代码...... 因为您的类“MySimpleArrayAdapter”不参与错误日志。 我不认为错误来自您粘贴的代码...