gpt4 book ai didi

android - IllegalStateException - 使用 AutocompleteTextView 支持 LoaderManager

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:51:39 27 4
gpt4 key购买 nike

我想到使用 CursorLoaders 和 Loadermanagers 的好处之一是您不需要手动管理游标的生命周期。因此,我使用 loadermanager 将适配器绑定(bind)到使用支持包的 AutoCompleteTextView。

除了它随机抛出一个错误“IllegalStateException - 试图重新打开一个已经关闭的对象”之外,它工作得很好。如果我们使用加载程序管理器,那肯定不会发生吗?

代码如下:

package com.bhagwad.tennis;

import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.SimpleCursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter.CursorToStringConverter;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.Button;

import com.bhagwad.tennis.TennisSchedule.TennisScheduleColumns;


public class WidgetConfiguration extends FragmentActivity implements OnClickListener, LoaderCallbacks<Cursor> {

Button mSaveWidget;
AutoCompleteTextView mPlayerName;
int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
String mSelection ="";
SimpleCursorAdapter mAdapter;

public static String PREFS = "com.bhagwad.tennis.appwidget";
public static final String PREFS_PREFIX_KEY = "prefix_";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.widget_configuration);

mPlayerName = (AutoCompleteTextView) findViewById(R.id.edit_filter);

mPlayerName.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

if (!s.equals(""))
mSelection = s.toString();
else
mSelection = "";

getSupportLoaderManager().restartLoader(0, null, WidgetConfiguration.this);



}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub

}

@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub

}
});

// Set up the adapter

mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, null, new String[] {TennisScheduleColumns.PLAYER_NAME}, new int[] {android.R.id.text1}, 0);
mAdapter.setCursorToStringConverter(new CursorToStringConverter() {

@Override
public CharSequence convertToString(Cursor c) {

return c.getString(c.getColumnIndexOrThrow(TennisScheduleColumns.PLAYER_NAME));

}
});

mPlayerName.setAdapter(mAdapter);

getSupportLoaderManager().initLoader(0, null, this);

}


@Override
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {

return new CursorLoader(this, TennisScheduleColumns.CONTENT_URI_PLAYERS, new String[] {TennisScheduleColumns._ID, TennisScheduleColumns.PLAYER_NAME}, TennisScheduleColumns.PLAYER_NAME + " LIKE ?", new String[] {"%"+mSelection+"%"}, null);

}


@Override
public void onLoaderReset(Loader<Cursor> arg0) {
mAdapter.swapCursor(null);

}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mAdapter.swapCursor(data);


}


}

这是错误堆栈:

08-16 22:21:23.244: E/AndroidRuntime(25475): java.lang.IllegalStateException: attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery (mSql = SELECT _id, player_name FROM players WHERE (player_name LIKE ?)) 
08-16 22:21:23.244: E/AndroidRuntime(25475): at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:33)
08-16 22:21:23.244: E/AndroidRuntime(25475): at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:82)
08-16 22:21:23.244: E/AndroidRuntime(25475): at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:164)
08-16 22:21:23.244: E/AndroidRuntime(25475): at android.database.sqlite.SQLiteCursor.onMove(SQLiteCursor.java:147)
08-16 22:21:23.244: E/AndroidRuntime(25475): at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:178)
08-16 22:21:23.244: E/AndroidRuntime(25475): at android.database.CursorWrapper.moveToPosition(CursorWrapper.java:162)
08-16 22:21:23.244: E/AndroidRuntime(25475): at android.support.v4.widget.CursorAdapter.getItem(CursorAdapter.java:213)
08-16 22:21:23.244: E/AndroidRuntime(25475): at android.widget.AutoCompleteTextView.buildImeCompletions(AutoCompleteTextView.java:1113)
08-16 22:21:23.244: E/AndroidRuntime(25475): at android.widget.AutoCompleteTextView.showDropDown(AutoCompleteTextView.java:1072)
08-16 22:21:23.244: E/AndroidRuntime(25475): at android.widget.AutoCompleteTextView.updateDropDownForFilter(AutoCompleteTextView.java:950)
08-16 22:21:23.244: E/AndroidRuntime(25475): at android.widget.AutoCompleteTextView.onFilterComplete(AutoCompleteTextView.java:932)
08-16 22:21:23.244: E/AndroidRuntime(25475): at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:285)
08-16 22:21:23.244: E/AndroidRuntime(25475): at android.os.Handler.dispatchMessage(Handler.java:99)
08-16 22:21:23.244: E/AndroidRuntime(25475): at android.os.Looper.loop(Looper.java:137)
08-16 22:21:23.244: E/AndroidRuntime(25475): at android.app.ActivityThread.main(ActivityThread.java:4507)

有什么可能出错的想法吗?

最佳答案

OnLoadFinished 似乎有时会在游标失效时被调用 - 如果您对游标进行 isClosed() 测试并通过,您会发现它在一次(大量)尝试中关闭。

不幸的是,放置在 OnLoadFinished 中的“标准”代码会立即在适配器上执行 changeCursor(),接下来是困惑、堆栈转储、瘟疫等。

我的解决方案并不比您的 try/catch 更漂亮。忽略伪造的 OnLoadFinished 并冒着最终用户获得空白 UI 的风险。

关于android - IllegalStateException - 使用 AutocompleteTextView 支持 LoaderManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11999098/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com