How do I store DatePicker Time in a SQLite database? Mine keeps crashing-Collection of common programming errors

I’ve created a simple record keeping application using various resources around the internet. I’m able to successfully store text data – however when I attempt to incorporate a TimePicker I end up crashing my entire app. I was instructed how to add this functionality in a previous post – but when I attempt to add fields for the timepicker data the entire app closes (and of course does not save the data)

How do I store TimePicker Data in my simple record keeping app?

I was instructed (by the user above) to use the following:

CREATE TABLE ...... dtField date, tmpName Text.....

use following for saving date as text

//sample date format – 2013-03-21 13:12:00

android.text.format.DateFormat.format("yyyy-MM-dd hh:mm:ss", dtDate.getTime())

The first half of which I’ve implemented (I believe). The second half I’m not too sure how to implement correctly (the first thing I need help with), and my app is force closing as well after making these changes (the second thing I need help with.)

Any help resolving this issue is greatly appreciated! (I’m a bit of a noob – so the more detailed the instructions – the better!)

Thanks in advance,

Amani Swann

P.S.

I updated the source code below with Robby Pond’s suggested:

Replace

EditText timeEt with

TimePicker timeEt

but I’m im still unable to run the code shown below.

Can someone take a look at the logcat or problems log and let me know if you can tell what is causing the issue at this point? The suggestion by Robby Pond was helpful but I have a deeper issue with the (current) source code below.

P.S.

I know the error cannot be resolved to a type usually indicates there is a class missing or perhaps an XML issue but the error indicates ‘TimePicker cannot be resolved to a type’ however I don’t have a TimePicker.Java – I just want to use the timepicker buttons coded in the XML below.

XML: DATA INPUT






  

  







    

    








    

    


 

 

 






  
 
 

JAVA: DATA INPUT

package com.nfc.linkingmanager;


import android.app.Activity;
import android.app.AlertDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class AddEditCountry extends Activity {

 private long rowID; 
 private EditText nameEt;
 private EditText capEt;
 private EditText codeEt;
 private TimePicker timeEt;

   @Override
   public void onCreate(Bundle savedInstanceState) 
   {
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.add_country);

      nameEt = (EditText) findViewById(R.id.nameEdit);
      capEt = (EditText) findViewById(R.id.capEdit);
      codeEt = (EditText) findViewById(R.id.codeEdit);
      timeEt = (TimePicker) findViewById(R.id.timeEdit);


      Bundle extras = getIntent().getExtras(); 

      if (extras != null)
      {
         rowID = extras.getLong("row_id");
         nameEt.setText(extras.getString("name"));  
         capEt.setText(extras.getString("cap"));  
         codeEt.setText(extras.getString("code"));  
         timeEt.setText(extras.getString("time"));  
      }

      Button saveButton =(Button) findViewById(R.id.saveBtn);
      saveButton.setOnClickListener(new OnClickListener() {

          public void onClick(View v) 
          {
             if (nameEt.getText().length() != 0)
             {
                AsyncTask saveContactTask = 
                   new AsyncTask() 
                   {
                      @Override
                      protected Object doInBackground(Object... params) 
                      {
                         saveContact();
                         return null;
                      }

                      @Override
                      protected void onPostExecute(Object result) 
                      {
                         finish();
                      }
                   }; 

                saveContactTask.execute((Object[]) null); 
             }

             else
             {
                AlertDialog.Builder alert = new AlertDialog.Builder(AddEditCountry.this);
                alert.setTitle(R.string.errorTitle); 
                alert.setMessage(R.string.errorMessage);
                alert.setPositiveButton(R.string.errorButton, null); 
                alert.show();
             }
          } 
     });
   }

   private void saveContact() 
   {
      DatabaseConnector dbConnector = new DatabaseConnector(this);

      if (getIntent().getExtras() == null)
      {
          dbConnector.insertContact(nameEt.getText().toString(),
                  capEt.getText().toString(),
                  timeEt.getText().toString(),
                  codeEt.getText().toString());
      }
      else
      {
         dbConnector.updateContact(rowID,
            nameEt.getText().toString(),
            capEt.getText().toString(),
            timeEt.getText().toString(),
            codeEt.getText().toString());
      }
   }
}

XML: DATA OUTPUT





            
     
              
  

           
              
              
  

           
              
              
  
           
              
              
  
            
              
              
  

DATA OUTPUT: JAVA

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;

public class ViewCountry extends Activity {

   private long rowID;
   private TextView nameTv;
   private TextView capTv;
   private TextView codeTv; 
   private TextView timeTv; 

   @Override
   public void onCreate(Bundle savedInstanceState) 
   {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.view_country);

