【问题标题】:do not start services不启动服务
【发布时间】:2014-08-08 08:55:56
【问题描述】:

我创建了一个警报服务并在主活动中调用它,但得到这个错误:“unable to start service”...... 我在 db.java 中创建表并在警报服务中通过 InsertMagnt 设置变量。

DB DataBase = new DB();
DataBase.InsertMagnt(Acce_x, Acce_y, Acce_z, "", "", 1);
    DataBase.InsertMagnt(Meg_x, Meg_y, Meg_z, datetime, "", 2);
    DataBase.InsertMagnt(Tilt_x, Tilt_y, Tilt_z, datetime, Direct, 3);
    DataBase.InsertMagnt(lon, lat, 0, datetime, "", 4);

主要活动是:

Intent AlarmIntent = new Intent(MainActivity.this, AlarmService.class);
    pending = PendingIntent.getService(MainActivity.this, 0, AlarmIntent, 0);
    alarm = (AlarmManager) getSystemService(ALARM_SERVICE);

    btnStart.setOnClickListener(new OnClickListener() {

        public void onClick(View view) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            calendar.add(Calendar.SECOND, 5);
            alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 10 * 1000, pending);
            Toast.makeText(MainActivity.this, "Service Start", Toast.LENGTH_SHORT).show();
        }
    });

我得到这个错误:

08-08 13:24:21.334: E/AndroidRuntime(9000): java.lang.RuntimeException: Unable to start service com.example.farzaneh.AlarmService@417c9cd8 with Intent { flg=0x4 cmp=com.example.farzaneh/.AlarmService (has extras) }: android.database.sqlite.SQLiteException: unrecognized token: "')" (code 1): , while compiling: INSERT INTO Acce_Log (X,Y,Z,Date) VALUES (0.0,0.0,0.0')

【问题讨论】:

标签: android eclipse sqlite


【解决方案1】:

insert 声明是怎么回事

INSERT INTO Acce_Log (X,Y,Z,Date) VALUES (0.0,0.0,0.0')//date is not inserted and the '

您已指定 four values will be inserted but inserting only three,'。删除它,并将日期作为最后一个参数传递

【讨论】:

  • 您将日期保存为文本,您知道sql中的数据类型日期吗?\
  • @DividebyZero SQLite 没有提供任何存储日期的数据类型
【解决方案2】:

萨拉姆!

您的错误是因为您在将行插入 SQLite 表时出错(再次查看您给出的错误!)

INSERT INTO Acce_Log (X, Y, Z, Date) VALUES (0.0, 0.0, 0.0 ')
                                    unrecognized token ----^

请更正您的插入语句。另请注意,SQLite 不提供任何数据类型来存储日期,您应该以字符串或长格式存储日期。

我建议你使用长!使用Calendar 对象将您的日期转换为自纪元(UTC 时区)以来的毫秒数,现在可以将其存储在 SQLite 中

更新 #1

假设我在 SQLite 数据库中有下表

column name      type
-------------------------
   GID          INTEGER
  Name           TEXT
  Quan          INTEGER
 Surname         TEXT

现在,我想用一些示例数据填充此表,为此我将使用以下函数。

public void addRow() {
    ContentValues contentValues = new ContentValues();
    contentValues.put("GID", 123);
    contentValues.put("Name", "Edward");
    contentValues.put("Quan", 12);
    contentValues.put("Surname", "Kenway");

    mDataBase.insert("Your table name", null, contentValues);
}

【讨论】:

  • salam,我在 acce_log 中通过文本创建日期:'database.execSQL("CREATE TABLE IF NOT EXISTS Acce_Log (" + "ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL ," + "X DOUBLE, Y DOUBLE , Z DOUBLE,日期文本)");'并通过 database.execSQL("INSERT INTO Acce_Log (X,Y,Z,Date) VALUES" + " (" + X + "," + Y + "," + Z + ",'" + date + " ')");
  • @farzanehabolqasemi 请告诉我date 对象的类型是什么?
  • @farzanehabolqasemi 你为什么不使用 ContentValues ?它非常简单,将字符串连接在一起也更好。如果您同意,请告诉我在我的答案中添加一些示例代码
  • 我应该写 contentvalues 而不是 TEXT?我不知道这个!
  • @farzanehabolqasemi 别担心,保持冷静!!!请查看stackoverflow.com/questions/15384550/…stackoverflow.com/questions/18971434/…
猜你喜欢
  • 1970-01-01
  • 2016-05-30
  • 1970-01-01
  • 2019-05-16
  • 1970-01-01
  • 1970-01-01
  • 2018-12-03
  • 2015-12-17
  • 2012-04-20
相关资源
最近更新 更多