private final Context fContext;
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "TestLiriri";
public static final String TABLE_NAME = "questions";
public static final String KEY_ID = "_id";
public static final String KEY_QUEST = "quest";
Cursor cursor;
SQLiteDatabase database;
ContentValues contentValues = new ContentValues();;
String valueQuest;
public String getValueQuest() {
return valueQuest;
}
public void setValueQuest(String valueQuest) {
this.valueQuest = valueQuest;
}
public DataHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
fContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME + "(" + KEY_ID + " integer primary key," + KEY_QUEST + " text" + ");");
Resources res = fContext.getResources();
String[] cattable_records = res.getStringArray(R.array.catlist);
// проходим через массив и вставляем записи в таблицу
int length = cattable_records.length;
for (int i = 0; i<length; i++) {
contentValues.put("title", cattable_records[i]);
db.insert(TABLE_NAME, null, contentValues);
}
}
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "TestLiriri";
public static final String TABLE_NAME = "questions";
public static final String KEY_ID = "_id";
public static final String KEY_QUEST = "quest";
Cursor cursor;
SQLiteDatabase database;
ContentValues contentValues = new ContentValues();
String valueQuest;
public String getValueQuest() {
return valueQuest;
}
public void setValueQuest(String valueQuest) {
this.valueQuest = valueQuest;
}
public DataHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME + "(" + KEY_ID + " integer primary key," + KEY_QUEST + " text" + ");");
WriteDB("Почему это не работает?");
}
public void WriteDB(String quest){
database = this.getWritableDatabase();
contentValues.put(KEY_QUEST, quest);
database.insert(TABLE_NAME, null, contentValues);
}
2020-08-30 16:15:59.845 25735-25735/freijer.app.sucktest E/AndroidRuntime: FATAL EXCEPTION: main
Process: freijer.app.sucktest, PID: 25735
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.view.View$DeclaredOnClickListener.onClick(View.java:5979)
at android.view.View.performClick(View.java:7125)
at android.view.View.performClickInternal(View.java:7102)
at android.view.View.access$3500(View.java:801)
at android.view.View$PerformClick.run(View.java:27336)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:5974)
at android.view.View.performClick(View.java:7125)
at android.view.View.performClickInternal(View.java:7102)
at android.view.View.access$3500(View.java:801)
at android.view.View$PerformClick.run(View.java:27336)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor android.database.sqlite.SQLiteDatabase.query(java.lang.String, java.lang.String[], java.lang.String, java.lang.String[], java.lang.String, java.lang.String, java.lang.String)' on a null object reference
<b> at freijer.app.sucktest.DataHelper.ReadDB(DataHelper.java:61)</b>
Правда сделал в итоге 3 SwitchCompat, зависимые друг от друга - если 1 включен, 2 других выключены всегда.
Но материал точно пригодится.