0

我想在android中做简单的报警应用程序。我使用ViewPager,Fragment用于查看并用于SQLiteDatabase数据库。

这是代码:

public class AlarmsActivity_Kronometre extends Fragment {

Button add;
SQL sql;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.activity_alarms_kronometre, container, false);
    sql = new SQL(view.getContext());
    add = (Button)view.findViewById(R.id.btn_add);
    add.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            sql.insertAlarm(1, 25, 11, 1, 1, "Message : 1", "path : 1");
            sql.insertAlarm(2, 30, 22, 5, 1, "Message : 2", "path : 2");
            sql.insertAlarm(3, 35, 33, 1, 5, "Message : 3", "path : 3");
            sql.insertAlarm(4, 40, 44, 4, 1, "Message : 4", "path : 4");
            sql.insertAlarm(5, 50, 55, 1, 4, "Message : 5", "path : 5");
        }
    });
    return view;
  }
}

SQL 类:

public class SQL {
private static final int VERSION = 3;
private static final String DATABASE_NAME = "alarms.db";
private static final String TABLE_ALARMS_ALARM = "Alarm";

private static final String COLUMN_ID = "ID";
private static final String COLUMN_HOUR = "Hour";
private static final String COLUMN_MINUTE = "Minute";
private static final String COLUMN_DAYSOFWEEK = "DaysOfWeek";
private static final String COLUMN_ENABLED = "Enabled";
private static final String COLUMN_VIBRATE = "Vibrate";
private static final String COLUMN_MESSAGE = "Message";
private static final String COLUMN_PATH = "Path";

int index_ID, index_HOUR, index_MINUTE, index_SECOND, index_WEEKDAY;
int index_ENABLED, index_VIBRATE, index_MESSAGE, index_PATH, index_DAYSOFWEEK;

private static final String CREATE_DATABASE_ALARMS = "CREATE TABLE " 
        + TABLE_ALARMS_ALARM+ " (" + COLUMN_ID      + " INTEGER PRIMARY KEY AUTOINCREMENT, " 
        + COLUMN_HOUR       + " INTEGER NOT NULL, " + COLUMN_MINUTE     + " INTEGER NOT NULL, " 
        + COLUMN_DAYSOFWEEK + " INTEGER NOT NULL, " + COLUMN_ENABLED    + " INTEGER NOT NULL, " 
        + COLUMN_VIBRATE    + " INTEGER NOT NULL, " + COLUMN_MESSAGE    + " TEXT NOT NULL, "
        + COLUMN_PATH       + " TEXT NOT NULL);";

private Context context;
private Cursor c;
private String[] columns_Alarms = { "id", "Hour", "Minute", "DaysOfWeek", "Enabled", "Vibrate", "Message", "Path"};
private SQLiteHelper sqLiteHelper;
private SQLiteDatabase sqLiteDatabase;

public SQL(Context c) {
    context = c;
}

public SQL openToRead() throws android.database.SQLException {
    sqLiteHelper = new SQLiteHelper(context, DATABASE_NAME, null, VERSION);
    sqLiteDatabase = sqLiteHelper.getReadableDatabase();
    return this;
}

public SQL openToWrite() throws android.database.SQLException {
    sqLiteHelper = new SQLiteHelper(context, DATABASE_NAME, null, VERSION);
    sqLiteDatabase = sqLiteHelper.getWritableDatabase();
    return this;
}

public void close() {
    sqLiteHelper.close();
}

public void insertAlarm(int hour, int minute, int daysofweek, int enabled, int vibrate, String message, String path) {
    ContentValues cv = new ContentValues();
    cv.put(COLUMN_HOUR, hour);
    cv.put(COLUMN_MINUTE, minute);
    cv.put(COLUMN_DAYSOFWEEK, daysofweek);
    cv.put(COLUMN_ENABLED, enabled);
    cv.put(COLUMN_VIBRATE, vibrate);
    cv.put(COLUMN_MESSAGE, message);
    cv.put(COLUMN_PATH, path);
    sqLiteDatabase.insert(TABLE_ALARMS_ALARM, null, cv);
}

public void deleteAlarm(int id) {
    sqLiteDatabase.delete(TABLE_ALARMS_ALARM, " " + COLUMN_ID + " = '" + id + "'", null);
}

public int deleteAlarmAll() {
    return sqLiteDatabase.delete(TABLE_ALARMS_ALARM, null, null);
}

protected ArrayList<String> getAlarmsId()
{
    c = sqLiteDatabase.query(TABLE_ALARMS_ALARM, columns_Alarms, null, null, null, null, null);
    ArrayList<String> id = null;

    setIndexColumns();
    for (c.moveToFirst(); !(c.isAfterLast()); c.moveToNext()) {
        id.add(c.getString(index_ID));
    }
    return id;
}
protected ArrayList<String> getAlarmsHour()
{
    c = sqLiteDatabase.query(TABLE_ALARMS_ALARM, columns_Alarms, null, null, null, null, null);
    ArrayList<String> hours = null;

    setIndexColumns();
    for (c.moveToFirst(); !(c.isAfterLast()); c.moveToNext()) {
        hours.add(c.getString(index_HOUR));
    }
    return hours;
}
protected ArrayList<String> getAlarmsMinute()
{
    c = sqLiteDatabase.query(TABLE_ALARMS_ALARM, columns_Alarms, null, null, null, null, null);
    ArrayList<String> minutes = null;

    setIndexColumns();
    for (c.moveToFirst(); !(c.isAfterLast()); c.moveToNext()) {
        minutes.add(c.getString(index_MINUTE));
    }
    return minutes;
}
protected ArrayList<String> getAlarmsDaysOfWeek()
{
    c = sqLiteDatabase.query(TABLE_ALARMS_ALARM, columns_Alarms, null, null, null, null, null);
    ArrayList<String> daysofweek = null;

    setIndexColumns();
    for (c.moveToFirst(); !(c.isAfterLast()); c.moveToNext()) {
        daysofweek.add(c.getString(index_DAYSOFWEEK));
    }
    return daysofweek;
}
protected ArrayList<String> getAlarmsEnabled()
{
    c = sqLiteDatabase.query(TABLE_ALARMS_ALARM, columns_Alarms, null, null, null, null, null);
    ArrayList<String> enabled = null;

    setIndexColumns();
    for (c.moveToFirst(); !(c.isAfterLast()); c.moveToNext()) {
        enabled.add(c.getString(index_ENABLED));
    }
    return enabled;
}
protected ArrayList<String> getAlarmsVibrate()
{
    c = sqLiteDatabase.query(TABLE_ALARMS_ALARM, columns_Alarms, null, null, null, null, null);
    ArrayList<String> vibrate = null;

    setIndexColumns();
    for (c.moveToFirst(); !(c.isAfterLast()); c.moveToNext()) {
        vibrate.add(c.getString(index_VIBRATE));
    }
    return vibrate;
}
protected ArrayList<String> getAlarmsMessage()
{
    c = sqLiteDatabase.query(TABLE_ALARMS_ALARM, columns_Alarms, null, null, null, null, null);
    ArrayList<String> message = null;

    setIndexColumns();
    for (c.moveToFirst(); !(c.isAfterLast()); c.moveToNext()) {
        message.add(c.getString(index_MESSAGE));
    }
    return message;
}
protected ArrayList<String> getAlarmsPath()
{
    c = sqLiteDatabase.query(TABLE_ALARMS_ALARM, columns_Alarms, null, null, null, null, null);
    ArrayList<String> path = null;

    setIndexColumns();
    for (c.moveToFirst(); !(c.isAfterLast()); c.moveToNext()) {
        path.add(c.getString(index_PATH));
    }
    return path;
}

private void setIndexColumns() {
    index_ID = c.getColumnIndex("ID");
    index_HOUR = c.getColumnIndex("Hour");
    index_MINUTE = c.getColumnIndex("Minute");
    index_DAYSOFWEEK = c.getColumnIndex("DaysOfWeek");
    index_ENABLED = c.getColumnIndex("Enabled");
    index_VIBRATE = c.getColumnIndex("Vibrate");
    index_MESSAGE = c.getColumnIndex("Message");
    index_PATH = c.getColumnIndex("Path");
}

protected class SQLiteHelper extends SQLiteOpenHelper {
    public SQLiteHelper(Context context, String name, CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_DATABASE_ALARMS);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (oldVersion <= newVersion)
            return;
        db.execSQL("DROP TABLE IF EXISTS " + CREATE_DATABASE_ALARMS);
        onCreate(db);
    }
}
}

