私はDatabaseHandler
このようなクラスを持っています:
public class DatabaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "CaseDatabase";
private static final String TABLE_CASES = "cases";
private static final String KEY_ID = "id";
private static final String DATE = "date";
private static final String RIGHTFINGER = "rightFinger";
private static final String LEFTFINGER = "leftFinger";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CASES + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + DATE + " TEXT,"
+ RIGHTFINGER + " BLOB," + LEFTFINGER +" BLOB" + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CASES);
// Create tables again
onCreate(db);
}
public void addContact(Case c) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_ID, c.getCaseNumber()); // PCN
values.put(DATE, c.getDate()); // Date
ByteArrayOutputStream out1 = new ByteArrayOutputStream();
c.getLeftIndexFinger().compress(Bitmap.CompressFormat.PNG, 100, out1);
ByteArrayOutputStream out2 = new ByteArrayOutputStream();
c.getRightIndexFinger().compress(Bitmap.CompressFormat.PNG, 100, out2);
values.put(LEFTFINGER, out1.toByteArray()); // LFINGER
values.put("RIGHTFINGER", out2.toByteArray()); //RFINGER
// Inserting Row
db.insert(TABLE_CASES, null, values);
db.close(); // Closing database connection
}
public ArrayList<Case> getAllContacts() {
ArrayList<Case> caseList = new ArrayList<Case>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_CASES;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Case c = new Case();
c.setCaseNumber(cursor.getString(0));
c.setDate(cursor.getString(1));
byte[] blob1 = cursor.getBlob(cursor.getColumnIndexOrThrow("2"));
Bitmap bmp1 = BitmapFactory.decodeByteArray(blob1, 0,blob1.length);
c.setLeftIndexFinger(bmp1);
byte[] blob2 = cursor.getBlob(cursor.getColumnIndexOrThrow("3"));
Bitmap bmp2 = BitmapFactory.decodeByteArray(blob1, 0,blob2.length);
c.setRightIndexFinger(bmp2);
// Adding contact to list
caseList.add(c);
} while (cursor.moveToNext());
}
// return contact list
return caseList;
}
}
メソッドは完全に機能しますが、メソッドadd
を呼び出そうとすると、次のようになります。getAllContacts
public ArrayList<Case> getFromDatabase() {
ArrayList<Case> c = db.getAllContacts();
return c;
}
list = getFromDatabase();
アプリがクラッシュし、LogCat が次のように出力します。
07-30 12:07:56.570: E/AndroidRuntime(13040): FATAL EXCEPTION: main
add
データベースからレコードを取得しようとするメソッドは、メソッドとは別のアクティビティにあります
これは私のgetAllContacts
方法です:
public ArrayList<Case> getAllContacts() {
ArrayList<Case> caseList = new ArrayList<Case>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_CASES;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Case c = new Case();
c.setCaseNumber(cursor.getString(0));
c.setDate(cursor.getString(1));
byte[] blob1 = cursor.getBlob(cursor.getColumnIndexOrThrow("2"));
Bitmap bmp1 = BitmapFactory.decodeByteArray(blob1, 0,blob1.length);
c.setLeftIndexFinger(bmp1);
byte[] blob2 = cursor.getBlob(cursor.getColumnIndexOrThrow("3"));
Bitmap bmp2 = BitmapFactory.decodeByteArray(blob1, 0,blob2.length);
c.setRightIndexFinger(bmp2);
// Adding contact to list
caseList.add(c);
} while (cursor.moveToNext());
}
// return contact list
return caseList;
}
LogCat 情報:
E/AndroidRuntime(15058): FATAL EXCEPTION: main
E/AndroidRuntime(15058): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.package.hello/com.package.hello.History}: java.lang.NullPointerException
E/AndroidRuntime(15058): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
E/AndroidRuntime(15058): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
E/AndroidRuntime(15058): at android.app.ActivityThread.access$600(ActivityThread.java:128)
E/AndroidRuntime(15058): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
E/AndroidRuntime(15058): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(15058): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(15058): at android.app.ActivityThread.main(ActivityThread.java:4517)
E/AndroidRuntime(15058): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(15058): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(15058): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
E/AndroidRuntime(15058): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
E/AndroidRuntime(15058): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(15058): Caused by: java.lang.NullPointerException
E/AndroidRuntime(15058): at com.package.hello.History.getFromDatabase(History.java:153)
E/AndroidRuntime(15058): at com.package.hello.History.onCreate(History.java:49)
E/AndroidRuntime(15058): at android.app.Activity.performCreate(Activity.java:4533)
E/AndroidRuntime(15058): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
E/AndroidRuntime(15058): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
History.java の 153 行目は次のとおりです。
ArrayList<Case> c = db.getAllContacts();
history クラスでは、このクラスはメソッドがdb
あるクラスから継承しているため、オブジェクトは宣言されていません。add
編集3
私の歴史の授業
public class History extends SuperClass {
ListView lv;
ArrayList<Case> list;
ArrayList<Case> tempCaseList;
TextView header;
EditText ed;
String[] tempList;
int textLength;
TextView error;
Boolean[] isPresent;
DatabaseHandler db;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setTabBar(R.layout.history);
lv = (ListView)findViewById(R.id.listOfCases);
list = new ArrayList<Case>();
list = getFromDatabase();
lv.setAdapter(new CustomAdapter(this, list));
lv.setTextFilterEnabled(true);
header = (TextView)findViewById(R.id.textView1);
header.setText("History");
error = (TextView)findViewById(R.id.error);
// db = new DatabaseHandler(this);
tempList = getCaseNumberToTempList(list);
tempCaseList = createTempList(list);
ed = (EditText)findViewById(R.id.editText1);
ed.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
textLength = ed.getText().length();
list.clear();
isPresent = initIsPresentArray();
for (int i = 0; i < tempList.length; i++) {
for (int j = 0; j < (tempList[i].length()-(textLength-1)); j++) {
if (textLength <= tempList[i].length()) {
if(isPresent[i] == false){
if (ed.getText().toString().equalsIgnoreCase((String) tempList[i].subSequence(j,(j+textLength)))) {
list.add(tempCaseList.get(i));
isPresent[i] = true;
}
}
}
}
if(list.size() == 0) {
error.setText("Not Found");
}
else {
error.setText("");
}
lv.setAdapter(new CustomAdapter(History.this, list));
}
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
}
public Boolean[] initIsPresentArray() {
Boolean[] isPresent = new Boolean[tempCaseList.size()];
for(int i = 0; i < isPresent.length; i++) {
isPresent[i] = false;
}
return isPresent;
}
public ArrayList<Case> createTempList(ArrayList<Case> listOfCases) {
ArrayList<Case> temporaryList = new ArrayList<Case>();
for(Case c : listOfCases) {
temporaryList.add(c);
}
return temporaryList;
}
public String[] getCaseNumberToTempList(ArrayList<Case> caseList) {
String[] objectCaseNumber = new String[caseList.size()];
for(int i = 0; i < caseList.size(); i++) {
objectCaseNumber[i] = caseList.get(i).getCaseNumber();
}
return objectCaseNumber;
}
public ArrayList<Case> getFromDatabase() {
ArrayList<Case> c = db.getAllContacts();
return c;
}
}
最終編集
エラーが見つかりました..初期化を忘れました。db
このオブジェクトを使用するメソッドを呼び出す前のオブジェクト。しかし、このスレッドのトピックではない別のエラーが発生したため、再び行き詰まった場合は新しいスレッドを投稿します。ありがとう。