我无法查看数据库中的日志

时间:2013-06-12 01:30:06

标签: java android database sqlite sqliteopenhelper

所以我试图创建一个允许用户将字符串放入SQLite数据库并在另一个活动中查看日志的应用程序。但是当我打开下一个活动时,应用程序会强制关闭并崩溃。这是我从用户将信息放入数据库的活动中的代码

Button ViewLogs = (Button)findViewById(R.id.button1);
    ViewLogs.setOnClickListener(new OnClickListener()
    {

    @Override
    public void onClick(View v) {
        Intent view = new Intent(StartBitching.this, ViewLogs.class);
        startActivity(view);
        // TODO Auto-generated method stub

    }

}

        );


Button MostWanted = (Button)findViewById(R.id.button2);
MostWanted.setOnClickListener(new OnClickListener()
{


    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent most = new Intent(StartBitching.this, MostWanted.class);
        startActivity(most);
    }


});
Button Add = (Button)findViewById(R.id.button3);
Add.setOnClickListener(new OnClickListener()
{

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        EditText txtName = (EditText)findViewById(R.id.editText1);
        EditText txtDate = (EditText)findViewById(R.id.editText2);
        EditText txtSummary = (EditText)findViewById(R.id.editText3);

        DatabaseConnector dc = new DatabaseConnector(null);
        try
        {
            String Name = txtName.getText().toString();
            String Date = txtDate.getText().toString();
            String Summary = txtSummary.getText().toString();

            dc.insertLog(Name, Date, Summary);
            txtName.setText("");
            txtDate.setText("");
            txtSummary.setText("");


    }
        catch(Exception ex)
        {
            txtName.setText(ex.getMessage().toString());

        }

这是我的数据库连接器活动

 public class DatabaseConnector{

private static final String DATABASE_NAME = "Blacklist";
private SQLiteDatabase database; // database object
private DatabaseOpenHelper databaseOpenHelper; // database helper
// public constructor for DatabaseConnector
{
 Context context = null;
// create a new DatabaseOpenHelper
 databaseOpenHelper =
   new DatabaseOpenHelper(context, DATABASE_NAME, null, 1);
} // end DatabaseConnector constructor

    public DatabaseConnector(ViewLogs viewLogs) {
    // TODO Auto-generated constructor stub
}

    // open the database connection
   public void open() throws SQLException
   {
      // create or open a database for reading/writing
      database = databaseOpenHelper.getWritableDatabase();
   } // end method open

// close the database connection
   public void close()
   {
      if (database != null)
         database.close(); // close the database connection
   } // end method close

// inserts a new dog in the database
   public void insertLog(String Name,
      String Date, String Summary)
   {
   try
   {
      ContentValues newLog = new ContentValues();
      newLog.put("Name", Name);
      newLog.put("Date", Date);
      newLog.put("Summary", Summary);
      open(); // open the database
      database.insert("logs", null, newLog);
      close(); // close the database
 }
    catch (Exception e){}
   } // end method insertDog

   public void updateDog(long id, String Name, String Date, String Summary)
   {
      ContentValues editContact = new ContentValues();
      editContact.put("Name", Name);
      editContact.put("Date", Date);
      editContact.put("Summary", Summary);
      open(); // open the database
      database.update("logs", editContact, "_id=" + id, null);
      close(); // close the database
   } // end method updateContact

   // return a Cursor with all contact information in the database
   public Cursor getAllLogs()
   {
      return database.query("logs", new String[] {"_id", "Name", "Date", "Summary"},
         null, null, null, null, "Name");
   } // end method getAllContacts
   // get a Cursor containing all information about the contact specified
   // by the given id
   public Cursor getOneContact(long id)
   {
      return database.query(
         "contacts", null, "_id=" + id, null, null, null, null);
   } // end method getOnContact

   // delete the contact specified by the given String name
   public void deleteLog(long id)
   {
      open(); // open the database
      database.delete("logs", "_id=" + id, null);
      close(); // close the database
   } // end method deleteContact
private class DatabaseOpenHelper extends SQLiteOpenHelper
   {
      // public constructor
      public DatabaseOpenHelper(Context context, String name,
         CursorFactory factory, int version)
      {
         super(context, name, factory, version);
      } // end DatabaseOpenHelper constructor
      // creates the contacts table when the database is created
          @Override
          public void onCreate(SQLiteDatabase db)
          {
         // query to create a new table named dog
         String createQuery = "CREATE TABLE logs" +
            "(_id integer primary key autoincrement," +
            "Name TEXT," +
            "Date TEXT," +
            "Summary TEXT);";

         db.execSQL(createQuery); // execute the query
      } // end method onCreate
      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion,
          int newVersion)
      {
      } // end method onUpgrade
    } // end class DatabaseOpenHelper

    }

以下是应用程序中允许用户查看数据库中所有日志的代码

public class ViewLogs extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_logs);

    DatabaseConnector dc = new DatabaseConnector(this);
    dc.open();
    Cursor c = dc.getAllLogs();
    String[] logs = new String[0];
            if (c !=null)
                    {
                        do{

                    String Name = c.getString(c.getColumnIndex("Name"));
                    String Date = c.getString(c.getColumnIndex("Date"));
                    String Summary = c.getString(c.getColumnIndex("Summary"));

                    TableLayout rl1 =(TableLayout)findViewById(R.id.rel);
                    for( String log: logs)
            {
                TextView lbl = (TextView)findViewById(R.id.texter);
                lbl.setText(log);
                rl1.addView(lbl);

            }



                        }while (c.moveToNext());
                    }








    super.onResume();


                    }

}

我知道这是很多代码,可能很难阅读,但我是编程的新手,如果我能得到任何帮助,我会非常感激

1 个答案:

答案 0 :(得分:0)

你应该看一下使用内容提供商和游标加载器,它有点工作,但由于几个原因,它可能是能够共享数据的最佳方式,参见

我对内容提供商的看法见 Android: What is better, using a SQLiteCursorLoader or implementing a ContentProvider?

这些链接也可能有助How to use Loaders in AndroidContent Providers part 1

希望这有帮助