SQLite データベースからデータを選択しようとしていますが、問題があります。まず、assets フォルダーから data/data/PACKAGE_NAME/databases にデータベースをロードします。正常にロードされ、データベースが正しくロードされます。しかし、カーソルでデータを読み取ろうとすると、アプリケーションが停止します(これが私のコードです。私の間違いを指摘してください。ありがとう。
public class SQLiteHelper extends SQLiteAssetHelper {
public static final String DB_NAME = "cards";
private static final int DATABASE_VERSION = 1;
public static final String TABLE_CARDS = "cards";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_CARD_NAME = "card_name";
public static final String COLUMN_TYPE = "type";
public static final String COLUMN_RARITY = "rarity";
public static final String COLUMN_CARD_CLASS = "card_class";
public static final String COLUMN_SET = "card_set";
public static final String COLUMN_RACE = "race";
public static final String COLUMN_SKILLS = "skills";
public static final String COLUMN_MANA = "mana";
public static final String COLUMN_ATTACK = "attack";
public static final String COLUMN_HEALTH = "health";
public static final String COLUMN_DAMAGE = "damage";
public static final String COLUMN_DURABILITY = "durability";
public static final String COLUMN_CRAFT = "craft";
public static final String COLUMN_DUST = "dust";
public static final String COLUMN_ARTIST = "artist";
public static final String COLUMN_EXTRA_INFO = "extra_info";
public static final String COLUMN_CARD_SRC = "card_src";
public SQLiteHelper(Context context) {
super(context, DB_NAME, null, DATABASE_VERSION);
}
public List<FullCard> getCards() {
SQLiteDatabase db = getReadableDatabase();
//I want to record all selected data to this list
List<FullCard> cards = new ArrayList<FullCard>();
Cursor cursor = db.query(SQLiteHelper.TABLE_CARDS,
null, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
FullCard card = cursorToCard(cursor);
cards.add(card);
cursor.moveToNext();
}
cursor.close();
return cards;
}
private FullCard cursorToCard(Cursor cursor) {
FullCard card = new FullCard();
card.setId(cursor.getInt(0));
card.setCardName(cursor.getString(1));
card.setType(cursor.getString(2));
card.setRarity(cursor.getString(3));
card.setCardClass(cursor.getString(4));
card.setSet(cursor.getString(5));
card.setRace(cursor.getString(6));
card.setSkills(cursor.getString(7));
card.setMana(cursor.getString(8));
card.setAttack(cursor.getString(9));
card.setHealth(cursor.getString(10));
card.setDamage(cursor.getString(11));
card.setDurability(cursor.getString(12));
card.setCraft(cursor.getString(13));
card.setDust(cursor.getString(14));
card.setArtist(cursor.getString(15));
card.setExtraInfo(cursor.getString(16));
card.setCard_src(cursor.getString(17));
return card;
}
}
メソッド getCards() を呼び出すと、アプリがクラッシュするクラスのフラグメント。
public class ActivityDB extends ListActivity implements View.OnClickListener{
private SQLiteHelper datasource;
private static Parcelable state;
private static ListView lv;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_db);
Button button_filters = (Button)findViewById(R.id.button_filters);
Button button_menu = (Button)findViewById(R.id.button_menu);
button_filters.setOnClickListener(this);
button_menu.setOnClickListener(this);
ListItem listItem;
ArrayList<ListItem> listItems = new ArrayList<ListItem>();
datasource = new SQLiteHelper(this);
List<FullCard> dbCardList = datasource.getCards(); // Here is that method
for (FullCard aDbCardList : dbCardList) {
listItem = new ListItem();
listItem.setTitle(aDbCardList.getCardName());
listItem.setPar1(aDbCardList.getMana());
listItem.setPar2(aDbCardList.getAttack());
listItem.setPar3(aDbCardList.getHealth());
listItems.add(listItem);
}
setListAdapter(new ListItemAdapter(this,listItems));
}
logcat エラーは次のとおりです。
09-29 01:54:11.989 I/SQLiteAssetHelper(15846): データベース カードを正常にオープンしました29 01:54:12.069 E/AndroidRuntime(15846): FATAL EXCEPTION: main 09-29 01:54:12.069 E/AndroidRuntime(15846): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.FHS/com. FHS.ActivityDB}: android.database.sqlite.SQLiteException: 不明なエラー: BLOB を文字列 09-29 01:54:12.069 に変換できません E/AndroidRuntime(15846): android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 1651) 09-29 01:54:12.069 E/AndroidRuntime(15846): android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 09-29 01:54:12.069 E/AndroidRuntime(15846): android. app.ActivityThread.access$1500(ActivityThread.java:117) 09-29 01:54:12.069 E/AndroidRuntime(15846): android.app.ActivityThread$H.handleMessage(ActivityThread.java:935) 09-29 01:54:12.069 E/AndroidRuntime(15846): at android.os.Handler.dispatchMessage(Handler.java:99) 09-29 01:54:12.069 E/AndroidRuntime(15846): android.os.Looper.loop(Looper.java:130) 09-29 01:54 :12.069 E/AndroidRuntime(15846): android.app.ActivityThread.main(ActivityThread.java:3687) 09-29 01:54:12.069 E/AndroidRuntime(15846): java.lang.reflect.Method.invokeNative(ネイティブ メソッド) 09-29 01:54:12.069 E/AndroidRuntime(15846): java.lang.reflect.Method.invoke(Method.java:507) 09-29 01:54:12.069 E/AndroidRuntime(15846): com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 09-29 01:54:12.069 E/AndroidRuntime(15846): com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 09-29 01:54:12.069 E/AndroidRuntime(15846): dalvik.system.NativeStart.main(ネイティブ メソッド) 09-29 01:54:12.069 E/AndroidRuntime(15846):原因: android.database.sqlite.SQLiteException: 不明なエラー: BLOB を文字列 09-29 01:54:12.069 に変換できません E/AndroidRuntime(15846): android.database.CursorWindow.getString_native(Native Method) 09-29 で01:54:12.069 E/AndroidRuntime(15846): android.database.CursorWindow.getString(CursorWindow.java:361) 09-29 01:54:12.069 E/AndroidRuntime(15846): android.database.AbstractWindowedCursor.getString で(AbstractWindowedCursor.java:49) 09-29 01:54:12.069 E/AndroidRuntime(15846): com.FHS.SQLiteHelper.cursorToCard(SQLiteHelper.java:84) 09-29 01:54:12.069 E/AndroidRuntime(15846) ): com.FHS.SQLiteHelper.getCards(SQLiteHelper.java:73) 09-29 01:54:12 で。069 E/AndroidRuntime(15846): com.FHS.ActivityDB.onCreate(ActivityDB.java:65) 09-29 01:54:12.069 E/AndroidRuntime(15846): android.app.Instrumentation.callActivityOnCreate(Instrumentation.java) :1047) 09-29 01:54:12.069 E/AndroidRuntime(15846): android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615) 09-29 01:54:12.069 E/AndroidRuntime(15846): .. . 11 more 09-29 01:54:23.589 W/InputManagerService(207): pid 15846 uid 10062 に setActive(false) 通知を送信する RemoteException を取得しました1615) 09-29 01:54:12.069 E/AndroidRuntime(15846): ... 11 詳細 09-29 01:54:23.589 W/InputManagerService(207): pid 15846 uid 10062 に setActive(false) 通知を送信する RemoteException を取得しました1615) 09-29 01:54:12.069 E/AndroidRuntime(15846): ... 11 詳細 09-29 01:54:23.589 W/InputManagerService(207): pid 15846 uid 10062 に setActive(false) 通知を送信する RemoteException を取得しました