I'd like to quickly mention this is a homework assignment, but I'm only looking for direction. My professor has not applied SQLite before, so he's unsure what to do. The class is doing SharedPreference (which I couldn't get), but this was another option to use for storing data for a To-Do List.
Now when I go to hit a button to "Add" a task under the AddDo class, it throws the following error(s):
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{net.example.name.dotoday/net.name.example.dotoday.AddDo}: java.lang.ClassCastException: net.example.name.dotoday.AddDo cannot be cast to android.app.Activity
EDIT: Here's the class for the actual button. This is where my main task list will reside, as the XML is ready for data and has a button for adding a new task.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button addMainTask = (Button) findViewById(R.id.addMainTask);
addMainTask.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//This is being flagged as of now because the class type IS not Activity, but serializable.
startActivity(new Intent(main.this, AddDo.class));
}
});
This is my class meant for adding a task (this is in early stages):
package net.example.name.dotoday;
import java.io.Serializable;
public class AddDo implements Serializable {
//This is what will be transferred to the actual DB when I make a task.
public String doParentID, doTaskTitle, doStatus, doDueDate, doCompletedTask, doCalculatedProgress = "";
}
This is my DBHandler:
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import net.example.name.dotoday.AddDo;
import java.util.ArrayList;
import java.util.List;
public class DBHelper extends SQLiteOpenHelper {
private static final String TAG = "DbHelper";
// Database Info
private static final String TheDB = "TheDB";
private static final int DATABASE_VERSION = 1;
//DB Table Names
private static final String TABLE_ParentTask = "ParentTask";
private static final String TABLE_SubTask = "SubTask";
//ParentTask Table Columns
private static final String parentIDColumn = "_parentID";
private static final String taskTitleColumn = "_taskTitle";
private static final String statusColumn = "_status";
private static final String dueDateColumn = "_dueDate";
private static final String completedColumn = "_completedTask"; //When completion is 100%
private static final String calculatedProgressColumn = "_calculatedProgress"; //LONG VALUE WHEN CONVERTED.
//SubTask Table Columns
private static final String foreignkeyColumn = "_foreignkey";
private static final String childIDColumn = "_childID";
private static final String subTaskDescColumn = "_subTaskDesc";
private static final String subCompletedColumn = "_subCompleted"; // BOOL for individual tasks -- may not be implemented
/* -- Here comes the real fun, ya'll! -- */
private static DBHelper mDbHelper;
public static synchronized DBHelper getInstance(Context context) {
// Use the application context, which will ensure that you
// don't accidentally leak an Activity's context.
if (mDbHelper == null) {
mDbHelper = new DBHelper(context.getApplicationContext());
}
return mDbHelper;
}
/**
* Constructor should be private to prevent direct instantiation.
* Make a call to the static method "getInstance()" instead.
*/
private DBHelper(Context context) {
super(context, TheDB, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_ParentTask_TABLE = "CREATE TABLE " + TABLE_ParentTask +
"(" +
parentIDColumn + " INTEGER PRIMARY KEY ," +
taskTitleColumn + " TEXT," +
statusColumn + " BOOL," +
dueDateColumn+ " TEXT," +
completedColumn + " BOOL," +
calculatedProgressColumn + " REAL" +
")";
//CREATE THE PHYSICAL TABLE
db.execSQL(CREATE_ParentTask_TABLE);
/*String CREATE_SubTask_TABLE = "CREATE TABLE " + TABLE_SubTask +
"(" +
childID + " INTEGER PRIMARY KEY ," +
foreignkey + " INT," +
subTaskDesc + " TEXT," +
subCompleted + " BOOL" +
")";
//CREATE THE PHYSICAL TABLE
db.execSQL(CREATE_SubTask_TABLE);*/
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion != newVersion) {
// Simplest implementation is to drop all old tables and recreate them
db.execSQL("DROP TABLE IF EXISTS " + TABLE_ParentTask);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_SubTask);
onCreate(db);
}
}
public void insertTaskDetails(AddDo userData) {
SQLiteDatabase db = getWritableDatabase();
db.beginTransaction();
try {
ContentValues values = new ContentValues();
values.put(parentIDColumn, userData.doParentID);
values.put(taskTitleColumn, userData.doTaskTitle);
values.put(statusColumn, userData.doStatus);
values.put(dueDateColumn, userData.doDueDate);
values.put(completedColumn, userData.doCompletedTask);
values.put(calculatedProgressColumn, userData.doCalculatedProgress);
db.insertOrThrow(TABLE_ParentTask, null, values);
db.setTransactionSuccessful();
} catch (SQLException e) {
e.printStackTrace();
Log.d(TAG, "Error while trying to add post to database");
} finally {
db.endTransaction();
}
}
/*
fetch all data from UserTable
*/
public List<AddDo> getAllMainTasks() {
List<AddDo> usersdetail = new ArrayList<>();
String USER_DETAIL_SELECT_QUERY = "SELECT * FROM " + TABLE_ParentTask;
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery(USER_DETAIL_SELECT_QUERY, null);
try {
if (cursor.moveToFirst()) {
do {
AddDo userData = new AddDo();
userData.doParentID = cursor.getString(cursor.getColumnIndex(parentIDColumn));
userData.doTaskTitle = cursor.getString(cursor.getColumnIndex(taskTitleColumn));
userData.doStatus = cursor.getString(cursor.getColumnIndex(statusColumn));
userData.doDueDate = cursor.getString(cursor.getColumnIndex(dueDateColumn));
userData.doCompletedTask = cursor.getString(cursor.getColumnIndex(completedColumn));
userData.doCalculatedProgress = cursor.getString(cursor.getColumnIndex(calculatedProgressColumn));
usersdetail.add(userData);
} while (cursor.moveToNext());
}
} catch (Exception e) {
Log.d(TAG, "Error while trying to get posts from database");
} finally {
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
}
return usersdetail;
}
/*
Delete single row from ParentTask
*/
void deleteRow(String name) {
SQLiteDatabase db = getWritableDatabase();
try {
db.beginTransaction();
db.execSQL("delete from " + TABLE_ParentTask + " where parentid ='" + parentIDColumn + "'");
db.setTransactionSuccessful();
} catch (SQLException e) {
Log.d(TAG, "Error while trying to delete task detail");
} finally {
db.endTransaction();
}
}
}
I was wondering if this had to do with the fact AddDo implements serializable? I could trade it off to a different XML with a standard activity, and it would work.
Any direction here would be appreciated. My professor and I have no idea why this is not wanting to behave, and neither of us used SQLite until now.
Thank you so much!
AddDo is not extending Activity class
Use below code extend using Activity or AppCompactActivity
public class AddDo extends Activity implements Serializable {