when I check Checkbox to fave an Item, app crashes, I tried to make some break points to test code and they successfully ran. I don't know where is the problem so here is how I handled the checkbox.(trying to test checked checkbox first, unchecked state later)
I'm new to android development with java basics so any explanation would help, thank you in advance
buttonFavorite = (CheckBox) findViewById(R.id.favButton);
buttonFavorite.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (buttonFavorite.isChecked()){
Uri mNewUri;
ContentValues values = new ContentValues();
values.put(MovieContract.MovieEntry.COLUMN_ID_MOVIE, movieId );
values.put(MovieContract.MovieEntry.COLUMN_TITLE, mTitle);
values.put(MovieContract.MovieEntry.COLUMN_OVERVIEW, mOverview);
values.put(MovieContract.MovieEntry.COLUMN_DATE, mReleaseDate);
values.put(MovieContract.MovieEntry.COLUMN_VOTE, mRate);
values.put(MovieContract.MovieEntry.COLUMN_POSTER, mPoster);
mNewUri = getContentResolver().insert(MovieContract.MovieEntry.CONTENT_URI, values);
Log.v("New insertion", "Successful insertion in URI: " + mNewUri);
Toast.makeText(getApplicationContext(), mTitle + " added to your Favorites" , Toast.LENGTH_SHORT).show();
}
and here are content provider code: Contract class:
public class MovieContract {
public static final String CONTENT_AUTHORITY =
"com.example.geekymind.movieapp.Data";
public static final Uri BASE_CONTENT_URI = Uri.parse("content://" + CONTENT_AUTHORITY);
public static final class MovieEntry implements BaseColumns {
// table name
public static final String TABLE_FMovies = "favorites";
// columns
public static final String _ID = "_id";
public static final String COLUMN_POSTER = "poster_path";
public static final String COLUMN_OVERVIEW = "overview";
public static final String COLUMN_ID_MOVIE ="movie_ID";
public static final String COLUMN_TITLE ="title";
public static final String COLUMN_DATE ="date";
public static final String COLUMN_VOTE ="vote_average";
public static final Uri CONTENT_URI = BASE_CONTENT_URI.buildUpon()
.appendPath(TABLE_FMovies).build();
public static final String CONTENT_DIR_TYPE =
ContentResolver.CURSOR_DIR_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + TABLE_FMovies;
public static final String CONTENT_ITEM_TYPE =
ContentResolver.CURSOR_ITEM_BASE_TYPE +"/" + CONTENT_AUTHORITY + "/" + TABLE_FMovies;
public static Uri buildFlavorsUri(long id){
return ContentUris.withAppendedId(CONTENT_URI, id);
}
}
}
Data base helper classs
public class MovieDBHelper extends SQLiteOpenHelper {
public static final String LOG_TAG = MovieDBHelper.class.getSimpleName();
//name & version
private static final String DATABASE_NAME = "favorites.db";
private static final int DATABASE_VERSION = 300;
public MovieDBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Create the database
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
final String SQL_CREATE_MOVIE_TABLE = "CREATE TABLE " +
MovieContract.MovieEntry.TABLE_FMovies + "(" + MovieContract.MovieEntry._ID +
" INTEGER PRIMARY KEY AUTOINCREMENT, " +
MovieContract.MovieEntry.COLUMN_ID_MOVIE + " INTEGER PRIMARY KEY," +
MovieContract.MovieEntry.COLUMN_TITLE + " TEXT NOT NULL, " +
MovieContract.MovieEntry.COLUMN_OVERVIEW + " TEXT NOT NULL, " +
MovieContract.MovieEntry.COLUMN_DATE + " TEXT NOT NULL, " +
MovieContract.MovieEntry.COLUMN_VOTE + " TEXT NOT NULL, " +
MovieContract.MovieEntry.COLUMN_POSTER +" TEXT NOT NULL);";
sqLiteDatabase.execSQL(SQL_CREATE_MOVIE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
Log.w(LOG_TAG, "Upgrading database from version " + oldVersion + " to " +
newVersion + ". OLD DATA WILL BE DESTROYED");
// Drop the table
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + MovieContract.MovieEntry.TABLE_FMovies);
sqLiteDatabase.execSQL("DELETE FROM SQLITE_SEQUENCE WHERE NAME = '" +
MovieContract.MovieEntry.TABLE_FMovies + "'");
// re-create database
onCreate(sqLiteDatabase);
}
}
insert method in content provider class
public Uri insert(Uri uri, ContentValues values){
final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
Uri returnUri;
switch (sUriMatcher.match(uri)) {
case MOVIE: {
long _id = db.insert(MovieContract.MovieEntry.TABLE_FMovies, null, values);
// insert unless it is already contained in the database
if (_id > 0) {
returnUri = MovieContract.MovieEntry.buildFlavorsUri(_id);
} else {
throw new android.database.SQLException("Failed to insert row into: " + uri);
}
break;
}
default: {
throw new UnsupportedOperationException("Unknown uri: " + uri);
}
}
getContext().getContentResolver().notifyChange(uri, null);
return returnUri;
}
Edit1: I don't know if that could help to figure out the problem, but any way that's how I get data from intent before any insertion.
Intent intent = getIntent();
final String mTitle = intent.getStringExtra("MovieTitle");
final String mRate = Double.toString(intent.getDoubleExtra("voteAverage", 0.0));
final String mReleaseDate = intent.getStringExtra("ReleaseDate");
final String mOverview = intent.getStringExtra("MovieOverview");
final String mPoster = intent.getStringExtra("PosterPath");
movieId = intent.getIntExtra("MovieId", 157336);
Edit2: crash log
01-05 02:25:18.567 17736-17736/com.example.geekymind.movieapp W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x415ffc80)
Aucun commentaire:
Enregistrer un commentaire