我尝试将 SQLite 添加到我的项目中,但是当我尝试在模拟器上运行我的应用程序时,它会显示这些错误,我对此一无所知!
[2012-10-25 14:16:29 - DeviceMonitor] Adb connection Error:An existing connection was forcibly closed by the remote host
[2012-10-25 14:16:29 - Unexpected error while launching logcat. Try reselecting the device.] An existing connection was forcibly closed by the remote host
java.io.IOException: An existing connection was forcibly closed by the remote host
at sun.nio.ch.SocketDispatcher.read0(Native Method)
at sun.nio.ch.SocketDispatcher.read(Unknown Source)
at sun.nio.ch.IOUtil.readIntoNativeBuffer(Unknown Source)
at sun.nio.ch.IOUtil.read(Unknown Source)
at sun.nio.ch.SocketChannelImpl.read(Unknown Source)
at com.android.ddmlib.AdbHelper.executeRemoteCommand(AdbHelper.java:395)
at com.android.ddmlib.Device.executeShellCommand(Device.java:462)
at com.android.ddmuilib.logcat.LogCatReceiver$1.run(LogCatReceiver.java:109)
at java.lang.Thread.run(Unknown Source)
[2012-10-25 14:16:30 - DeviceMonitor] Connection attempts: 1
我遵循了本教程: http ://www.androidhive.info/2011/11/android-sqlite-database-tutorial/
我的处理程序类:
public class DatabaseHandler extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "PHM";
// Contacts table name
private static final String TABLE_USERS = "Users";
// Users Table Columns names
private static final String KEY_ID = "_id";
private static final String KEY_NAME = "name";
private static final String KEY_PASS = "password";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_USERS_TABLE = "CREATE TABLE " + TABLE_USERS + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
+ KEY_PASS + " TEXT" + ")";
db.execSQL(CREATE_USERS_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERS);
// Create tables again
onCreate(db);
}
public void addUser(User user) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, user.get_name()); // User Name
values.put(KEY_PASS, user.get_pass()); // User Phone Number
// Inserting Row
db.insert(TABLE_USERS, null, values);
db.close(); // Closing database connection
}
// Getting single User
public User getUser(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_USERS, new String[] { KEY_ID,
KEY_NAME, KEY_PASS }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
User user = new User(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2));
return user;
}
public List<User> getAllUsers() {
List<User> contactList = new ArrayList<User>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_USERS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
User user = new User();
user.set_id(Integer.parseInt(cursor.getString(0)));
user.set_name(cursor.getString(1));
user.set_pass(cursor.getString(2));
// Adding contact to list
contactList.add(user);
} while (cursor.moveToNext());
}
// return contact list
return contactList;
}
// Getting users Count
public int getUsersCount() {
String countQuery = "SELECT * FROM " + TABLE_USERS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}
// Updating single contact
public int updateContact(User user) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, user.get_name());
values.put(KEY_PASS, user.get_pass());
// updating row
return db.update(TABLE_USERS, values, KEY_ID + " = ?",
new String[] { String.valueOf(user.get_id()) });
}
// Deleting single user
public void deleteUser(User user) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_USERS, KEY_ID + " = ?",
new String[] { String.valueOf(user.get_id()) });
db.close();
}
}
这是我通过以下命令将其添加到我的 JavaScript 界面的类:
mean db= new mean(this, appView);
appView.addJavascriptInterface(db, "database");
班级:
public class mean implements Cloneable{
private WebView mAppView;
private static DroidGap mGap;
DatabaseHandler db = new DatabaseHandler(mGap);
public mean(DroidGap gap, WebView view)
{
mAppView = view;
mGap = gap;
}
public void AddUser(String name,String pass ){
User user=new User(name,pass);
db.addUser(user);
}
public String getAllUsers(){
String ret="ajab";
List<User> toShow=db.getAllUsers();
for(User a :toShow){
ret+=a.get_name();
}
return ret;
}
public String Generate(){
QRCodeGenerator.getQRCode ("pic.JPG", "Hamed Mahsa Pouya");
return "BEHI";
}
}
在我的 JavaScript 中,我这样调用我的函数:
function AddUser(name,pass){
alert(name+"::::::"+pass);
window.database.AddUser(name,pass);
alert("pass");
}
我应该提一下,有时我会看到第二个警报,但大多数时候我会看到这个异常。
Uncaught Error: Error calling method on NPObject. at file:///android_asset/www/code.js:12