当我单击添加按钮时,出现以下错误:

07-08 15:13:17.036: I/Process(4004): Sending signal. PID: 4004 SIG: 9
07-08 15:13:18.704: D/libEGL(4037): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
07-08 15:13:18.712: D/libEGL(4037): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
07-08 15:13:18.716: D/libEGL(4037): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
07-08 15:13:18.786: D/OpenGLRenderer(4037): Enabling debug mode 0
07-08 15:13:22.696: I/EKLE_ONCLICK(4037): TRUE
07-08 15:13:22.696: D/AndroidRuntime(4037): Shutting down VM
07-08 15:13:22.696: W/dalvikvm(4037): threadid=1: thread exiting with uncaught exception (group=0x40c6d930)
07-08 15:13:22.704: E/AndroidRuntime(4037): FATAL EXCEPTION: main
07-08 15:13:22.704: E/AndroidRuntime(4037): java.lang.NullPointerException
07-08 15:13:22.704: E/AndroidRuntime(4037):     at com.kongry.testalarm.SQL.insertAlarm(SQL.java:71)
07-08 15:13:22.704: E/AndroidRuntime(4037):     at com.kongry.testalarm.AlarmsActivity_Kronometre$1.onClick(AlarmsActivity_Kronometre.java:27)
07-08 15:13:22.704: E/AndroidRuntime(4037):     at android.view.View.performClick(View.java:4211)
07-08 15:13:22.704: E/AndroidRuntime(4037):     at android.view.View$PerformClick.run(View.java:17362)
07-08 15:13:22.704: E/AndroidRuntime(4037):     at android.os.Handler.handleCallback(Handler.java:725)
07-08 15:13:22.704: E/AndroidRuntime(4037):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-08 15:13:22.704: E/AndroidRuntime(4037):     at android.os.Looper.loop(Looper.java:137)
07-08 15:13:22.704: E/AndroidRuntime(4037):     at android.app.ActivityThread.main(ActivityThread.java:5227)
07-08 15:13:22.704: E/AndroidRuntime(4037):     at java.lang.reflect.Method.invokeNative(Native Method)
07-08 15:13:22.704: E/AndroidRuntime(4037):     at java.lang.reflect.Method.invoke(Method.java:511)
07-08 15:13:22.704: E/AndroidRuntime(4037):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
07-08 15:13:22.704: E/AndroidRuntime(4037):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
07-08 15:13:22.704: E/AndroidRuntime(4037):     at dalvik.system.NativeStart.main(Native Method)

错误是什么?我哪里错了?

4

2 回答 2

1

使用此代码:

public class AlarmsActivity_Kronometre extends Fragment {

Button add;
SQL sql;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_alarms_kronometre, container, false);
sql = new SQL(view.getContext());
**sql.openToWrite();**
add = (Button)view.findViewById(R.id.btn_add);
add.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        sql.insertAlarm(1, 25, 11, 1, 1, "Message : 1", "path : 1");
        sql.insertAlarm(2, 30, 22, 5, 1, "Message : 2", "path : 2");
        sql.insertAlarm(3, 35, 33, 1, 5, "Message : 3", "path : 3");
        sql.insertAlarm(4, 40, 44, 4, 1, "Message : 4", "path : 4");
        sql.insertAlarm(5, 50, 55, 1, 4, "Message : 5", "path : 5");
    }
  });
return view;
 }
}
于 2013-07-08T12:30:20.503 回答
1

添加
sql = new SQL(view.getContext());
sql.openToWrite();

于 2013-07-08T12:30:20.787 回答