The LaunchFav crashes at the first i.putExtra. The error is "The
application Track My Drinks (process.parisj13.trackmydrinks) has stopped
unexpectedly. What am I doing wrong?
private Intent LaunchFav()
{
Cursor c = mFavDrink.fetchAllDrinks();
Intent i = new Intent(this, FavoriteDrink.class);
if(c.isBeforeFirst() && c.isAfterLast()){
i.putExtra(TrackMyDrinksDbAdapter.KEY_DRINK_ID,
-1);
return i;
}
i.putExtra(TrackMyDrinksDbAdapter.KEY_DRINK_NAME, c.getString(
c.getColumnIndex(TrackMyDrinksDbAdapter.KEY_DRINK_NAME)));
i.putExtra(TrackMyDrinksDbAdapter.KEY_ALCOHOL_VOL, c.getDouble(
c.getColumnIndex(TrackMyDrinksDbAdapter.KEY_ALCOHOL_VOL)));
i.putExtra(TrackMyDrinksDbAdapter.KEY_ALCOHOL_PERC,c.getDouble(
c.getColumnIndex(TrackMyDrinksDbAdapter.KEY_ALCOHOL_PERC)));
i.putExtra(TrackMyDrinksDbAdapter.KEY_DRINK_ID, c.getInt(
c.getColumnIndex(TrackMyDrinksDbAdapter.KEY_DRINK_ID)));
return i;
}
This code is the code that puts a record or updates a record when
necessary.
private void ProcessFavorite(Intent i)
{
Bundle extras = i.getExtras();
TextView tv = (TextView)this.findViewById(R.id.Otheramt);
String FavName =
extras.getString(TrackMyDrinksDbAdapter.KEY_DRINK_NAME);
double AlcVol =
extras.getDouble(TrackMyDrinksDbAdapter.KEY_ALCOHOL_VOL, 0.00);
double AlcPerc =
extras.getDouble(TrackMyDrinksDbAdapter.KEY_ALCOHOL_PERC, 0.00);
long RowId =
extras.getLong(TrackMyDrinksDbAdapter.KEY_DRINK_ID);
double curAlc = AlcVol * AlcPerc;
othercnt++;
mCurrentTime = new Date();
if(mBAC == 0){
mStartTime = (Date) mCurrentTime.clone();
}
if(RowId == -1)
{
mFavDrink.createDrink(FavName, AlcVol, AlcPerc);
}
else
{
mFavDrink.updateDrink(RowId, FavName, AlcVol, AlcPerc);
}
mTotAlcohol = mTotAlcohol + curAlc;
tv.setText(String.valueOf(othercnt));
CalcDisplayBac();
}
This is my entire DB Helper class.
package com.parisj13.trackmydrinks;
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;
class TrackMyDrinksDbAdapter {
public static final String KEY_DRINK_NAME ="drinkname";
public static final String KEY_ALCOHOL_VOL ="volume";
public static final String KEY_ALCOHOL_PERC = "perc";
public static final String KEY_DRINK_ID = "_id";
private static final String TAG = "TrackMyDrinksDbAdapter";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private static final String DATABASE_CREATE =
"create table drink (_id integer primary key autoincrement, "
+ "drinkname text not null, volume double not null, perc
double not null);";
private static final String DATABASE_NAME = "trackmydrinks";
private static final String DATABASE_TABLE = "drink";
private static final int DATABASE_VERSION = 2;
private final Context mCtx;
private static class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int
newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion +
" to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS drink");
onCreate(db);
}
}
/**
* Constructor - takes the context to allow the database to be
* opened/created
*
* @param ctx the Context within which to work
*/
public TrackMyDrinksDbAdapter(Context ctx){
this.mCtx = ctx;
}
/**
* Open the notes database. If it cannot be opened, try to create a
new
* instance of the database. If it cannot be created, throw an
exception to
* signal the failure
*
* @return this (self reference, allowing this to be chained in an
* initialization call)
* @throws SQLException if the database could be neither opened or
created
*/
public TrackMyDrinksDbAdapter open() throws SQLException {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void close() {
mDbHelper.close();
}
/**
* Create a new drink using the name, alcohol volume and alcohol
percent
* provided. If the drink is successfully created return the new
rowId for
* that drink, otherwise return a -1 to indicate failure.
*
*/
public long createDrink(String drinkName, double alcvol, double
alcperc){
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_DRINK_NAME, drinkName);
initialValues.put(KEY_ALCOHOL_VOL, alcvol);
initialValues.put(KEY_ALCOHOL_PERC, alcperc);
return mDb.insert(DATABASE_TABLE, null, initialValues);
}
/**
* Return a Cursor over the list of all drinks in the database
*
* @return Cursor over all notes
*/
public Cursor fetchAllDrinks() {
return mDb.query(DATABASE_TABLE, new String[] {KEY_DRINK_ID,
KEY_DRINK_NAME,
KEY_ALCOHOL_VOL, KEY_ALCOHOL_PERC}, null, null, null,
null,
null);
}
/**
* Return a Cursor positioned at the note that matches the given
rowId
*
* @param rowId id of note to retrieve
* @return Cursor positioned to matching drink, if found
* @throws SQLException if note could not be found/retrieved
*/
public Cursor fetchDrink(long rowId) throws SQLException {
Cursor mCursor =
mDb.query(true, DATABASE_TABLE, new String[] {KEY_DRINK_ID,
KEY_DRINK_NAME, KEY_ALCOHOL_VOL, KEY_ALCOHOL_PERC},
KEY_DRINK_ID + "=" + rowId, null,
null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
/**
* Update the note using the details provided. The note to be
updated is
* specified using the rowId, and it is altered to use the title and
body
* values passed in
*
* @param rowId id of drink to update
* @param name value to set drink name to
* @param alcvol value to set alcohol volume to
* @param alcperc value to set alcohol percent to
* @return true if the note was successfully updated, false
otherwise
*/
public boolean updateDrink(long rowId, String name, double alcvol,
double alcperc) {
ContentValues args = new ContentValues();
args.put(KEY_DRINK_NAME, name);
args.put(KEY_ALCOHOL_VOL, alcvol);
args.put(KEY_ALCOHOL_PERC, alcperc);
return mDb.update(DATABASE_TABLE, args, KEY_DRINK_ID + "=" +
rowId, null) > 0;
}
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---