      setUpViews();
      Bundle extras = getIntent().getExtras();
      rowID = extras.getLong(CountryList.ROW_ID); 
   }

   private void setUpViews() {
       nameTv = (TextView) findViewById(R.id.nameText);
       capTv = (TextView) findViewById(R.id.capText);
       timeTv = (TextView) findViewById(R.id.timeText);
       codeTv = (TextView) findViewById(R.id.codeText);
   }

   @Override
   protected void onResume()
   {
      super.onResume();
      new LoadContacts().execute(rowID);
   } 

   private class LoadContacts extends AsyncTask 
   {
      DatabaseConnector dbConnector = new DatabaseConnector(ViewCountry.this);

      @Override
      protected Cursor doInBackground(Long... params)
      {
         dbConnector.open();
         return dbConnector.getOneContact(params[0]);
      } 

      @Override
      protected void onPostExecute(Cursor result)
      {
         super.onPostExecute(result);

         result.moveToFirst();
         // get the column index for each data item
         int nameIndex = result.getColumnIndex("name");
         int capIndex = result.getColumnIndex("cap");
         int codeIndex = result.getColumnIndex("code");
         int timeIndex = result.getColumnIndex("time");

         nameTv.setText(result.getString(nameIndex));
         capTv.setText(result.getString(capIndex));
         timeTv.setText(result.getString(timeIndex));
         codeTv.setText(result.getString(codeIndex));

         result.close();
         dbConnector.close();
      }
   } 


   @Override
   public boolean onCreateOptionsMenu(Menu menu) 
   {
      super.onCreateOptionsMenu(menu);
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.view_country_menu, menu);
      return true;
   }

   @Override
   public boolean onOptionsItemSelected(MenuItem item) 
   {
      switch (item.getItemId())
      {
         case R.id.editItem:
            Intent addEditContact =
               new Intent(this, AddEditCountry.class);

            addEditContact.putExtra(CountryList.ROW_ID, rowID);
            addEditContact.putExtra("name", nameTv.getText());
            addEditContact.putExtra("cap", capTv.getText());
            addEditContact.putExtra("time", timeTv.getText());
            addEditContact.putExtra("code", codeTv.getText());
            startActivity(addEditContact); 
            return true;

         case R.id.deleteItem:
            deleteContact();
            return true;

         default:
            return super.onOptionsItemSelected(item);
      } 
   }

   private void deleteContact()
   {

      AlertDialog.Builder alert = new AlertDialog.Builder(ViewCountry.this);

      alert.setTitle(R.string.confirmTitle); 
      alert.setMessage(R.string.confirmMessage); 

      alert.setPositiveButton(R.string.delete_btn,
         new DialogInterface.OnClickListener()
         {
            public void onClick(DialogInterface dialog, int button)
            {
               final DatabaseConnector dbConnector = 
                  new DatabaseConnector(ViewCountry.this);

               AsyncTask deleteTask =
                  new AsyncTask()
                  {
                     @Override
                     protected Object doInBackground(Long... params)
                     {
                        dbConnector.deleteContact(params[0]); 
                        return null;
                     } 

                     @Override
                     protected void onPostExecute(Object result)
                     {
                        finish(); 
                     }
                  };

               deleteTask.execute(new Long[] { rowID });               
            }
         }
      );

      alert.setNegativeButton(R.string.cancel_btn, null).show();
   }
}

DATABASE CONNECTOR JAVA:

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;


public class DatabaseConnector {

private static final String DB_NAME = "WorldCountries";
private SQLiteDatabase database;
private DatabaseOpenHelper dbOpenHelper;

public DatabaseConnector(Context context) {
    dbOpenHelper = new DatabaseOpenHelper(context, DB_NAME, null, 1);
}

   public void open() throws SQLException 
   {
      //open database in reading/writing mode
      database = dbOpenHelper.getWritableDatabase();
   } 

   public void close() 
   {
      if (database != null)
         database.close();
   }       

   public void insertContact(String name, String cap, String code, String time) 
           {
              ContentValues newCon = new ContentValues();
              newCon.put("name", name);
              newCon.put("cap", cap);
              newCon.put("time", time);
              newCon.put("code", code);

              open();
              database.insert("country", null, newCon);
              close();
           }


           public void updateContact(long id, String name, String cap,String code, String time) 
           {
              ContentValues editCon = new ContentValues();
              editCon.put("name", name);
              editCon.put("cap", cap);
              editCon.put("time", time);
              editCon.put("code", code);

              open();
              database.update("country", editCon, "_id=" + id, null);
              close();
           }


           public Cursor getAllContacts() 
           {
              return database.query("country", new String[] {"_id", "name"}, 
                 null, null, null, null, "name");
           }

           public Cursor getOneContact(long id) 
           {
              return database.query("country", null, "_id=" + id, null, null, null, null);
           }

           public void deleteContact(long id) 
           {
              open(); 
              database.delete("country", "_id=" + id, null);
              close();
           }
}

