Question

I am working on a simple app for myself that posts information to a database. I'm really new at this and tried to follow some tutorials along with other tidbits to assemble this.

There is a home screen that gets to the second screen which has a button (add item). Onclicking, this is supposed to build/update the database. I get a crash when I enter that second screen before anything happens. I tried to put in breakpoints to debug but I can't even to get anywhere. It doesn't pause at my breakpoints (or doesn't seem to) so I can't see what's going on

Can anyone point in the right direction for debugging/fixing this?

This is my 2nd screen code - java

public class AddItem extends Activity {

    private final String TAG = "Main Activity";
    View view;
    SQLiteDatabase db; 
    DbPrice dbprice ; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_item); 
        Log.i(TAG, "OnCreate"); 
        dbprice = new DbPrice(this);
        db = dbprice.getWritableDatabase(); 

        Button addButton = (Button) findViewById(R.id.addNew);
        addButton.setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {
                String subcat,item,store,extra;
                Integer day,month,year,price,quantity,weight,volume;
                Boolean sale;

                DatePicker datePicker1 = (DatePicker) findViewById(R.id.datePicker1);
                AutoCompleteTextView autoCompleteSubCat = (AutoCompleteTextView) findViewById(R.id.autoCompleteSubCat);
                EditText editItem = (EditText) findViewById(R.id.editItem);
                EditText editPrice = (EditText) findViewById(R.id.editPrice);
                EditText editQuantity = (EditText) findViewById(R.id.editQuantity); 
                EditText editWeight = (EditText) findViewById(R.id.editWeight);
                EditText editVolume = (EditText) findViewById(R.id.editVolume);
                CheckBox checkSale = (CheckBox) findViewById(R.id.checkSale);
                AutoCompleteTextView autoCompleteStore = (AutoCompleteTextView) findViewById(R.id.autoCompleteStore);
                EditText editExtra = (EditText) findViewById(R.id.editExtra);

                day = datePicker1.getDayOfMonth();
                month = datePicker1.getMonth();
                year = datePicker1.getYear();
                subcat = autoCompleteSubCat.getText().toString();
                item = editItem.getText().toString();
                extra = editExtra.getText().toString();
                price = Integer.parseInt(editPrice.getText().toString());
                quantity = Integer.parseInt(editQuantity.getText().toString());
                weight = Integer.parseInt(editWeight.getText().toString());
                volume = Integer.parseInt(editVolume.getText().toString());
                // sale = checkSale.isChecked(); 
                store = autoCompleteStore.getText().toString();


                ContentValues cv = new ContentValues();
                cv.put(DbPrice.SUBCAT, subcat);
                cv.put(DbPrice.ITEM, item);
                cv.put(DbPrice.EXTRA, extra);
                cv.put(DbPrice.PRICE, price);
                cv.put(DbPrice.QUANTITY, quantity);
                cv.put(DbPrice.WEIGHT, weight);
                cv.put(DbPrice.VOLUME, volume);
                cv.put(DbPrice.SALE, sale);
                cv.put(DbPrice.STORE, store);

                db.insert(DbPrice.TABLE_NAME, null, cv);
            }
        });
    }


    @Override 
    public void onStart() {
        super.onStart();
        Log.i(TAG, "OnStart");
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.i(TAG, "OnResume");
    }

    public void OnPause() {
        super.onPause();
        Log.i(TAG,"OnPause");
    }

    public void OnStop() {
        super.onStart();
        Log.i(TAG, "OnStop"); 
    }

    public void OnDestroy() {
        super.onDestroy();
        Log.i(TAG, "OnDestroy");
    }

    public void addNewItem (View v) {
        Log.i(TAG, "Starting New Activity"); 
        Intent intent = new Intent (this, AllItems.class);
        startActivity(intent);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.add_item, menu);
        return true;
    }
}

The database class java is below. I don't know if you need the xml too but I included the catlog.

public class DbPrice extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "data";
    public static final String TABLE_NAME = "price_table";
    public static final String C_ID = "_id";
    public static final String DAY = "day";  
    public static final String MONTH = "month";  
    public static final String YEAR = "day";  
    public static final String SUBCAT = "subcategory";
    public static final String ITEM = "item";
    public static final String PRICE = "price";  
    public static final String QUANTITY = "quantity"; 
    public static final String WEIGHT = "weight"; 
    public static final String VOLUME = "volume"; 
    public static final String SALE = "sale";
    public static final String STORE = "store";
    public static final String EXTRA = "extra";
    public static final int VERSION = 1; 

    private final String createDb = "create table if not exists " + TABLE_NAME+ " ( "
        + C_ID + " integer primary key autoincrement, "
        + DAY + " text, "
        + MONTH + " text, "
        + YEAR + " text, "
        + SUBCAT + " text, "
        + ITEM + " text, "
        + PRICE + " text, "
        + QUANTITY + " text, "
        + WEIGHT + " text, "
        + VOLUME + " text, "
        + SALE + " text, "
        + STORE + " text, "
        + EXTRA + " text) ";

    public DbPrice(Context context) {
        super(context, DATABASE_NAME, null, VERSION);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(createDb); 
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldversion, int newversion) {
        db.execSQL("drop table " + TABLE_NAME);

    }

}

CATLOG BELOW

