0

I am new to programming and my app crashes every time I try to get the data from my extras which are stored in SQL.

public class MainActivity extends ListActivity {
    public final static String ID_EXTRA="com.pixelcrunch._ID";
    Cursor model=null;
    CountdownHelper helper=null;
    CountdownAdapter adapter = null;      
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);      
    helper=new CountdownHelper(this);
    model=helper.getAll();
    startManagingCursor(model);
    adapter=new CountdownAdapter(model);
    setListAdapter(adapter);
    @Override
    public void onListItemClick(ListView list, View view,
                              int position, long id) {
        Intent i=new Intent(MainActivity.this, Edit.class);
        i.putExtra(ID_EXTRA, String.valueOf(id));
        startActivity(i);
    }   

and in my receiving Activity I have

public class Edit extends Activity {
CountdownHelper helper=null;
String countdownId=null;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.edit);
    helper=new CountdownHelper(this);
    countdownId=getIntent().getStringExtra(MainActivity.ID_EXTRA);    
    if (countdownId!=null) {
        load();
    }
}
private void load() {
    Cursor c=helper.getById(countdownId);
    c.moveToFirst();    
    mDescription.setText(helper.getDescription(c));
    mDateDisplay.setText(helper.getDate(c));
    mTimeDisplay.setText(helper.getTime(c));            
    c.close();
}   

and finally my SQL I have

class CountdownHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME="countdown.db";
private static final int SCHEMA_VERSION=1;

public CountdownHelper(Context context) {
    super(context, DATABASE_NAME, null, SCHEMA_VERSION);
}     
@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE countdowns (_id INTEGER PRIMARY KEY AUTOINCREMENT, description TEXT, date TEXT, time TEXT);");
}

public Cursor getAll() {
    return(getReadableDatabase()
            .rawQuery("SELECT _id, description, date, time FROM countdowns ORDER BY description",
                    null));
      }

public Cursor getById(String id) {
    String[] args={id};     
    return(getReadableDatabase()
            .rawQuery("SELECT _id, description, date, time, FROM countdowns WHERE _ID=?",
                      args));
  }
public void insert(String description, String date, String time) {
    ContentValues cv=new ContentValues();
    cv.put("description", description);
    cv.put("date", date);
    cv.put("time", time);
    getWritableDatabase().insert("countdowns", "description", cv);
}

public void update(String id, String description, String date, String time) {
    ContentValues cv=new ContentValues();
    String[] args={id};
    cv.put("description", description);
    cv.put("date", date);
    cv.put("time", time);
    getWritableDatabase().update("countdowns", cv, "_ID=?",
                      args);
    }

public String getDescription(Cursor c) {
    return(c.getString(1));
}

public String getDate(Cursor c) {
    return(c.getString(2));
}

public String getTime(Cursor c) {
    return(c.getString(3));
}  

Sorry for the long code, I really cant pinpoint where my problem is. I know if I get rid of the code under the load() and replace it with a toast it works. As I am inexperienced and self taught any criticism would be greatly appreciated. Thanks Logcat:

