I have been working on an app which has has a SQL database. I am trying to retrieve data in a specific column 'COL3' of SQL database "YEAR_DETAILS" when a button is pressed but I am getting a lot of errors can someone tell me what is it that i am doing wrong.
public class AddTransaction extends AppCompatActivity {
int type1 ;
int type2;
double amount;
ArrayList<add_transaction_data> data;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_transaction);
ActionBar actionBar = getSupportActionBar();
assert actionBar != null;
actionBar.hide();
populateList();
final ImageButton cancel = findViewById(R.id.at_btn_cancel);
final EditText EditAmount = findViewById(R.id.at_enter_amount);
ImageButton save = findViewById(R.id.at_btn_save);
ImageButton income = findViewById(R.id.at_btn_income);
ImageButton loan = findViewById(R.id.at_btn_loan);
RecyclerView recyclerView = findViewById(R.id.at_btn_recyclerview);
recyclerView.setLayoutManager(new GridLayoutManager(this , 5));
recyclerView.setAdapter(new at_btn_adapter(data, new onClickRecyclerButtion() {
@Override
public void onPositionClicked(int position) {
type1 = 2;
type2 = position;
}
}));
income.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
type1 = 1;
type2 = -1;
}
});
loan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
type1 = 1;
type2 = -2;
}
});
context = this;
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("it is all cool here");
DatabaseHelper1 databaseHelper1 = new DatabaseHelper1(getBaseContext());
DatabaseHelper2 databaseHelper2 = new DatabaseHelper2(getBaseContext());
double amount = Double.parseDouble(EditAmount.getText().toString());
System.out.println(amount);
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
String m = changeString(month);
System.out.println(type1);
double mRamount = -1;
if(type1 == 1){
Cursor cursor = databaseHelper1.getRemainingAmount(m , String.valueOf(year));
mRamount= cursor.getDouble(3);
System.out.println(mRamount);//printing amount.
}
startActivity(new Intent(AddTransaction.this , MainActivity.class));
}
});
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(AddTransaction.this , MainActivity.class));
}
});
}
void populateList(){
data = new ArrayList<>();
data.add(new add_transaction_data("Add" , R.drawable.ic_add_black_24dp));
data.add(new add_transaction_data("Shopping" , R.drawable.at_ic_shopping));
data.add(new add_transaction_data("Food" , R.drawable.at_ic_food));
data.add(new add_transaction_data("Traveling" , R.drawable.at_ic_travel));
data.add(new add_transaction_data("Education" , R.drawable.at_ic_education));
data.add(new add_transaction_data("Energy" , R.drawable.at_ic_energy));
data.add(new add_transaction_data("House" , R.drawable.at_ic_home));
data.add(new add_transaction_data("Fitness" , R.drawable.at_ic_sports));
data.add(new add_transaction_data("Personal" , R.drawable.at_ic_personal));
data.add(new add_transaction_data("Other" , R.drawable.at_ic_other));
}
String changeString(int month){
String m;
switch (month){
case 0:
m = "January";
break;
case 1:
m = "Feburary";
break;
case 2:
m = "March";
break;
case 3:
m = "April";
break;
case 4:
m = "May";
break;
case 5:
m = "June";
break;
case 6:
m = "July";
break;
case 7:
m = "August";
break;
case 8:
m = "September";
break;
case 9:
m = "October";
break;
case 10:
m = "November";
break;
case 11:
m = "December";
break;
default:
m = " ";
}
return m;
}
}
Database helper:
public class DatabaseHelper1 extends SQLiteOpenHelper {
private static final int version = 3;
private static final String name = "YEAR_DETAILS";
private static final String COL0 = "YEAR";
private static final String COL1 = "MONTH";
private static final String COL2 = "SPENT";
private static final String COL3 = "REMAINING";
private static final String TAG = "Database Helper 1";
public DatabaseHelper1(@Nullable Context context) {
super(context, name, null, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.d(TAG , "Database 1 is being created");
String create_table = "CREATE TABLE "+name+"("+
COL0+" INTEGER, "+
COL1+" TEXT, "+
COL2+" DOUBLE, "+
COL3+" DOUBLE);";
db.execSQL(create_table);
}
@Override
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+name);
onCreate(db);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
private boolean createMonth(String month , String year){
Log.d(TAG , "month row is being created");
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL0 , year);
contentValues.put(COL1 , month);
contentValues.put(COL2 , 0.0);
contentValues.put(COL3 , 0.0);
long result = db.insert(name,null , contentValues);
return result != -1;
}
public boolean spentChange(String month , String year , Double newAmount){
Log.d(TAG , "spent amount is being changed");
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COL2 , newAmount);
long result = db.update(name, values , "MONTH = ? AND YEAR = ?", new String[]{month , year});
return result != -1;
}
public boolean remainingChange(String month ,String year, Double newAmount){
Log.d(TAG , "Remaining amount is being changed");
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues =new ContentValues();
contentValues.put(COL3 , newAmount);
long result = db.update(name , contentValues , "MONTH = ? AND YEAR = ?" , new String[]{month , year});
return result != -1;
}
public void checkExistance(String month, String year){
Log.d(TAG , "Checking if month already exits");
SQLiteDatabase database = this.getWritableDatabase();
Cursor cur = database.query(name , null , "MONTH = ? AND YEAR = ?" , new String[]{month , year} , null , null , null);
System.out.println(cur);
if(cur.getCount() <= 0) {
boolean b = createMonth(month , year);
if(b)
Log.d(TAG , "checkExistance: failed to create month");
else
Log.d(TAG, "checkExistance: month created successfully");
}
}
public Cursor getdata(){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT * FROM YEAR_DETAILS;";
return db.rawQuery(query , null);
}
public Cursor getSpentAmount(String month , String year){
SQLiteDatabase db = this.getReadableDatabase();
String query = "SELECT * FROM YEAR_DETAILS WHERE MONTH = " + month+ " AND YEAR = "+year;
return db.rawQuery(query , null);
}
public Cursor getRemainingAmount(String month , String year){
SQLiteDatabase db = this.getReadableDatabase();
Cursor cur = db.query(name , new String[]{COL3} , "MONTH = ? AND YEAR = ?" , new String[]{month , year} , null , null , null);
return cur;
}
}
Error:
2020-05-29 21:40:06.971 30591-30591/com.carrot.wallet E/AndroidRuntime: FATAL EXCEPTION: main Process: com.carrot.wallet, PID: 30591 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.carrot.wallet/com.carrot.wallet.MainActivity}: android.view.InflateException: Binary XML file line #10 in com.carrot.wallet:layout/activity_main: Binary XML file line #10 in com.carrot.wallet:layout/activity_main: Error inflating class fragment at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3387) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3526) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2122) at android.os.Handler.dispatchMessage(Handler.java:107) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7695) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950) Caused by: android.view.InflateException: Binary XML file line #10 in