0

所以我正在尝试创建一个应用程序,允许用户将字符串放入 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();


                    }

}

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

4

1 回答 1

0

您应该考虑使用内容提供程序和游标加载程序,这有点工作,但由于几个原因,它可能是能够共享数据的最佳方式,请参阅

对于我对内容提供者的看法,请参阅Android:使用 SQLiteCursorLoader 还是实现 ContentProvider 更好?

这些链接也可能有助于如何在 Android内容提供程序中使用加载器第 1 部分

希望这可以帮助

于 2013-07-31T03:08:01.310 回答