This question already has an answer here:
It's my first try at SQLite databases, so it'll probably look chaotic and such.
DBHelper.java
package com.example.tim.timapp;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
/**
* Created by Tim on 18-Feb-16.
*/
public class DBHelper extends SQLiteOpenHelper{
public static final String DATABASE_NAME = "Database.db";
private static final int DATABASE_VERSION = 1;
public static final String LINK_TABLE_NAME = "myLinkTable";
public static final String LINK_COLUMN_ID= "_id";
public static final String LINK_COLUMN_NAME = "Name";
public static final String LINK_COLUMN_TAG = "Tag";
public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + LINK_TABLE_NAME + "(" + LINK_COLUMN_ID + " INTEGER PRIMARY KEY, " + LINK_COLUMN_NAME + " TEXT, " + LINK_COLUMN_TAG + " TEXT" + ");");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + LINK_TABLE_NAME);
onCreate(db);
}
public void insertLink(String name, String tag){
SQLiteDatabase db = getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(LINK_COLUMN_NAME, name);
contentValues.put(LINK_COLUMN_TAG, tag);
db.insert(LINK_TABLE_NAME, null, contentValues);
db.close();
}
public Cursor getLink(String name){
SQLiteDatabase db = getReadableDatabase();
Cursor res = db.rawQuery("SELECT * FROM " + LINK_TABLE_NAME + " WHERE " + LINK_COLUMN_NAME + "=?", new String[]{name});
return res;
}
public Cursor getAll(){
SQLiteDatabase db = getReadableDatabase();
Cursor res = db.rawQuery("SELECT * FROM " + LINK_TABLE_NAME, null);
return res;
}
public void deleteLink(String name){
SQLiteDatabase db = getWritableDatabase();
db.execSQL("DELETE FROM " + LINK_TABLE_NAME + " WHERE " + LINK_COLUMN_NAME + " =\"" + name + "\";" );
}
public void deleteAll(){
SQLiteDatabase db = getWritableDatabase();
db.execSQL("DELETE FROM " + LINK_TABLE_NAME + " WHERE 1");
}
public String databaseToString(){
String dbString = "";
SQLiteDatabase db = getWritableDatabase();
String query = "SELECT * FROM " + LINK_TABLE_NAME + " WHERE 1";
Cursor c = db.rawQuery(query, null);
c.moveToFirst();
c.close();
while (!c.isAfterLast()){
if(c.getString(c.getColumnIndex("Name")) != null) {
dbString += c.getString(c.getColumnIndex("Name"));
dbString += "\n";
}
}
db.close();
return dbString;
}
}
TimeLightFragment.java (Fragment that contains the textview and button)
package com.example.fragments.MainFragments;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import com.example.tim.timapp.DBHelper;
import com.example.tim.timapp.R;
import org.w3c.dom.Text;
/**
* Created by Tim on 29-Jan-16.
*/
public class TimeLightFragment extends Fragment {
TextView timText;
DBHelper dbHelper;
@Override
public void onCreate(Bundle savedInstanceState) {
setHasOptionsMenu(true);
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_timelight, container, false);
final Button refreshButton = (Button) rootView.findViewById(R.id.refreshButton);
final Button deleteButton = (Button) rootView.findViewById(R.id.deleteButton);
final TextView textView = (TextView) rootView.findViewById(R.id.overviewTextview);
refreshButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
buttonClicked(v);
}
}
);
return rootView;
}
public void buttonClicked(View view){
printDatabase();
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// menu.add(0, 1, 0, "TEST Time Light");
// super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.main, menu);
}
public void deleteButtonClick(View view) {
dbHelper.deleteAll();
printDatabase();
}
public void refreshClicked(View view) {
// printDatabase();
Log.d("TEST", "TEST");
}
public void printDatabase(){
String dbString = dbHelper.databaseToString();
timText.setText(dbString);
}
}
Log:
02-21 17:24:15.420 4830-4830/? I/art: Not late-enabling -Xcheck:jni (already on)
02-21 17:24:15.470 4830-4830/com.example.tim.timapp W/System: ClassLoader referenced unknown path: /data/app/com.example.tim.timapp-1/lib/x86_64
02-21 17:24:15.630 4830-4853/com.example.tim.timapp D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
02-21 17:24:15.690 4830-4853/com.example.tim.timapp I/OpenGLRenderer: Initialized EGL, version 1.4
02-21 17:24:15.760 4830-4853/com.example.tim.timapp W/EGL_emulation: eglSurfaceAttrib not implemented
02-21 17:24:15.760 4830-4853/com.example.tim.timapp W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7f5c96c5c380, error=EGL_SUCCESS
02-21 17:24:16.660 4830-4830/com.example.tim.timapp I/Choreographer: Skipped 48 frames! The application may be doing too much work on its main thread.
02-21 17:24:24.410 4830-4830/com.example.tim.timapp D/AndroidRuntime: Shutting down VM
02-21 17:24:24.410 4830-4830/com.example.tim.timapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.tim.timapp, PID: 4830
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.tim.timapp.DBHelper.databaseToString()' on a null object reference
at com.example.fragments.MainFragments.TimeLightFragment.printDatabase(TimeLightFragment.java:77)
at com.example.fragments.MainFragments.TimeLightFragment.buttonClicked(TimeLightFragment.java:56)
at com.example.fragments.MainFragments.TimeLightFragment$1.onClick(TimeLightFragment.java:47)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
02-21 17:29:24.520 4830-4830/? I/Process: Sending signal. PID: 4830 SIG: 9
So the issue: I'm trying to use this database to store stuff (obviously). At the moment I got the saving stuff running without errors, but I don't know if it's actually working. That's why I wanted to create a simple button and textview, to quickly show me everything that's stored in a table. Unfortunately, I get the error seen in the log when I hit the refresh button. I'm able to narrow it down to the databaseToString function (not that hard of course), but then I'm stuck. Anybody know what's going wrong?
Aucun commentaire:
Enregistrer un commentaire