我正在尝试在android中开发自己的应用程序,我想将信息更新到DB中,所以这里是我的代码 ///////// DB //////////
// - 更新记录 -
public boolean updateRecords(long columnId, String nAME ,String sURNAME,String dateOfBirth, String homeAddress,String emailNO, String phoneNumber, String city,String type_payment, String type_shipping,String cardNumber,String username, String passwords )
{
ContentValues args = new ContentValues();
args.put(COLUMN_NAME,nAME);
args.put(COLUMN_SURNAME,sURNAME);
args.put(COLUMN_DATE_OF_BIRTH,dateOfBirth);
args.put(COLUMN_ADDRESS,homeAddress);
args.put(COLUMN_EMAIL,emailNO);
args.put(COLUMN_PHONE_NUMBER,phoneNumber);
args.put(COLUMN_CITY,city);
args.put(COLUMN_PTYE_PAYMENT,type_payment);
args.put(COLUMN_SHIPPING_TYPE,type_shipping);
args.put(COLUMN_CARD_NUMBER,cardNumber);
args.put(COLUMN_username,username);
args.put(COLUMN_Password,passwords);
return db.update(USERS_TABLE, args,COLUMN_ID + "= "+ columnId, null) > 0;
}
我的ACTIVITY CLASS就像这样........................
public void onClick(View v) {
if(v.getId() == R.id.btnEditSave){
Log.d("test", "edited");
//get data from form
EditText NAME = (EditText)findViewById(R.id.editTxtname);
EditText SURNAME = (EditText)findViewById(R.id.editTxtSurname);
EditText DateOfBirth = (EditText)findViewById(R.id.editTxtID);
EditText HomeAddress = (EditText)findViewById(R.id.editTxtPAddress);
EditText EmailNO = (EditText)findViewById(R.id.editTxtEmail);
EditText PhoneNumber = (EditText)findViewById(R.id.editTxtNumber);
EditText City = (EditText)findViewById(R.id.editTxtCity);
EditText type_payment = (EditText)findViewById(R.id.editTextspinnerPayment);
EditText type_shipping = (EditText)findViewById(R.id.editTextSpinnerShipping);
EditText CardNumber = (EditText)findViewById(R.id.editTxtCardNumber);
EditText Username = (EditText)findViewById(R.id.editTxtUsername);
EditText Passwords = (EditText)findViewById(R.id.editTxtPassword);
Cursor value = null ;
db.open();
Log.d("test", "opened database");
long columnId = value.getLong(value.getColumnIndex("ID"));
//boolean
if( db.updateRecords(columnId, NAME.getText().toString(),
SURNAME.getText().toString(),
DateOfBirth.getText().toString(),
HomeAddress.getText().toString(),
EmailNO.getText().toString(),
PhoneNumber.getText().toString(),
City.getText().toString(),
type_payment.getText().toString(),
type_shipping.getText().toString(),
CardNumber.getText().toString(),
Username.getText().toString(),
Passwords.getText().toString()));
else
{
Toast.makeText(this,"Failed",Toast.LENGTH_LONG).show();
}
db.close();
我在logcast中遇到错误.......
06-02 16:29:48.518: D/dalvikvm(12573): GC_CONCURRENT freed 6K, 5% free 7179K/7495K, paused 7ms+5ms
06-02 16:30:03.218: D/test(12573): edited
06-02 16:30:03.278: D/test(12573): opened database
06-02 16:30:03.278: D/AndroidRuntime(12573): Shutting down VM
06-02 16:30:03.278: W/dalvikvm(12573): threadid=1: thread exiting with uncaught exception (group=0x40014760)
06-02 16:30:03.320: E/AndroidRuntime(12573): FATAL EXCEPTION: main
06-02 16:30:03.320: E/AndroidRuntime(12573): java.lang.NullPointerException
06-02 16:30:03.320: E/AndroidRuntime(12573): at com.sdc.editpersonalinfo.onClick(editpersonalinfo.java:119)
06-02 16:30:03.320: E/AndroidRuntime(12573): at android.view.View.performClick(View.java:3110)
06-02 16:30:03.320: E/AndroidRuntime(12573): at android.view.View$PerformClick.run(View.java:11928)
06-02 16:30:03.320: E/AndroidRuntime(12573): at android.os.Handler.handleCallback(Handler.java:587)
06-02 16:30:03.320: E/AndroidRuntime(12573): at android.os.Handler.dispatchMessage(Handler.java:92)
06-02 16:30:03.320: E/AndroidRuntime(12573): at android.os.Looper.loop(Looper.java:132)
06-02 16:30:03.320: E/AndroidRuntime(12573): at android.app.ActivityThread.main(ActivityThread.java:4025)
06-02 16:30:03.320: E/AndroidRuntime(12573): at java.lang.reflect.Method.invokeNative(Native Method)
06-02 16:30:03.320: E/AndroidRuntime(12573): at java.lang.reflect.Method.invoke(Method.java:491)
06-02 16:30:03.320: E/AndroidRuntime(12573): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
06-02 16:30:03.320: E/AndroidRuntime(12573): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
06-02 16:30:03.320: E/AndroidRuntime(12573): at dalvik.system.NativeStart.main(Native Method)
06-02 16:35:03.524: I/Process(12573): Sending signal. PID: 12573 SIG: 9
06-02 16:35:05.550: V/TLINE(13716): new: android.text.TextLine@40684488
06-02 16:35:06.151: V/TLINE(13716): new: android.text.TextLine@406894b0
答案 0 :(得分:1)
这里你将“value”初始化为null然后使用它,所以我认为你的NullPointerException:
Cursor value = null ;
db.open();
Log.d("test", "opened database");
long columnId = value.getLong(value.getColumnIndex("ID"));
如果你看一下这个教程,也许这是一个好主意:
http://developer.android.com/resources/tutorials/notepad/index.html
在练习1中,他们解释了如何在Android中处理sqlite数据库。