错误:使用 CursorAdapter 从列表的 onItemClick 启动活动
我正在尝试构建一个联系人应用程序
在主要活动中我显示所有联系人的列表。
但是,当我尝试在某个项目之后启动一项活动时 单击列表后,出现错误:
致命异常:main 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): 致命异常:main 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.measureChildHorizontal(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):在java.lang.reflect.Method.invokeNative(本机方法) 01-20 09:54:48.407: E/AndroidRuntime(8459): 在 java.lang.reflect.Method.invoke(Method.java:507) 01-20 09:54:48.407: E/AndroidRuntime(8459): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 01-20 09:54:48.407: E/AndroidRuntime(8459): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 01-20 09:54:48.407: E/AndroidRuntime(8459): 在dalvik.system.NativeStart.main(本机方法)
请帮助我解决这个问题。
I'm trying to build a contact application
In the main activity i Show a list of all the contacts.
However, when I try to launch an activity after an item from
the list has been clicked, I get Error:
FATAL EXCEPTION: main 01-20 09:54:48.407: E/AndroidRuntime(8459):
java.lang.IllegalStateException: this should only be called when the
cursor is valid
It seems that somehow the adapter methods (getView, getCount) keep calling
My Code is as follows,
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();
}
}
Here's the log:
01-20 09:54:48.407: E/AndroidRuntime(8459): FATAL EXCEPTION: main
01-20 09:54:48.407: E/AndroidRuntime(8459): java.lang.IllegalStateException: this should only be called when the cursor is valid
01-20 09:54:48.407: E/AndroidRuntime(8459): at android.widget.CursorAdapter.getView(CursorAdapter.java:175)
01-20 09:54:48.407: E/AndroidRuntime(8459): at android.widget.AbsListView.obtainView(AbsListView.java:1409)
01-20 09:54:48.407: E/AndroidRuntime(8459): at android.widget.ListView.measureHeightOfChildren(ListView.java:1307)
01-20 09:54:48.407: E/AndroidRuntime(8459): at android.widget.ListView.onMeasure(ListView.java:1127)
01-20 09:54:48.407: E/AndroidRuntime(8459): at android.view.View.measure(View.java:8510)
01-20 09:54:48.407: E/AndroidRuntime(8459): at android.widget.RelativeLayout.measureChild(RelativeLayout.java:566)
01-20 09:54:48.407: E/AndroidRuntime(8459): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:381)
01-20 09:54:48.407: E/AndroidRuntime(8459): at android.view.View.measure(View.java:8510)
01-20 09:54:48.407: E/AndroidRuntime(8459): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:581)
01-20 09:54:48.407: E/AndroidRuntime(8459): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:365)
01-20 09:54:48.407: E/AndroidRuntime(8459): at android.view.View.measure(View.java:8510)
01-20 09:54:48.407: E/AndroidRuntime(8459): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3202)
01-20 09:54:48.407: E/AndroidRuntime(8459): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
01-20 09:54:48.407: E/AndroidRuntime(8459): at android.view.View.measure(View.java:8510)
01-20 09:54:48.407: E/AndroidRuntime(8459): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3202)
01-20 09:54:48.407: E/AndroidRuntime(8459): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
01-20 09:54:48.407: E/AndroidRuntime(8459): at android.view.View.measure(View.java:8510)
01-20 09:54:48.407: E/AndroidRuntime(8459): at android.view.ViewRoot.performTraversals(ViewRoot.java:871)
01-20 09:54:48.407: E/AndroidRuntime(8459): at android.view.ViewRoot.handleMessage(ViewRoot.java:1921)
01-20 09:54:48.407: E/AndroidRuntime(8459): at android.os.Handler.dispatchMessage(Handler.java:99)
01-20 09:54:48.407: E/AndroidRuntime(8459): at android.os.Looper.loop(Looper.java:143)
01-20 09:54:48.407: E/AndroidRuntime(8459): at 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)
Please help me regarding this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
但是你怎么能说错误来自你粘贴的代码......
因为您的类“MySimpleArrayAdapter”不涉及错误日志。
我不认为错误来自您粘贴的代码......
But how can you say that error is coming from your pasted code...
as your class "MySimpleArrayAdapter" is not involved in error logs.
I do not think error is coming from your pasted code...