2
ユーザー入力に基づいてデータベースを照会しようとしていますが、この例外が発生します。Android:データベースを照会するときにキャッチされないハンドラの例外
public ArrayList<ArrayList<String>> getAnswer(int questionId, String startDate, String endDate)
{
String query = "SELECT " + HealthyDroidQuizHelper.COLUMN_ANSWER + ", "
+ HealthyDroidQuizHelper.COLUMN_DATETIME + " FROM " + HealthyDroidQuizHelper.TABLE_ASSOCIATION
+ " WHERE " + HealthyDroidQuizHelper.COLUMN_QUESTION_ID + " = " + questionId + " AND "
+ HealthyDroidQuizHelper.COLUMN_DATETIME + " BETWEEN " + startDate + " AND " + endDate;
ArrayList<ArrayList<String>> totalResults = new ArrayList<ArrayList<String>>();
ArrayList<String> QuizResults = new ArrayList<String>();
String ans, currDate, oldDate = null;
Cursor cursor = database.rawQuery(query, null);
cursor.moveToFirst();
oldDate = cursor.getString(cursor.getColumnIndex(HealthyDroidQuizHelper.COLUMN_DATETIME));
QuizResults.add(oldDate);
while(!cursor.isAfterLast())
{
currDate = cursor.getString(cursor.getColumnIndex(HealthyDroidQuizHelper.COLUMN_DATETIME));
if(!oldDate.equalsIgnoreCase(currDate))
{
QuizResults.add(currDate);
oldDate = currDate;
}
ans = cursor.getString(cursor.getColumnIndex(HealthyDroidQuizHelper.COLUMN_ANSWER));
QuizResults.add(ans);
cursor.moveToNext();
}
totalResults.add(QuizResults);
return totalResults;
}
:これは、データベースを照会するために呼び出されるメソッドである
public void query()
{
quesNum = (EditText)findViewById(R.id.idBox);
startingDate = (EditText)findViewById(R.id.startDate);
endingDate = (EditText)findViewById(R.id.endDate);
String num, start, end;
num = quesNum.getText().toString();
start = startingDate.getText().toString();
end = endingDate.getText().toString();
ArrayList<ArrayList<String>> result = datasource.getAnswer(Integer.parseInt(num), start, end);
textview.setText(result.get(0).toString());
}
public void onClick(View view)
{
switch (view.getId())
{
case R.id.add:
changeText();
break;
case R.id.query:
query();
break;
}
}
:これは、アクティビティのコードは
private static String CREATE_TABLE_ASSOCIATION = "create table "
+ TABLE_ASSOCIATION + "(" + COLUMN_QUESTION_ID + " integer not null, "
+ COLUMN_ANSWER + " text not null, " + COLUMN_DATETIME + " text not null, "
+ "FOREIGN KEY (" + COLUMN_QUESTION_ID + ") REFERENCES " + TABLE_QUESTION
+ "(" + COLUMN_ID + "), FOREIGN KEY (" + COLUMN_ANSWER + ") "
+ "REFERENCES "+ TABLE_ANSWER + "(" + COLUMN_ANSWER + "), "
+ "FOREIGN KEY (" + COLUMN_DATETIME + ") REFERENCES " + TABLE_ANSWER
+ "(" + COLUMN_DATETIME + ") PRIMARY KEY (" + COLUMN_QUESTION_ID + "," + COLUMN_DATETIME + "));";
ある:
これは、テーブルのスキーマであります
最後に、これはLogcat出力です。
05-09 19:07:32.897: D/AndroidRuntime(1229): Shutting down VM
05-09 19:07:32.897: W/dalvikvm(1229): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
05-09 19:07:32.897: E/AndroidRuntime(1229): Uncaught handler: thread main exiting due to uncaught exception
05-09 19:07:32.907: E/AndroidRuntime(1229): java.lang.IllegalStateException: Could not execute method of the activity
05-09 19:07:32.907: E/AndroidRuntime(1229): at android.view.View$1.onClick(View.java:2031)
05-09 19:07:32.907: E/AndroidRuntime(1229): at android.view.View.performClick(View.java:2364)
05-09 19:07:32.907: E/AndroidRuntime(1229): at android.view.View.onTouchEvent(View.java:4179)
05-09 19:07:32.907: E/AndroidRuntime(1229): at android.widget.TextView.onTouchEvent(TextView.java:6541)
05-09 19:07:32.907: E/AndroidRuntime(1229): at android.view.View.dispatchTouchEvent(View.java:3709)
05-09 19:07:32.907: E/AndroidRuntime(1229): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
05-09 19:07:32.907: E/AndroidRuntime(1229): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
05-09 19:07:32.907: E/AndroidRuntime(1229): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
05-09 19:07:32.907: E/AndroidRuntime(1229): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
05-09 19:07:32.907: E/AndroidRuntime(1229): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
05-09 19:07:32.907: E/AndroidRuntime(1229): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
05-09 19:07:32.907: E/AndroidRuntime(1229): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
05-09 19:07:32.907: E/AndroidRuntime(1229): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
05-09 19:07:32.907: E/AndroidRuntime(1229): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
05-09 19:07:32.907: E/AndroidRuntime(1229): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
05-09 19:07:32.907: E/AndroidRuntime(1229): at android.os.Handler.dispatchMessage(Handler.java:99)
05-09 19:07:32.907: E/AndroidRuntime(1229): at android.os.Looper.loop(Looper.java:123)
05-09 19:07:32.907: E/AndroidRuntime(1229): at android.app.ActivityThread.main(ActivityThread.java:4363)
05-09 19:07:32.907: E/AndroidRuntime(1229): at java.lang.reflect.Method.invokeNative(Native Method)
05-09 19:07:32.907: E/AndroidRuntime(1229): at java.lang.reflect.Method.invoke(Method.java:521)
05-09 19:07:32.907: E/AndroidRuntime(1229): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-09 19:07:32.907: E/AndroidRuntime(1229): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-09 19:07:32.907: E/AndroidRuntime(1229): at dalvik.system.NativeStart.main(Native Method)
05-09 19:07:32.907: E/AndroidRuntime(1229): Caused by: java.lang.reflect.InvocationTargetException
05-09 19:07:32.907: E/AndroidRuntime(1229): at ca.bcit.comp4900.TestDatabaseActivity.onClick(TestDatabaseActivity.java:74)
05-09 19:07:32.907: E/AndroidRuntime(1229): at java.lang.reflect.Method.invokeNative(Native Method)
05-09 19:07:32.907: E/AndroidRuntime(1229): at java.lang.reflect.Method.invoke(Method.java:521)
05-09 19:07:32.907: E/AndroidRuntime(1229): at android.view.View$1.onClick(View.java:2026)
05-09 19:07:32.907: E/AndroidRuntime(1229): ... 22 more
05-09 19:07:32.907: E/AndroidRuntime(1229): Caused by: java.lang.NullPointerException
05-09 19:07:32.907: E/AndroidRuntime(1229): at ca.bcit.comp4900.TestDatabaseActivity.query(TestDatabaseActivity.java:60)
05-09 19:07:32.907: E/AndroidRuntime(1229): ... 26 more
05-09 19:07:32.937: I/dalvikvm(1229): threadid=7: reacting to signal 3
05-09 19:07:32.947: I/dalvikvm(1229): Wrote stack trace to '/data/anr/traces.txt'
ありがとうございます!ここで
私は空の文字列の問題を修正し、私は別の原因で同じエラーを取得しています。私はUPDましたLogcatの出力を受け取りました – Strata
私は私の意見を正しく設定できませんでした。あなたの助けをありがとうDavid! – Strata