12-29 01:27:20.834: I/Main Activity(1362): OnCreate
12-29 01:27:21.044: E/SQLiteLog(1362): (1) duplicate column name: day
12-29 01:27:21.054: D/AndroidRuntime(1362): Shutting down VM
12-29 01:27:21.064: W/dalvikvm(1362): threadid=1: thread exiting with uncaught exception (group=0xb4b11b90)
12-29 01:27:21.194: E/AndroidRuntime(1362): FATAL EXCEPTION: main
12-29 01:27:21.194: E/AndroidRuntime(1362): Process: com.unsuccessfulstudent.grocerypricehistory, PID: 1362
12-29 01:27:21.194: E/AndroidRuntime(1362): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.unsuccessfulstudent.grocerypricehistory/com.unsuccessfulstudent.grocerypricehistory.AddItem}: android.database.sqlite.SQLiteException: duplicate column name: day (code 1): , while compiling: create table if not exists price_table ( _id integer primary key autoincrement, day text, month text, day text, subcategory text, item text, price text, quantity text, weight text, volume text, sale text, store text, extra text)
12-29 01:27:21.194: E/AndroidRuntime(1362):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
12-29 01:27:21.194: E/AndroidRuntime(1362):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
12-29 01:27:21.194: E/AndroidRuntime(1362):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
12-29 01:27:21.194: E/AndroidRuntime(1362):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
12-29 01:27:21.194: E/AndroidRuntime(1362):     at android.os.Handler.dispatchMessage(Handler.java:102)
12-29 01:27:21.194: E/AndroidRuntime(1362):     at android.os.Looper.loop(Looper.java:137)
12-29 01:27:21.194: E/AndroidRuntime(1362):     at android.app.ActivityThread.main(ActivityThread.java:4998)
12-29 01:27:21.194: E/AndroidRuntime(1362):     at java.lang.reflect.Method.invokeNative(Native Method)
12-29 01:27:21.194: E/AndroidRuntime(1362):     at java.lang.reflect.Method.invoke(Method.java:515)
12-29 01:27:21.194: E/AndroidRuntime(1362):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
12-29 01:27:21.194: E/AndroidRuntime(1362):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
12-29 01:27:21.194: E/AndroidRuntime(1362):     at dalvik.system.NativeStart.main(Native Method)
12-29 01:27:21.194: E/AndroidRuntime(1362): Caused by: android.database.sqlite.SQLiteException: duplicate column name: day (code 1): , while compiling: create table if not exists price_table ( _id integer primary key autoincrement, day text, month text, day text, subcategory text, item text, price text, quantity text, weight text, volume text, sale text, store text, extra text)
12-29 01:27:21.194: E/AndroidRuntime(1362):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
12-29 01:27:21.194: E/AndroidRuntime(1362):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
12-29 01:27:21.194: E/AndroidRuntime(1362):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
12-29 01:27:21.194: E/AndroidRuntime(1362):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
12-29 01:27:21.194: E/AndroidRuntime(1362):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
12-29 01:27:21.194: E/AndroidRuntime(1362):     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
12-29 01:27:21.194: E/AndroidRuntime(1362):     at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1672)
12-29 01:27:21.194: E/AndroidRuntime(1362):     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1603)
12-29 01:27:21.194: E/AndroidRuntime(1362):     at com.unsuccessfulstudent.grocerypricehistory.DbPrice.onCreate(DbPrice.java:49)
12-29 01:27:21.194: E/AndroidRuntime(1362):     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
12-29 01:27:21.194: E/AndroidRuntime(1362):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
12-29 01:27:21.194: E/AndroidRuntime(1362):     at com.unsuccessfulstudent.grocerypricehistory.AddItem.onCreate(AddItem.java:31)
12-29 01:27:21.194: E/AndroidRuntime(1362):     at android.app.Activity.performCreate(Activity.java:5243)
12-29 01:27:21.194: E/AndroidRuntime(1362):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-29 01:27:21.194: E/AndroidRuntime(1362):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
12-29 01:27:21.194: E/AndroidRuntime(1362):     ... 11 more
12-29 01:27:24.554: I/Process(1362): Sending signal. PID: 1362 SIG: 9
Was it helpful?

Solution

The reason for your app crash is caused by the fact that in your database creation query you are defining the same key twice. Thats why you are getting a duplicate column error.

Both the keys you have,i.e., DAY as well as YEAR have the same value day. You cannot have two columns in a database with the same name. So, to resolve this, all you need to do is change the YEAR definition to-

public static final String YEAR = "year";  

Hope this helps!!!

OTHER TIPS

You use two same columns

  1. DAY = "day";
  2. YEAR = "day";

Code:

create table if not exists price_table 
(
     _id integer primary key autoincrement, 
     "**day**" text, 
     month text, 
     "**day**" text,... 
)

Whenever there is an Exception, if you see a line FATAL EXCEPTION: main in Logcat, look at the below lines. The line with Unable to start activity ComponentInfo, will give you a clear idea about the cause of exception.
Still if you are confused, look for a line starting with Caused by:.

So here your problem is "duplicate column name: day".
Also you can navigate to the specific line which caused the exception. Just look for your package name. Here in this case,

12-29 01:27:21.194: E/AndroidRuntime(1362):     at com.unsuccessfulstudent.grocerypricehistory.DbPrice.onCreate(DbPrice.java:49)
12-29 01:27:21.194: E/AndroidRuntime(1362):     at com.unsuccessfulstudent.grocerypricehistory.AddItem.onCreate(AddItem.java:31)

At the end of the lines, There is corresponding class and line number. Note that these are the lines caused exception, but the cause of exception may be somewhere else. In this case, String YEAR

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top