DATABASE HELPER JAVA:

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseOpenHelper extends SQLiteOpenHelper {

public DatabaseOpenHelper(Context context, String name,
        CursorFactory factory, int version) {
    super(context, name, factory, version);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String createQuery = "CREATE TABLE country (_id integer primary key autoincrement,name, cap, code, time);";                 
    db.execSQL(createQuery);        
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

}

LOGCAT DATA:

03-21 17:17:24.276: I/Adreno200-EGLSUB(8655): : Format RGBA_8888.
03-21 17:17:24.276: D/memalloc(8655): ion: Mapped buffer base:0x5ca41000 size:614400 offset:0 fd:57
03-21 17:17:24.276: E/(8655): Can't open file for reading
03-21 17:17:24.276: E/(8655): Can't open file for reading
03-21 17:17:24.376: D/memalloc(8655): ion: Mapped buffer base:0x5d12e000 size:614400 offset:0 fd:61
03-21 17:17:26.188: D/Activity(8655): Activity.onPause(), editTextTapSensorList size: 0
03-21 17:17:26.268: I/Adreno200-EGLSUB(8655): : Format  RGBA_8888.
03-21 17:17:26.278: D/memalloc(8655): ion: Mapped buffer base:0x5d4ce000 size:614400  offset:0 fd:68
03-21 17:17:26.318: D/memalloc(8655): ion: Mapped buffer base:0x5d937000 size:614400 offset:0 fd:72
03-21 17:17:26.328: D/memalloc(8655): ion: Unmapping buffer  base:0x5ca41000 size:614400
03-21 17:17:26.328: D/memalloc(8655): ion: Unmapping buffer  base:0x5d12e000 size:614400
03-21 17:17:26.468: D/Activity(8655): Activity.onPause(), editTextTapSensorList size: 0
03-21 17:17:26.549: D/memalloc(8655): ion: Mapped buffer base:0x5c929000 size:614400 offset:0 fd:54
03-21 17:17:26.619: W/IInputConnectionWrapper(8655): getExtractedText on inactive  InputConnection
03-21 17:17:26.639: W/IInputConnectionWrapper(8655): getExtractedText on inactive InputConnection
03-21 17:17:48.322: D/Activity(8655): Activity.onPause(), editTextTapSensorList size: 0
03-21 17:17:48.342: W/dalvikvm(8655): threadid=1: thread exiting with uncaught exception (group=0x410889d8)
03-21 17:17:48.352: E/AndroidRuntime(8655): FATAL EXCEPTION: main
03-21 17:17:48.352: E/AndroidRuntime(8655): java.lang.Error: Unresolved compilation  problems: 
03-21 17:17:48.352: E/AndroidRuntime(8655):     TimePicker cannot be resolved to a type
03-21 17:17:48.352: E/AndroidRuntime(8655):     TimePicker cannot be resolved to a type
03-21 17:17:48.352: E/AndroidRuntime(8655):     TimePicker cannot be resolved to a type
03-21 17:17:48.352: E/AndroidRuntime(8655):     timeEdit cannot be resolved or is not a field
03-21 17:17:48.352: E/AndroidRuntime(8655):     TimePicker cannot be resolved to a  type
03-21 17:17:48.352: E/AndroidRuntime(8655):     TimePicker cannot be resolved to a  type
03-21 17:17:48.352: E/AndroidRuntime(8655):     TimePicker cannot be resolved to a  type
03-21 17:17:48.352: E/AndroidRuntime(8655):     at  com.nfc.linkingmanager.AddEditCountry.(AddEditCountry.java:19)
03-21 17:17:48.352: E/AndroidRuntime(8655):     at java.lang.Class.newInstanceImpl(Native Method)
03-21 17:17:48.352: E/AndroidRuntime(8655):     at  java.lang.Class.newInstance(Class.java:1319)
03-21 17:17:48.352: E/AndroidRuntime(8655):     at  android.app.Instrumentation.newActivity(Instrumentation.java:1025)
03-21 17:17:48.352: E/AndroidRuntime(8655):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1875)
03-21 17:17:48.352: E/AndroidRuntime(8655):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1985)
03-21 17:17:48.352: E/AndroidRuntime(8655):     at android.app.ActivityThread.access$600(ActivityThread.java:127)
03-21 17:17:48.352: E/AndroidRuntime(8655):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1151)
03-21 17:17:48.352: E/AndroidRuntime(8655):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-21 17:17:48.352: E/AndroidRuntime(8655):     at android.os.Looper.loop(Looper.java:137)
03-21 17:17:48.352: E/AndroidRuntime(8655):     at android.app.ActivityThread.main(ActivityThread.java:4477)
03-21 17:17:48.352: E/AndroidRuntime(8655):     at  java.lang.reflect.Method.invokeNative(Native Method)
03-21 17:17:48.352: E/AndroidRuntime(8655):     at java.lang.reflect.Method.invoke(Method.java:511)
03-21 17:17:48.352: E/AndroidRuntime(8655):     at  com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:788)
03-21 17:17:48.352: E/AndroidRuntime(8655):     at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
03-21 17:17:48.352: E/AndroidRuntime(8655):     at  dalvik.system.NativeStart.main(Native Method)

