- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在实现一个 ExpandableListView 并且对于每个组,它显示子项,每个子项都有自己的持续时间,并且该组显示所有子项持续时间的总和。
但是,当我点击一个 child 时,它会显示错误:
Couldn't read row 1, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
而且我没有调用方法 setOnChildClickListener
。即使我调用它,当我点击 child 时,当我放置断点时它也不会停止。
这就是问题所在,我找不到错误出在哪里,因为堆栈跟踪没有指向我项目的类,而是指向 android native 的类。我已经尝试在我的代码的每个部分放置断点,但是当我点击一个 child 时,没有达到任何断点。
顺便说一句,我使用ViewHolder 模式来处理 View ,适配器是CursorTreeAdapter
这是我的代码 - 完整的堆栈跟踪在最后:
public class MyClass extends ExpandableListActivity
{
private Cursor mContactsCursor;
private ExpandableListView mExpandableListView;
private Integer mContactId;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.contacts_layout);
mExpandableListView.setOnGroupClickListener(new OnGroupClickListener()
{
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id)
{
mContactId = Integer.parseInt(v.getTag(R.id.contact_name).toString());
return false;
}
});
}
@Override
public void onResume()
{
super.onResume();
mContactsCursor = mController.getContacts();
mExpandableListView.setAdapter(new ContactsExpandableListAdapter(mContactsCursor, this));
}
@Override
public void onPause()
{
super.onPause();
if(!mContactsCursor.isClosed())
mContactsCursor.close();
}
@Override
public void onDestroy()
{
super.onDestroy();
if(!mContactsCursor.isClosed())
mContactsCursor.close();
}
public class ContactsExpandableListAdapter extends CursorTreeAdapter
{
private TextView mContactNameTextView, mContactNumberTextView, mDurationTextView;
public ContactsExpandableListAdapter(Cursor cursor, Context context)
{
super(cursor, context);
}
@Override
protected Cursor getChildrenCursor(Cursor groupCursor)
{
return mController.getContactById(mContactId);
}
@Override
protected View newGroupView(Context context, Cursor cursor, boolean isExpanded, ViewGroup viewGroup)
{
View view = LayoutInflater.from(context).inflate(R.layout.listitem_contacts, viewGroup, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.contactName = (TextView) view.findViewById(R.id.contact_name);
viewHolder.duration = (TextView) view.findViewById(R.id.duration);
viewHolder.groupIndicator = (ImageView) view.findViewById(R.id.indicator_icon);
viewHolder.contactName.setText(cursor.getString(cursor.getColumnIndex("contact_name")));
// Set the duration to the view
viewHolder.duration.setText(cursor.getString(cursor.getColumnIndex("duration_sum")));
// Set the tags to the view to be used later when the user click the group view.
// These tags are the contact number, contact id and the the view holder object
// to be retrieved when binding the group view
view.setTag(R.id.contact_number, cursor.getString(cursor.getColumnIndex("contact_number")));
view.setTag(R.id.rlt_main, viewHolder);
view.setTag(R.id.contact_name, cursor.getString(cursor.getColumnIndex("contact_id")));
}
return view;
}
@Override
protected View newChildView(Context context, Cursor cursor, boolean isLastChild, ViewGroup viewGroup)
{
View view = LayoutInflater.from(context).inflate(R.layout.listitem_contacts, viewGroup, false);
mContactNumberTextView = (TextView) view.findViewById(R.id.phone_number);
mContactNumberTextView.setText(cursor.getString(cursor.getColumnIndex("contact_name")));
mDurationTextView = (TextView) view.findViewById(R.id.duration);
mDurationTextView.setText(cursor.getString(cursor.getColumnIndex("duration")));
view.setTag(cursor.getString(cursor.getColumnIndex("contact_number")));
return view;
}
@Override
protected void bindGroupView(View view, Context context, Cursor cursor, boolean isExpanded)
{
ViewHolder viewHolder = (ViewHolder) view.getTag(R.id.rlt_main);
// For each view created, make a query to the database based on the contact id.
// This is not very wise to do, but will probably be changed in the future.
Integer contactId = cursor.getInt(cursor.getColumnIndex("contact_id"));
// Check if the current contact id is equal to -1. If yes it means the contact
Cursor currentContactCursor = mFilterByContactController.getContactById(contactId);
// Check if the cursor being retrieved from the database based on the current view cursor
// is major than zero. If yes, it means it has children, so set the indicator icon to this
// view as visible. If not, set the indicator for this view as gone.
if(currentContactCursor != null && currentContactCursor.getCount() > 0)
{
viewHolder.groupIndicator.setVisibility(View.VISIBLE);
viewHolder.groupIndicator.setImageResource(isExpanded ? R.drawable.expander_ic_maximized : R.drawable.expander_ic_minimized);
}
else
{
viewHolder.groupIndicator.setVisibility(View.GONE);
}
if(cursor.getString(cursor.getColumnIndex("contact_name")) == null)
viewHolder.contactName.setText(cursor.getString(cursor.getColumnIndex("contact_number")));
else
viewHolder.contactName.setText(cursor.getString(cursor.getColumnIndex("contact_name")));
// Set the duration to the view
viewHolder.duration.setText(Utils.convertTime(Integer.parseInt(cursor.getString(cursor.getColumnIndex("duration_sum")))));
// Set the tags to the view to be used later when the user click the group view
view.setTag(R.id.contact_number, cursor.getString(cursor.getColumnIndex("contact_number")));
view.setTag(R.id.contact_name, cursor.getString(cursor.getColumnIndex("contact_id")));
if(!currentContactCursor.isClosed())
currentContactCursor.close();
}
}
@Override
protected void bindChildView(View view, Context context, Cursor cursor, boolean expanded)
{
try
{
if(cursor.getString(cursor.getColumnIndex("contact_name")) == null)
{
mContactNameTextView = (TextView) view.findViewById(R.id.contact_name);
mContactNameTextView.setText(cursor.getString(cursor.getColumnIndex("phone_number")));
}
else
{
mContactNumberTextView = (TextView) view.findViewById(R.id.phone_number);
mContactNumberTextView.setText(cursor.getString(cursor.getColumnIndex("contact_number")));
}
mDurationTextView = (TextView) view.findViewById(R.id.duration);
mDurationTextView.setText(cursor.getString(cursor.getColumnIndex("duration")));
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
if(cursor.isClosed())
cursor.close();
}
}
}
完整堆栈跟踪
07-22 16:47:46.388: E/AndroidRuntime(7718): java.lang.IllegalStateException: Couldn t read row 1, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
07-22 16:47:46.388: E/AndroidRuntime(7718): at android.database.CursorWindow.nativeGetLong(Native Method)
07-22 16:47:46.388: E/AndroidRuntime(7718): at android.database.CursorWindow.getLong(CursorWindow.java:515)
07-22 16:47:46.388: E/AndroidRuntime(7718): at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:75)
07-22 16:47:46.388: E/AndroidRuntime(7718): at android.widget.CursorTreeAdapter$MyCursorHelper.getId(CursorTreeAdapter.java:436)
07-22 16:47:46.388: E/AndroidRuntime(7718): at android.widget.CursorTreeAdapter.getChildId(CursorTreeAdapter.java:173)
07-22 16:47:46.388: E/AndroidRuntime(7718): at android.widget.ExpandableListConnector.getItemId(ExpandableListConnector.java:427)
07-22 16:47:46.388: E/AndroidRuntime(7718): at android.widget.AdapterView.getItemIdAtPosition(AdapterView.java:756)
07-22 16:47:46.388: E/AndroidRuntime(7718): at android.widget.AdapterView.setSelectedPositionInt(AdapterView.java:1128)
07-22 16:47:46.388: E/AndroidRuntime(7718): at android.widget.AbsListView.onTouchEvent(AbsListView.java:3147)
07-22 16:47:46.388: E/AndroidRuntime(7718): at android.view.View.dispatchTouchEvent(View.java:5541)
07-22 16:47:46.388: E/AndroidRuntime(7718): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1951)
07-22 16:47:46.388: E/AndroidRuntime(7718): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1712)
07-22 16:47:46.388: E/AndroidRuntime(7718): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
07-22 16:47:46.388: E/AndroidRuntime(7718): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
07-22 16:47:46.388: E/AndroidRuntime(7718): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
07-22 16:47:46.388: E/AndroidRuntime(7718): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
07-22 16:47:46.388: E/AndroidRuntime(7718): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
07-22 16:47:46.388: E/AndroidRuntime(7718): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
07-22 16:47:46.388: E/AndroidRuntime(7718): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
07-22 16:47:46.388: E/AndroidRuntime(7718): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
07-22 16:47:46.388: E/AndroidRuntime(7718): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1912)
07-22 16:47:46.388: E/AndroidRuntime(7718): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1371)
07-22 16:47:46.388: E/AndroidRuntime(7718): at android.app.Activity.dispatchTouchEvent(Activity.java:2364)
07-22 16:47:46.388: E/AndroidRuntime(7718): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1860)
07-22 16:47:46.388: E/AndroidRuntime(7718): at android.view.View.dispatchPointerEvent(View.java:5721)
07-22 16:47:46.388: E/AndroidRuntime(7718): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:2890)
07-22 16:47:46.388: E/AndroidRuntime(7718): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2466)
07-22 16:47:46.388: E/AndroidRuntime(7718): at android.view.ViewRootImpl.processInputEvents(ViewRootImpl.java:845)
07-22 16:47:46.388: E/AndroidRuntime(7718): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2475)
07-22 16:47:46.388: E/AndroidRuntime(7718): at android.os.Handler.dispatchMessage(Handler.java:99)
07-22 16:47:46.388: E/AndroidRuntime(7718): at android.os.Looper.loop(Looper.java:137) 07-22 16:47:46.388: E/AndroidRuntime(7718): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-22 16:47:46.388: E/AndroidRuntime(7718): at java.lang.reflect.Method.invokeNative(Native Method)
07-22 16:47:46.388: E/AndroidRuntime(7718): at java.lang.reflect.Method.invoke(Method.java:511)
07-22 16:47:46.388: E/AndroidRuntime(7718): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-22 16:47:46.388: E/AndroidRuntime(7718): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-22 16:47:46.388: E/AndroidRuntime(7718): at dalvik.system.NativeStart.main(Native Method)
最佳答案
正如@Jens 在评论中提到的:
Is it looking for a row identifier (i.e. BaseColumns#_ID) and not finding it in the cursor? Which columns are you using in mContactsCursor - you haven't forgotten the row id in that?
在所有查询中,必须指定列 _id
。这就是导致错误的原因,在我将 _id
列添加到查询后,它起作用了。
关于android - 无法从 CursorWindow 读取第 1 行,col -1。在从游标访问数据之前确保正确初始化了游标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11603585/
我如何知道 CursorWindow 上有多少列?为什么它有 getNumRows() 而没有 getNumColumns(),尽管有 setNumColumns()? 最佳答案 我用这种最可怕的方式
我在我的 Android 应用程序中操作 SQLite3 数据库。我刚刚从具有 20 万行和 14 列的预填充数据库中读取。条目是单词。所有列的数据类型都是文本。查询最多 11 个字母的单词(例如 A
我试图用 recyclerview 显示我的 sqlite 数据,但它给了我这个错误: 无法从 CursorWindow 读取第 0 行第 9 列。在访问游标中的数据之前,请确保游标已正确初始化。 C
我正在尝试用我的数据库 SQLite 中的数据填充一个二维数组。但是会发生以下情况: 顺便说一下,这是我第一次使用 SQLite,我试图找出是否有类似“结果集”或“数据表”的东西......我找到了所
我正在开发一个 Android 应用程序,我想在其中实现一个添加新用户的注册功能。 首先,我创建了一个名为 Registration 的 Activity ,当按下按钮时,它会调用一个添加用户的方法:
这个问题在这里已经有了答案: Could not allocate CursorWindow (3 个答案) 关闭 6 年前。 我正在开发一个 android 应用程序,因为我使用 SQLite 数
我必须执行一个查询并将结果存储在一个列表中,我使用的函数如下: List getSpoolInRecords(String label, boolean getLastInserted) {
我正在使用 AsyncStorage在 ReactNative 中在设备上存储一些数据(大尺寸>2MB),然后用下面的代码读取它 try { const value = await Asyn
我有一个应用程序,它将一些数据(9 列)存储到 SQLite 数据库中。我遵循 vogella 中的示例并用其他一些例子对其进行了调整,直到我认为应该没问题。现在,我花了两天时间努力让我的应用程序运行
我在进程之间发送/接收自定义 Query 对象。 Query 实现了 Parcelable 并包装了一个 CursorWindow 实例。首先,我通过 Messenger 将它从进程 A 发送到进程
我遇到了问题。我无法从数据库获取和设置 blob。打开我的应用程序后,我在 Logcat 中收到此错误 Caused by: java.lang.IllegalStateException: Coul
我正在尝试创建一个应用程序来从预先存在的数据库中提取 sqlite 数据,在应用程序运行之前将其复制到目录中。该数据库包含超过 2000 行和大约 20 列。 DBAdapter 类。 package
当我尝试添加到 android 上的 sqllite DB 时,我看到了上述错误。我正在使用 cloudant 库。这是堆栈跟踪: 2019-07-17 18:04:48.292 5522-5753/
我收到一些用户的以下错误。 无法从 CursorWindow 读取第 34 行第 11 列。在访问游标中的数据之前,请确保游标已正确初始化。 此问题发生在以下行中: @Override pub
我尝试使用上下文菜单从 ListView 中删除数据,但出现 java.lang.IllegalStateException: Couldn't read row 0, col 2 from Curs
我已经创建了静态数据库并且这个数据库为图像添加了 base 64 字符串这个图像是大尺寸我运行我的应用程序时数据库出错以获取字符串如何解决它。 我是 Android 编程的新手... 数据库 publ
Log Cat Caused by: java.lang.IllegalStateException: Couldn't read row 0, col 5 from CursorWindow. Ma
我在 sqlite 数据库中插入图像时遇到困难。我的代码中没有语法错误。我运行它后,应用程序自动强制停止。 logcat 显示: 由以下原因引起:java.lang.IllegalStateExcep
我以前使用过 SQLDB,但只保存了 3 项。现在我想添加更多内容,但出现此错误,但无法解决。我不明白我的光标在哪里变坏并且不再工作了。谁能解释一下为什么我现在会收到此错误?非常感谢。 缺席.java
我在执行我的代码时遇到此错误。 Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized c
我是一名优秀的程序员,十分优秀!