02-14 19:33:19.798: V/InputMethodManager(23673): focusIn: android.widget.ListView@40eab1e8
02-14 19:33:19.798: V/InputMethodManager(23673): onWindowFocus:    android.widget.ListView@40eab1e8 softInputMode=272 first=true flags=#8010100
02-14 19:33:19.798: V/InputMethodManager(23673): Has been inactive!  Starting fresh
02-14 19:33:19.798: V/InputMethodManager(23673): focusIn: android.widget.ListView@40eab1e8
02-14 19:33:19.798: V/InputMethodManager(23673): checkFocus:  view=android.widget.ListView@40eab1e8 next=android.widget.ListView@40eab1e8 restart=true
02-14 19:33:19.798: V/InputMethodManager(23673): Starting input: view=android.widget.ListView@40eab1e8
02-14 19:33:19.798: V/InputMethodManager(23673): Starting input:   tba=android.view.inputmethod.EditorInfo@423b3230 ic=null
02-14 19:33:19.798: V/InputMethodManager(23673): START INPUT:   android.widget.ListView@40eab1e8 ic=null tba=android.view.inputmethod.EditorInfo@423b3230 initial=true
02-14 19:33:19.803: V/InputMethodManager(23673): Starting input: Bind result=InputBindResult{com.android.internal.view.IInputMethodSession$Stub$Proxy@423b3dd0  com.android.inputmethod.latin/.LatinIME #1180}
02-14 19:33:25.483: W/ResourceType(23673): No package identifier when getting name for resource number 0x00000000
02-14 19:33:25.488: W/ResourceType(23673): No package identifier when getting name for resource number 0x00000000
02-14 19:33:25.503: W/ResourceType(23673): No package identifier when getting name for resource number 0x00000000
02-14 19:33:25.503: W/ResourceType(23673): No package identifier when getting name for resource number 0x00000000
02-14 19:33:25.533: I/SqliteDatabaseCpp(23673): sqlite returned: error code = 1, msg = near "FROM": syntax error, db=/data/data/com.pixelcrunch.crunchtime/databases/countdown.db
02-14 19:33:25.533: D/AndroidRuntime(23673): Shutting down VM
02-14 19:33:25.533: W/dalvikvm(23673): threadid=1: thread exiting with uncaught exception (group=0x40c381f8)
02-14 19:33:25.538: E/AndroidRuntime(23673): FATAL EXCEPTION: main
02-14 19:33:25.538: E/AndroidRuntime(23673): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pixelcrunch.crunchtime/com.pixelcrunch.crunchtime.Edit}: android.database.sqlite.SQLiteException: near "FROM": syntax error: , while compiling: SELECT _id, description, date, time, FROM countdowns WHERE _ID=?
02-14 19:33:25.538: E/AndroidRuntime(23673):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968)
02-14 19:33:25.538: E/AndroidRuntime(23673):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
02-14 19:33:25.538: E/AndroidRuntime(23673):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
02-14 19:33:25.538: E/AndroidRuntime(23673):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
02-14 19:33:25.538: E/AndroidRuntime(23673):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-14 19:33:25.538: E/AndroidRuntime(23673):    at android.os.Looper.loop(Looper.java:137)
02-14 19:33:25.538: E/AndroidRuntime(23673):    at android.app.ActivityThread.main(ActivityThread.java:4507)
02-14 19:33:25.538: E/AndroidRuntime(23673):    at java.lang.reflect.Method.invokeNative(Native Method)
02-14 19:33:25.538: E/AndroidRuntime(23673):    at java.lang.reflect.Method.invoke(Method.java:511)
02-14 19:33:25.538: E/AndroidRuntime(23673):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
02-14 19:33:25.538: E/AndroidRuntime(23673):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
02-14 19:33:25.538: E/AndroidRuntime(23673):    at dalvik.system.NativeStart.main(Native Method)
02-14 19:33:25.538: E/AndroidRuntime(23673): Caused by: android.database.sqlite.SQLiteException: near "FROM": syntax error: , while compiling: SELECT _id, description, date, time, FROM countdowns WHERE _ID=?
02-14 19:33:25.538: E/AndroidRuntime(23673):    at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
02-14 19:33:25.538: E/AndroidRuntime(23673):    at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:68)
02-14 19:33:25.538: E/AndroidRuntime(23673):    at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:143)
02-14 19:33:25.538: E/AndroidRuntime(23673):    at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
02-14 19:33:25.538: E/AndroidRuntime(23673):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:127)
02-14 19:33:25.538: E/AndroidRuntime(23673):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:94)
02-14 19:33:25.538: E/AndroidRuntime(23673):    at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:53)
02-14 19:33:25.538: E/AndroidRuntime(23673):    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
02-14 19:33:25.538: E/AndroidRuntime(23673):    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1673)
02-14 19:33:25.538: E/AndroidRuntime(23673):    at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1647)
02-14 19:33:25.538: E/AndroidRuntime(23673):    at com.pixelcrunch.crunchtime.CountdownHelper.getById(CountdownHelper.java:33)
02-14 19:33:25.538: E/AndroidRuntime(23673):    at com.pixelcrunch.crunchtime.Edit.load(Edit.java:78)
02-14 19:33:25.538: E/AndroidRuntime(23673):    at com.pixelcrunch.crunchtime.Edit.onCreate(Edit.java:68)
02-14 19:33:25.538: E/AndroidRuntime(23673):    at android.app.Activity.performCreate(Activity.java:4465)
02-14 19:33:25.538: E/AndroidRuntime(23673):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
02-14 19:33:25.538: E/AndroidRuntime(23673):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
02-14 19:33:25.538: E/AndroidRuntime(23673):    ... 11 more

if failed at the first:

02-14 19:33:25.483: W/ResourceType(23673): No package identifier when getting name for resource number 0x00000000
Sean Croft
  • 375
  • 1
  • 2
  • 8
  • 1
    The LogCat helps you as it shows you the stacktrace. Please paste it here and point us on the line where the crash happens. Than we will be able to help you. – WarrenFaith Feb 15 '12 at 00:17
  • I think you are mixing things, bundle is different from database data. – kosa Feb 15 '12 at 00:30

1 Answers1

1

As it looks from the exception

02-14 19:33:25.538: E/AndroidRuntime(23673): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pixelcrunch.crunchtime/com.pixelcrunch.crunchtime.Edit}: android.database.sqlite.SQLiteException: near "FROM": syntax error: , while compiling: SELECT _id, description, date, time, FROM countdowns WHERE _ID=?

the line

  .rawQuery("SELECT _id, description, date, time, FROM countdowns WHERE _ID=?",

Should be

  .rawQuery("SELECT _id, description, date, time FROM countdowns WHERE _ID=?",
  //                                            ^removed comma
MByD
  • 135,866
  • 28
  • 264
  • 277
  • Thank you, this was the problem and the app functions as intended. I didn't realize how powerful the logcat was. I simply need to pay more attention. – Sean Croft Feb 15 '12 at 00:58