0

I'm making small to-do app, and I got stuck.
The idea is:

Save the data into the DB -> Retrieve it in a ListView.

At the moment, my app crashes right after I press the "BUILD!" Button, even though I think it should just say "SAVED!" (To make sure the data is saved into the db).
Some hints on how to display that data in ListView would be much appreciated.

P.S.: It's my first app, so don't be harsh

Code:

Mainactivity

package com.example.mrti.todo2;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

TaskDbHelper taskDbHelper;
SQLiteDatabase SQLdb;
ArrayAdapter<String> itemsAdapter;
Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ListView lvItems = (ListView) findViewById(R.id.listView);
    ArrayList<String> items = new ArrayList<String>();

    itemsAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, items);
    lvItems.setAdapter(itemsAdapter);


}

// ADDING YOUR FUTURE GOALS!
public void onBuildButton (View view){
    EditText dreamText = (EditText) findViewById(R.id.dreamText);
    String task = dreamText.getText().toString();

    //db
    taskDbHelper = new TaskDbHelper(context);
    SQLdb = taskDbHelper.getWritableDatabase();
    taskDbHelper.insertData(task, SQLdb);
    Toast.makeText(getBaseContext(), "Saved!", Toast.LENGTH_LONG).show();
    taskDbHelper.close();
}


}

TaskDBHelper.java

public class TaskDbHelper extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "TASKS.db";
private static final int DB_VERSION = 1;
private static final String CREATE_QUERY =
                        "CREATE TABLE " +         TaskContract.newTaskInfo.TABLE_NAME + " ( " +
                        TaskContract.newTaskInfo.TASK_NAME + " TEXT NOT NULL);";


public TaskDbHelper(Context context) {
    super(context, DATABASE_NAME, null, DB_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_QUERY);
    Log.e("DB Operations", "Table created...");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TaskContract.newTaskInfo.TABLE_NAME);
    onCreate(db);
}
// For adding new data to database
public void insertData(String task, SQLiteDatabase db){
    ContentValues contentValues = new ContentValues();
    contentValues.put(TaskContract.newTaskInfo.TASK_NAME, task);
    db.insert(TaskContract.newTaskInfo.TABLE_NAME, null, contentValues);
        Log.e("DB Operations", "1 record added...");

}
}

TaskContract.java

public class TaskContract {

public static abstract class newTaskInfo{
    public static final String TASK_NAME = "task_name"; // Table field
    public static final String TABLE_NAME = "task_info"; // Name of the table
}
  • If the app crashes, please show the logcat. – OneCricketeer Oct 29 '16 at 17:55
  • And tip: Use a CursorAdapter, not an ArrayAdapter – OneCricketeer Oct 29 '16 at 17:56
  • @cricket_007 There's like 30 lines of error. Not sure which one I should show you. Maybe it's easier to just copy/paste my code and check for yourself? Or is there a specific way to find an error @logcat? – Maartin1996 Oct 29 '16 at 18:00
  • Copy the majority of it. The important lines contain `Caused by`, plus those following it. http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – OneCricketeer Oct 29 '16 at 18:02
  • Also, you can move `getWritableDatabase()` into the `insertData` method. Rather than a parameter. The reason for the SqliteOpenHelper is to hide the raw database object from the Activity class – OneCricketeer Oct 29 '16 at 18:05
  • at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223) – Maartin1996 Oct 29 '16 at 18:05
  • Please [edit] your post and format the error, if you can. Thanks – OneCricketeer Oct 29 '16 at 18:06

1 Answers1

0

Your context variable is null and unnecessary.

Activities are contexts, so use that instead

taskDbHelper = new TaskDbHelper(MainActivity.this);
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245