我一直在尝试对只读SQLite数据库使用startManagingCursor()
,但是即使使用stopManagingCursor()
,它也会引发很多错误(是的,我知道they're deprecated )。
我想获得一些帮助,以了解从管理游标到更好方法的步骤,而无需进行太多更改,因为我从未使用过它们,并且该代码是稳定的,尽管有时会弹出随机错误,因为的startManagingCursor()
(在单个活动应用程序中,它可以正常工作,但是在多片段应用程序中将其用作框架时,确实会出现问题)。
但是,当我简单地将startManagingCursor()
更改为getLoaderManager()
时,它仍然可以工作,但是the documentation also tells it's deprecated并表示getSupportLoaderManager()
,当我使用它时,它被划掉如果它也被弃用(Lint说“ getSupportLoaderManager()
被弃用”,但对getLoaderManager()
说的不一样);尽管两者都可以运行,但它们可能会产生java.lang.NullPointerException
不会发生的情况startManagingCursor(cursor)
。
那我该怎么办?
SQLite片段是这样的:
public class SQLiteDatabaseActivity extends Fragment {
public static SQLiteDatabaseActivity newInstance() {
return new SQLiteDatabaseActivity();
}
ExpandableListView expandableListView;
Database mDatabase;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.sqlite_database_activity, container, false);
mDatabase = new Database(getActivity());
mDatabase.open();
Cursor cursor = mDatabase.getDatabase();
getActivity().startManagingCursor(cursor);
String[] groupFrom = {
Database.DATABASE_GROUP_1,
Database.DATABASE_GROUP_2,
Database.DATABASE_GROUP_3,
Database.DATABASE_GROUP_4
};
int[] groupTo = {
R.id.group_number,
R.id.group_name,
R.id.group_path,
R.id.group_company
};
String[] childFrom = new String[]{
Database.DATABASE_CHILD_1,
Database.DATABASE_CHILD_2,
Database.DATABASE_CHILD_3,
Database.DATABASE_CHILD_4,
Database.DATABASE_CHILD_5,
Database.DATABASE_CHILD_6,
Database.DATABASE_CHILD_7,
Database.DATABASE_CHILD_8,
Database.DATABASE_CHILD_9,
Database.DATABASE_CHILD_10,
Database.DATABASE_CHILD_11,
Database.DATABASE_CHILD_12
};
int[] childTo = {
R.id.child1,
R.id.child2,
R.id.child_saturday_origin,
R.id.child_saturday_destiny,
R.id.child_sunday_origin,
R.id.child_sunday_destiny,
R.id.child_origin,
R.id.child_destiny,
R.id.child_saturday_origin_origin,
R.id.child_saturday_destiny_destiny,
R.id.child_sunday_origin_origin,
R.id.child_sunday_destiny_destiny
};
SimpleCursorTreeAdapter simplecursortreeAdapter = new ExpandableListViewAdapter(
getActivity(),
cursor,
R.layout.sqlite_database_list_group,
groupFrom,
groupTo,
R.layout.sqlite_database_list_child,
childFrom,
childTo
);
expandableListView = view.findViewById(R.id.expandableListview);
expandableListView.setAdapter(simplecursortreeAdapter);
getActivity().stopManagingCursor(cursor);
return view;
}
private class ExpandableListViewAdapter extends SimpleCursorTreeAdapter {
private ExpandableListViewAdapter(
Context context,
Cursor cursor,
int groupLayout,
String[] groupFrom,
int[] groupTo,
int childLayout,
String[] childFrom,
int[] childTo) {
super(context, cursor, groupLayout, groupFrom, groupTo, childLayout, childFrom, childTo);
}
protected Cursor getChildrenCursor(Cursor groupCursor) {
return mDatabase.getID(groupCursor.getInt(groupCursor.getColumnIndex(Database.DATABASE_ID)));
}
}
public void onDestroy() {
super.onDestroy();
mDatabase.close();
}
}
数据库在这里:
public class Database {
private static final int DATABASE_VERSION = 3;
private static final String DATABASE_NAME = "BusScheduleDatabase.db";
private static final String DATABASE_TABLE = "BusSchedule";
public static final String DATABASE_ID = "_id";
public static final String DATABASE_GROUP_1 = "Linha";
public static final String DATABASE_GROUP_2 = "Nome";
public static final String DATABASE_GROUP_3 = "Caminho";
public static final String DATABASE_GROUP_4 = "Empresa";
public static final String DATABASE_CHILD_1 = "SemanaIda";
public static final String DATABASE_CHILD_2 = "SemanaVolta";
public static final String DATABASE_CHILD_3 = "SabadoIda";
public static final String DATABASE_CHILD_4 = "SabadoVolta";
public static final String DATABASE_CHILD_5 = "DomingoIda";
public static final String DATABASE_CHILD_6 = "DomingoVolta";
public static final String DATABASE_CHILD_7 = "Origem";
public static final String DATABASE_CHILD_8 = "Destino";
public static final String DATABASE_CHILD_9 = "Origem";
public static final String DATABASE_CHILD_10 = "Destino";
public static final String DATABASE_CHILD_11 = "Origem";
public static final String DATABASE_CHILD_12 = "Destino";
private final Context mContext;
private DatabaseHelper mDatabaseHelper;
private SQLiteDatabase mDB;
public Database(Context context) {
mContext = context;
}
public void open() {
mDatabaseHelper = new DatabaseHelper(mContext, DATABASE_NAME, null, DATABASE_VERSION);
mDB = mDatabaseHelper.getReadableDatabase();
}
public void close() {
if (mDatabaseHelper != null) mDatabaseHelper.close();
}
public Cursor getDatabase() {
return mDB.query(DATABASE_TABLE, null, null, null, null, null, DATABASE_GROUP_1);
}
public Cursor getID(long rowID) {
return mDB.query(DATABASE_TABLE, null, "_id" + " = "
+ rowID, null, null, null, null);
}
public class DatabaseHelper extends SQLiteAssetHelper {
public DatabaseHelper(Context mContext, String DATABASE_NAME, SQLiteDatabase.CursorFactory factory, int DATABASE_VERSION) {
super(mContext, DATABASE_NAME, factory, DATABASE_VERSION);
super.setForcedUpgrade();
}
}
}
重复错误基本上是以下内容:
java.lang.IllegalStateException:仅当游标有效时才应调用
非常感谢您的帮助!