有谁能告诉我这个logcat要求的是什么? 现在我试图在SQlite android中创建,读取,更新,删除字符串数据,但是我得到了一些NumberFormatException,如何解决这个问题?
建议 谢谢你宝贵的时间!..
logcat的
05-09 11:53:21.660: E/AndroidRuntime(854): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sqlite_sample/com.example.sqlite_sample.MainActivity}: java.lang.NumberFormatException: unable to parse 'null' as integer
05-09 11:53:21.660: E/AndroidRuntime(854): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-09 11:53:21.660: E/AndroidRuntime(854): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-09 11:53:21.660: E/AndroidRuntime(854): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-09 11:53:21.660: E/AndroidRuntime(854): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-09 11:53:21.660: E/AndroidRuntime(854): at android.os.Handler.dispatchMessage(Handler.java:99)
05-09 11:53:21.660: E/AndroidRuntime(854): at android.os.Looper.loop(Looper.java:123)
05-09 11:53:21.660: E/AndroidRuntime(854): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-09 11:53:21.660: E/AndroidRuntime(854): at java.lang.reflect.Method.invokeNative(Native Method)
05-09 11:53:21.660: E/AndroidRuntime(854): at java.lang.reflect.Method.invoke(Method.java:507)
05-09 11:53:21.660: E/AndroidRuntime(854): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-09 11:53:21.660: E/AndroidRuntime(854): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-09 11:53:21.660: E/AndroidRuntime(854): at dalvik.system.NativeStart.main(Native Method)
05-09 11:53:21.660: E/AndroidRuntime(854): Caused by: java.lang.NumberFormatException: unable to parse 'null' as integer
05-09 11:53:21.660: E/AndroidRuntime(854): at java.lang.Integer.parseInt(Integer.java:356)
05-09 11:53:21.660: E/AndroidRuntime(854): at java.lang.Integer.parseInt(Integer.java:332)
05-09 11:53:21.660: E/AndroidRuntime(854): at com.example.sqlite_sample.DatabaseHandler.getAllContacts(DatabaseHandler.java:98)
05-09 11:53:21.660: E/AndroidRuntime(854): at com.example.sqlite_sample.MainActivity.onCreate(MainActivity.java:40)
05-09 11:53:21.660: E/AndroidRuntime(854): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-09 11:53:21.660: E/AndroidRuntime(854): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
05-09 11:53:21.660: E/AndroidRuntime(854): ... 11 more
MainActivity.java
public class MainActivity extends Activity
{
String str_value = "Contact 1";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DatabaseHandler db = new DatabaseHandler(this);
// Inserting Contacts
Log.d("Insert: ", "Inserting ..");
db.addContact(new Contact(str_value));
db.addContact(new Contact("Contact 2"));
db.addContact(new Contact("Contact 3"));
// Reading all contacts
Log.d(">>--Reading: ", "Reading all contacts..");
List<Contact> contacts = db.getAllContacts();
for (Contact cn : contacts) {
String log = "Id: "+cn.getID()+" ,Name: " + cn.getName() ;
// Writing Contacts to log
Log.d("Name: ", log);
}
}}
Contact.java
public class Contact {
//private variables
int _id;
String _name;
// Empty constructor
public Contact(){
}
// constructor
public Contact(int id, String name){
this._id = id;
this._name = name;
}
// constructor
public Contact(String name){
this._name = name;
}
// getting ID
public int getID(){
return this._id;
}
// setting id
public void setID(int id){
this._id = id;
}
// getting name
public String getName(){
return this._name;
}
// setting name
public void setName(String name){
this._name = name;
}}
DadabaseHandler.java
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 = "contactsManager";
// Contacts table name
private static final String TABLE_CONTACTS = "contacts";
// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"+ ")";
db.execSQL(CREATE_CONTACTS_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_CONTACTS);
// Create tables again
onCreate(db);
}
// Adding new contact
void addContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, contact.getName()); // Contact Name
// Inserting Row
db.insert(TABLE_CONTACTS, null, values);
db.close(); // Closing database connection
}
// Getting single contact
Contact getContact(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID,
KEY_NAME }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
cursor.getString(1));
// return contact
return contact;
}
// Getting All Contacts
public List<Contact> getAllContacts() {
List<Contact> contactList = new ArrayList<Contact>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_CONTACTS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Contact contact = new Contact();
contact.setID(Integer.parseInt(cursor.getString(0)));
contact.setName(cursor.getString(1));
// Adding contact to list
contactList.add(contact);
} while (cursor.moveToNext());
}
return contactList;
}
// Updating single contact
public int updateContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, contact.getName());
// updating row
return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
new String[] { String.valueOf(contact.getID()) });
}
// Deleting single contact
public void deleteContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_CONTACTS, KEY_ID + " = ?",
new String[] { String.valueOf(contact.getID()) });
db.close();
}
// Getting contacts Count
public int getContactsCount() {
String countQuery = "SELECT * FROM " + TABLE_CONTACTS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}}
答案 0 :(得分:2)
试试这个
contact.setID(cursor.getInt(0));
而不是
contact.setID(Integer.parseInt(cursor.getString(0)));
在您的getContact(int id)
和getAllContacts()
方法
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"+ ")"; // remove , after TEXT
更正您的SQL
创建表格命令,如
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT"+ ")";