何かをクエリして何かが返されない場合、if(cursor.moveToFirst()) { //logic} はアクティビティでうまく機能しますが、インテント サービスで使用する場合は同じことが言えません。何が悪いのかわかりません。クエリが何かを返すと (CLASSROOM_AVAILABLE の値が 0 のすべての行を選択すると)、特定の行の「CLASSROOM_AVAILABLE」は 0 から 1 になり、何も返さない場合は、すべての行の CLASSROOM_AVAILABLE が 1 から 2 になります。 .
package my.skul;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class CheckClassroom extends IntentService {
/**
* A constructor is required, and must call the super IntentService(String)
* constructor with a name for the worker thread.
*/
public CheckClassroom() {
super("CheckClassroom");
}
/**
* The IntentService calls this method from the default worker thread with
* the intent that started the service. When this method returns,
* IntentService stops the service, as appropriate.
*/
@Override
protected void onHandleIntent(Intent intent) {
open();
getColumns();
close();
}
private DbHelper ourHelper;
private SQLiteDatabase ourDatabase;
public void open() {
ourHelper = new DbHelper();
ourDatabase = ourHelper.getWritableDatabase();
}
public void close() {
ourHelper.close();
}
private class DbHelper extends SQLiteOpenHelper {
public DbHelper() {
super(CheckClassroom.this, "myschool", null, 1);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
}
public void getColumns()
{
String[] columns = new String[] {"CLASSROOM_ID"};
Cursor c = ourDatabase.query("CLASSROOM_TABLE", columns, "CLASSROOM_AVAILABLE = 0", null,
null, null, null);
if (c.moveToFirst())
{
int[] myid = new int[c.getCount()];
int i = 0;
while (!c.isAfterLast())
{
myid[i]=c.getInt(0);
ContentValues cv = new ContentValues();
cv.put("CLASSROOM_AVAILABLE", 1);
ourDatabase.update("CLASSROOM_TABLE", cv, "CLASSROOM_ID = "+myid[i], null);
i++;
c.moveToNext();
}
}
else
{
ContentValues cv = new ContentValues();
cv.put("CLASSROOM_AVAILABLE", 2);
ourDatabase.update("CLASSROOM_TABLE", cv, "CLASSROOM_ID =1", null);
}
}
}
ここにログキャットがあります
10-29 12:04:23.903: D/AndroidRuntime(22782): Shutting down VM
10-29 12:04:23.903: W/dalvikvm(22782): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
10-29 12:04:23.923: E/AndroidRuntime(22782): FATAL EXCEPTION: main
10-29 12:04:23.923: E/AndroidRuntime(22782): java.lang.IllegalStateException: Could not execute method of the activity
10-29 12:04:23.923: E/AndroidRuntime(22782): at android.view.View$1.onClick(View.java:2072)
10-29 12:04:23.923: E/AndroidRuntime(22782): at android.view.View.performClick(View.java:2408)
10-29 12:04:23.923: E/AndroidRuntime(22782): at android.view.View$PerformClick.run(View.java:8816)
10-29 12:04:23.923: E/AndroidRuntime(22782): at android.os.Handler.handleCallback(Handler.java:587)
10-29 12:04:23.923: E/AndroidRuntime(22782): at android.os.Handler.dispatchMessage(Handler.java:92)
10-29 12:04:23.923: E/AndroidRuntime(22782): at android.os.Looper.loop(Looper.java:123)
10-29 12:04:23.923: E/AndroidRuntime(22782): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-29 12:04:23.923: E/AndroidRuntime(22782): at java.lang.reflect.Method.invokeNative(Native Method)
10-29 12:04:23.923: E/AndroidRuntime(22782): at java.lang.reflect.Method.invoke(Method.java:521)
10-29 12:04:23.923: E/AndroidRuntime(22782): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-29 12:04:23.923: E/AndroidRuntime(22782): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-29 12:04:23.923: E/AndroidRuntime(22782): at dalvik.system.NativeStart.main(Native Method)
10-29 12:04:23.923: E/AndroidRuntime(22782): Caused by: java.lang.reflect.InvocationTargetException
10-29 12:04:23.923: E/AndroidRuntime(22782): at my.skul.Mainmenu.sectwenty(Mainmenu.java:71)
10-29 12:04:23.923: E/AndroidRuntime(22782): at java.lang.reflect.Method.invokeNative(Native Method)
10-29 12:04:23.923: E/AndroidRuntime(22782): at java.lang.reflect.Method.invoke(Method.java:521)
10-29 12:04:23.923: E/AndroidRuntime(22782): at android.view.View$1.onClick(View.java:2067)
10-29 12:04:23.923: E/AndroidRuntime(22782): ... 11 more
10-29 12:04:23.923: E/AndroidRuntime(22782): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
10-29 12:04:23.923: E/AndroidRuntime(22782): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
10-29 12:04:23.923: E/AndroidRuntime(22782): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
10-29 12:04:23.923: E/AndroidRuntime(22782): at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:99)
10-29 12:04:23.923: E/AndroidRuntime(22782): at my.skul.MyCreateClassrooms.get(MyCreateClassrooms.java:111)
10-29 12:04:23.923: E/AndroidRuntime(22782): ... 15 more
これは同じコードで、アクティビティで完全に正常に動作します..
package my.skul;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class MyCreateClassrooms {
private static final String DATABASE_NAME = "myschool";
private static final int DATABASE_VERSION = 1;
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private static class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE MAIN_TABLE (OURID INTEGER PRIMARY KEY AUTOINCREMENT, date DATETIME);");
db.execSQL("CREATE TABLE CLASSROOM_TABLE (CLASSROOM_ID INTEGER PRIMARY KEY AUTOINCREMENT,"
+ "CLASSROOM_AVAILABLE INTEGER , "
+ "CLASSROOM_COMPLETION DATETIME); ");
db.execSQL("INSERT INTO MAIN_TABLE (date) VALUES (DATETIME('now'))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXIST MAIN_TABLE");
onCreate(db);
}
}
public MyCreateClassrooms(Context c) {
ourContext = c;
}
public MyCreateClassrooms open() {
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close() {
ourHelper.close();
}
public void getColumns()
{
String[] columns = new String[] {"CLASSROOM_ID"};
Cursor c = ourDatabase.query("CLASSROOM_TABLE", columns, "CLASSROOM_AVAILABLE = 0", null,
null, null, null);
if (!c.moveToFirst())
{
int[] myid = new int[c.getCount()];
int i = 0;
while (!c.isAfterLast())
{
myid[i]=c.getInt(0);
ContentValues cv = new ContentValues();
cv.put("CLASSROOM_AVAILABLE", 1);
ourDatabase.update("CLASSROOM_TABLE", cv, "CLASSROOM_ID = "+myid[i], null);
i++;
c.moveToNext();
}
}
if(c.moveToFirst())
{
ContentValues cv = new ContentValues();
cv.put("CLASSROOM_AVAILABLE", 2);
ourDatabase.update("CLASSROOM_TABLE", cv, null, null);
}
}
}