PROBLEMS:

    Description Resource    Path    Location    Type
user3SettingsSave cannot be resolved or is not a field  User3Settings.java  /NFC Linking Manager/src/com/nfc/linkingmanager line 35 Java Problem
The import android.content.Context is never used    AppActivity.java    /NFC Linking Manager/src/com/nfc/linkingmanager line 5  Java Problem
The import android.view.View.OnClickListener is never used  AppActivity.java    /NFC Linking Manager/src/com/nfc/linkingmanager line 13 Java Problem
The value of the field AppActivity.button1 is not used  AppActivity.java    /NFC Linking Manager/src/com/nfc/linkingmanager line 18 Java Problem
The value of the field AppActivity.button2 is not used  AppActivity.java    /NFC Linking Manager/src/com/nfc/linkingmanager line 18 Java Problem
user3Tap cannot be resolved to a type   User3Settings.java  /NFC Linking Manager/src/com/nfc/linkingmanager line 40 Java Problem
The value of the field AppActivity.button3 is not used  AppActivity.java    /NFC Linking Manager/src/com/nfc/linkingmanager line 18 Java Problem
TimePicker cannot be resolved to a type AddEditCountry.java /NFC Linking Manager/src/com/nfc/linkingmanager line 19 Java Problem
TimePicker cannot be resolved to a type AddEditCountry.java /NFC Linking Manager/src/com/nfc/linkingmanager line 30 Java Problem
TimePicker cannot be resolved to a type AddEditCountry.java /NFC Linking Manager/src/com/nfc/linkingmanager line 30 Java Problem
The method deactivate() from the type Cursor is deprecated  CountryList.java    /NFC Linking Manager/src/com/nfc/linkingmanager line 52 Java Problem
The constructor SimpleCursorAdapter(Context, int, Cursor, String[], int[]) is deprecated    CountryList.java    /NFC Linking Manager/src/com/nfc/linkingmanager line 33 Java Problem
The import android.app.AlertDialog is never used    User1.java  /NFC Linking Manager/src/com/nfc/linkingmanager line 4  Java Problem
The import android.view.View.OnClickListener is never used  User1.java  /NFC Linking Manager/src/com/nfc/linkingmanager line 12 Java Problem
The method deactivate() from the type Cursor is deprecated  NewCore.java    /NFC Linking Manager/src/com/nfc/linkingmanager line 54 Java Problem
The constructor SimpleCursorAdapter(Context, int, Cursor, String[], int[]) is deprecated    NewCore.java    /NFC Linking Manager/src/com/nfc/linkingmanager line 35 Java Problem
The import android.content.DialogInterface is never used    User1.java  /NFC Linking Manager/src/com/nfc/linkingmanager line 6  Java Problem
The import android.widget.Button is never used  Link.java   /NFC Linking Manager/src/com/nfc/linkingmanager line 5  Java Problem
The import android.content.Intent is never used Link.java   /NFC Linking Manager/src/com/nfc/linkingmanager line 6  Java Problem
The import android.view.View.OnClickListener is never used  User1Settings.java  /NFC Linking Manager/src/com/nfc/linkingmanager line 10 Java Problem
The import android.view.View is never used  Link.java   /NFC Linking Manager/src/com/nfc/linkingmanager line 7  Java Problem
The import android.view.View.OnClickListener is never used  Link.java   /NFC Linking Manager/src/com/nfc/linkingmanager line 8  Java Problem
TimePicker cannot be resolved to a type AddEditCountry.java /NFC Linking Manager/src/com/nfc/linkingmanager line 99 Java Problem
TimePicker cannot be resolved to a type AddEditCountry.java /NFC Linking Manager/src/com/nfc/linkingmanager line 91 Java Problem
TimePicker cannot be resolved to a type AddEditCountry.java /NFC Linking Manager/src/com/nfc/linkingmanager line 41 Java Problem
timeEdit cannot be resolved or is not a field   AddEditCountry.java /NFC Linking Manager/src/com/nfc/linkingmanager line 30 Java Problem
The method showDialog(int) from the type Activity is deprecated User2Settings.java   /NFC Linking Manager/src/com/nfc/linkingmanager    line 37 Java Problem
  1. The timeEt field should be a TimePicker. You are getting that ClassCastException because in your activity you declared it as an EditText.

    Replace

    EditText timeEt
    

    with

    TimePicker timeEt