多CursorLoader
我扩展了之前的问题,解决了该问题,但出现了一个新的类似错误。
我有以下活动
public class ActivityMatchesList extends FragmentActivity implements LoaderManager.LoaderCallbacks<Cursor> {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView lvLive = (ListView)findViewById(R.id.matchListList);
lvLive.setOnItemClickListener(OnItemClickListenerLive);
getSupportLoaderManager().initLoader(1, null, this);
getSupportLoaderManager().initLoader(2, null, this);
getSupportLoaderManager().initLoader(3, null, this);
}
private OnItemClickListener OnItemClickListenerLive = new OnItemClickListener(){
Intent i = new Intent(ActivityMatchesList.this, ActivityEvents.class);
finish();
startActivity(i);
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) { //FIXME: add switch for 3 loader
switch (id){
case 1:
CursorLoader cursorLoaderLive = new CursorLoader(this, uri1,null,null,null,null);
return cursorLoaderLive;
case 2:
CursorLoader cursorLoaderRankingLeague = new CursorLoader(this, uri1,null,null,null,null);
return cursorLoaderRankingLeague;
case 3:
CursorLoader cursorLoaderRankingLive = new CursorLoader(this, uri1,null,null,null,null);
return cursorLoaderRankingLive;
default:
return null;
}
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
switch (loader.getId()){
case 1:
Adapter1.swapCursor(cursor);
break;
case 2:
Adapter2.swapCursor(cursor);
break;
case 3:
Adapter3.swapCursor(cursor);
break;
}
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
switch (loader.getId()){
case 1:
Adapter1.swapCursor(null);
break;
case 2:
Adapter2.swapCursor(null);
break;
case 3:
Adapter3.swapCursor(null);
break;
}
}
}
使用 OnItemClickListener 我完成()此活动并开始一个新的活动,当我完成新的活动以返回到此时,有时我会出现以下错误
11-30 09:35:01.839: INFO/dalvikvm(321): Uncaught exception thrown by finalizer (will be discarded):
11-30 09:35:01.848: INFO/dalvikvm(321): Ljava/lang/IllegalStateException;: Finalizing cursor android.database.sqlite.SQLiteCursor@43c6ab00 on MAIN_TABLE that has not been deactivated or closed
11-30 09:35:01.848: INFO/dalvikvm(321): at android.database.sqlite.SQLiteCursor.finalize(SQLiteCursor.java:596)
11-30 09:35:01.848: INFO/dalvikvm(321): at dalvik.system.NativeStart.run(Native Method)
,我无法发现原因,有人可以帮助我吗?
编辑:如果我只使用 1 个加载程序,问题仍然存在。似乎随着活动完成,加载程序有时不会关闭光标,为什么?
I extend a my previous question, I resolve that issue but I have a new similar error.
I have the following activity
public class ActivityMatchesList extends FragmentActivity implements LoaderManager.LoaderCallbacks<Cursor> {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView lvLive = (ListView)findViewById(R.id.matchListList);
lvLive.setOnItemClickListener(OnItemClickListenerLive);
getSupportLoaderManager().initLoader(1, null, this);
getSupportLoaderManager().initLoader(2, null, this);
getSupportLoaderManager().initLoader(3, null, this);
}
private OnItemClickListener OnItemClickListenerLive = new OnItemClickListener(){
Intent i = new Intent(ActivityMatchesList.this, ActivityEvents.class);
finish();
startActivity(i);
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) { //FIXME: add switch for 3 loader
switch (id){
case 1:
CursorLoader cursorLoaderLive = new CursorLoader(this, uri1,null,null,null,null);
return cursorLoaderLive;
case 2:
CursorLoader cursorLoaderRankingLeague = new CursorLoader(this, uri1,null,null,null,null);
return cursorLoaderRankingLeague;
case 3:
CursorLoader cursorLoaderRankingLive = new CursorLoader(this, uri1,null,null,null,null);
return cursorLoaderRankingLive;
default:
return null;
}
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
switch (loader.getId()){
case 1:
Adapter1.swapCursor(cursor);
break;
case 2:
Adapter2.swapCursor(cursor);
break;
case 3:
Adapter3.swapCursor(cursor);
break;
}
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
switch (loader.getId()){
case 1:
Adapter1.swapCursor(null);
break;
case 2:
Adapter2.swapCursor(null);
break;
case 3:
Adapter3.swapCursor(null);
break;
}
}
}
With OnItemClickListener I finish() this activity and start a new, when I finish the new to return to this sometimes I have the following error
11-30 09:35:01.839: INFO/dalvikvm(321): Uncaught exception thrown by finalizer (will be discarded):
11-30 09:35:01.848: INFO/dalvikvm(321): Ljava/lang/IllegalStateException;: Finalizing cursor android.database.sqlite.SQLiteCursor@43c6ab00 on MAIN_TABLE that has not been deactivated or closed
11-30 09:35:01.848: INFO/dalvikvm(321): at android.database.sqlite.SQLiteCursor.finalize(SQLiteCursor.java:596)
11-30 09:35:01.848: INFO/dalvikvm(321): at dalvik.system.NativeStart.run(Native Method)
I am not able to discover the reason, someone can help me?
edit: the problem persist also if I use only 1 loader. It seems that with the activity finish the loader sometimes gon't close cursor, why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么在开始新活动之前在此活动中调用
finish()
? Android 处理 Activity 生命周期。如果是因为您不想在下一个活动完成时返回此活动,请考虑使用 noHistory。您发布的错误意味着有一个应该关闭的打开游标。如果您在完成新活动时看到错误,这似乎表明错误是在新活动中,而不是在本活动中。您可能需要调用 Cursor.close() 在完成新活动之前(可能在
onPause()
或onDestroy()
中)。我对您上一个问题的回答可能也有一定用处:https://stackoverflow.com/a/8857179/399105
Why are you calling
finish()
in this activity before starting the new one? Android handles the activity life cycle. If it's because you don't want to return to this activity when the next activity finishes, consider noHistory instead.The error you posted means there's an open cursor that should have been closed. If you're seeing the error when you finish the new activity, it seems to indicate to me that the error is in the new activity, not this one. You may need to call Cursor.close() before finishing the new activity (perhaps in
onPause()
oronDestroy()
).My answer to your previous question may also be of some use: https://stackoverflow.com/a/8857179/399105