dimanche 3 janvier 2016

runtime error, app crashes

i didn't have a compile time error but when i execute my app it crashes. there are the code and the logcat

public class MainActivity extends AppCompatActivity {
EditText nbBeer;
EditText nbWhisky;
EditText nbWine;
EditText nbVodka;
DatePicker myDatePicker;
private Calendar calendar;
private int year, month, day;
TextView dateView;
MyDBHandler dbHandler;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    nbBeer = (EditText) findViewById(R.id.nbBeer);
    nbWhisky = (EditText) findViewById(R.id.nbWhisky);
    nbWine = (EditText) findViewById(R.id.nbWine);
    nbVodka = (EditText) findViewById(R.id.nbVodka);

    dateView = (TextView) findViewById(R.id.dateView);
    calendar = Calendar.getInstance();
    year = calendar.get(Calendar.YEAR);
    month = calendar.get(Calendar.MONTH);
    day = calendar.get(Calendar.DAY_OF_MONTH);
    showDate(year, month+1, day);
    dbHandler = new MyDBHandler(this, null, null, 1);

}
@SuppressWarnings("deprecation")
public void setDate(View view) {
    showDialog(999);
    Toast.makeText(getApplicationContext(), "ca", Toast.LENGTH_SHORT)
            .show();
}
@Override
protected Dialog onCreateDialog(int id) {
    // TODO Auto-generated method stub
    if (id == 999) {
        return new DatePickerDialog(this, myDateListener, year, month, day);
    }
    return null;
}
private DatePickerDialog.OnDateSetListener myDateListener = new DatePickerDialog.OnDateSetListener() {

    @Override
    public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub
        // arg1 = year
        // arg2 = month
        // arg3 = day
        showDate(arg1, arg2+1, arg3);
    }
};
private void showDate(int year, int month, int day) {
    dateView.setText(new StringBuilder().append(day).append("/")
            .append(month).append("/").append(year));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

// add a product to the database
public void saveButtonClicked(View view){
    Products product = new Products(nbBeer.getInputType(), dateView.getText().toString(), nbWhisky.getInputType(),nbWine.getInputType(), nbVodka.getInputType());
    dbHandler.addProduct(product);
}

}

Products class  

public class Products {
private int _id ;
private String _date;
private int _beers;
private int _whisky;
private int _wine;
private int _vodka;

public Products(){}

public Products(int beers, String date, int whisky, int wine, int vodka) {
    this._beers = beers;
    this._date = date;
    this._whisky = whisky;
    this._wine = wine;
    this._vodka = vodka;
}

public String get_date() {
    return _date;
}
public int get_beers() {
    return _beers;
}
public int get_whisky() {
    return _whisky;
}
public int get_wine() {
    return _wine;
}
public int get_vodka() {
    return _vodka;
}

}

MyDBHlander class
public class MyDBHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "products.db";
private static final String TABLE_PRODUCTS = "products";
private static final String COLUMN_ID = "id";
private static final String COLUMN_DATE = "date";
private static final String COLUMN_BEERS = "beers";
private static final String COLUMN_WHISKY = "whisky";
private static final String COLUMN_WINE = "wine";
private static final String COLUMN_vodka = "vodka";
public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
    super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
    String query = " CREATE TABLE" + TABLE_PRODUCTS + "(" +
            COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT" +
            COLUMN_DATE + " TEXT" +
            COLUMN_BEERS + "INTEGER" +
            COLUMN_WHISKY + "INTEGER" +
            COLUMN_WINE + "INTEGER" +
            COLUMN_vodka + "INTEGER" +
            ")";
    db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS" + TABLE_PRODUCTS);
    onCreate(db);
}
// add a new row the database
public void addProduct(Products product){
    ContentValues values = new ContentValues();
    values.put(COLUMN_DATE, product.get_date());
    values.put(COLUMN_BEERS, product.get_beers());
    values.put(COLUMN_WHISKY, product.get_whisky());
    values.put(COLUMN_WINE, product.get_wine());
    values.put(COLUMN_vodka, product.get_vodka());
    SQLiteDatabase db = getWritableDatabase();
    db.insert(TABLE_PRODUCTS, null, values);
    db.close();
}
// print out the database as a string
public String databaseToString () {
    String dbString = "";
    SQLiteDatabase db = getWritableDatabase();
    String query = " SELECT * FROM " + TABLE_PRODUCTS + " WHERE 1";
    // cursor points to a location in your results
    Cursor c = db.rawQuery(query, null);
    // move to the first row in the results
    c.moveToFirst();
    while (!c.isAfterLast()) {
        if (c.getString(c.getColumnIndex("date")) != null) {
        dbString += c.getString(c.getColumnIndex("date"));
        dbString += "\n";
        }
    }
    db.close();
    return dbString;
}

}

Logcat

01-03 18:38:55.397 7241-7241/com.example.sony.myapp E/SQLiteLog: (1) near "TABLEproducts": syntax error
01-03 18:38:55.399 7241-7241/com.example.sony.myapp D/AndroidRuntime: Shutting down VM
01-03 18:38:55.399 7241-7241/com.example.sony.myapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.sony.myapp, PID: 7241
java.lang.IllegalStateException: Could not execute method for android:onClick

Aucun commentaire:

Enregistrer un commentaire