我希望将一些数据存储到sqlite数据库中,但是在我的DBHandler和DBGen类中发生错误,我无法找到此错误,我想存储用户名,密码,确认密码,手机号码和电子邮件ID。
UserDetailsDTO.java
public class UserDetailsDTO
{
private String userName;
private String password;
private String confirmPassword;
private String mobileNumber;
private String emailId;
public String getUserName(){
return userName;
}
public void setUserName(String userName){
this.userName = userName;
}
public String getPasssword(){
return password;
}
public void setPassword(String password){
this.password = password;
}
public String getConfirmPassword(){
return confirmPassword ;
}
public void setConfirmPassword(String confirmPassword){
this.confirmPassword = confirmPassword;
}
public String getMobileNumber(){
return mobileNumber;
}
public void setMobileNumber(String mobileNumber){
this.mobileNumber = mobileNumber;
}
public String getEmailId(){
return emailId;
}
public void setEmailId(String emailId){
this.emailId = emailId;
}
}
DBGen.java
public class DBGen
{
private Context ctx;
//constants
public final static int MODE_READ=0;
public final static int MODE_WRITE=1;
//SQLite Database
private SQLiteDatabase myDB = null;
private DBHelper dbHelper;
private DBHandler dbHandler;
private static boolean isClosed = false;
private static DBGen dbGen = null;
private DBGen(Context ctx, DBHandler dbHandler){
this.ctx=ctx;
this.dbHandler=dbHandler;
this.dbHelper = new DBHelper(ctx, dbHandler.getDatabase(), null, dbHandler.getVersion());
}
public static DBGen getInstance(Context ctx, DBHandler dbHandler){
if(dbGen == null || isClosed){
dbGen = new DBGen(ctx, dbHandler);
isClosed = false;
}
return dbGen;
}
public SQLiteDatabase open(int mode){
// To close the connection before opening a new one.
if(myDB != null){
close();
}
if(mode == MODE_WRITE){
myDB = dbHelper.getWritableDatabase();
}
else{
myDB = dbHelper.getReadableDatabase();
}
return myDB;
}
public SQLiteDatabase open(){
if((myDB == null)|| (! myDB.isOpen())){
myDB = dbHelper.getWritableDatabase();
}
return myDB;
}
public boolean isDBExists(){
boolean isDBExist=false;
try{
File dbPath=ctx.getDatabasePath(dbHandler.getDatabase());
isDBExist = dbPath.exists();
}
catch(Exception e){
e.printStackTrace();
Log.e(dbHandler.getLogTag(),"ERROR DELETING THE FILE",e);
}
return isDBExist;
}
public boolean removeDB(){
boolean isDBClosed=removeHardDB();
return isDBClosed;
}
private boolean removeHardDB(){
boolean isDBdeleted = false;
try{
File dbFilePath=ctx.getDatabasePath(dbHandler.getDatabase());
close();
isDBdeleted=dbFilePath.delete();
}
catch(Exception e){
e.printStackTrace();
Log.e(dbHandler.getLogTag(),"EXCEPTION RAISED IN DELETING DB");
}
return isDBdeleted;
}
public static void closeCursor(Cursor c){
if(c != null){
c.close();
}
}
public void close(){
isClosed = true;
try{
myDB.close();
}
catch(SQLException sqlex){
Log.e(dbHandler.getLogTag(),sqlex.getMessage(),sqlex);
}
try{
dbHelper.close();
}
catch(SQLException sqlex){
Log.e(dbHandler.getLogTag(),sqlex.getMessage(),sqlex);
}
}
class DBHelper extends SQLiteOpenHelper{
public DBHelper(Context context, String name, CursorFactory factory, int
version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
dbHandler.onCreate(db);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
dbHandler.onUpgrade(db, oldVersion, newVersion);
}
}
}
DBHandler.java
public abstract class DBHandler {
private int version;
private String database;
private String logTag;
private DBGen dbGen;
@SuppressWarnings("unused")
private DBHandler(){}
public DBHandler(Context context, String database, int version, String logTag){
this.database=database;
this.version=version;
this.logTag=logTag;
dbGen = DBGen.getInstance(context, this);
}
public String getDatabase() {
return database;
}
public void setDatabase(String database) {
this.database = database;
}
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
public String getLogTag() {
return logTag;
}
public void setLogTag(String logTag) {
this.logTag = logTag;
}
public SQLiteDatabase getReadDB(){
return dbGen.open();
}
public SQLiteDatabase getWriteDB(){
return dbGen.open();
}
public void close(){
try{
dbGen.close();
}
catch(SQLException sqlEx){
Log.e(logTag, "Exception while closing connection",sqlEx);
}
}
public abstract void onCreate(SQLiteDatabase db);
public abstract void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion);
}
UserDBHandler.java
public class UserDBHandler extends DBHandler {
// DataBase Name
public final static String DATABASE = "UserDetails";
// DataBase Table Name's
public final static String USER_TABLE = "TABLE_USER";
// Field names of MALLS Table
public static final String USER_NAME = "username";
public static final String PASSWORD = "password";
public static final String CONFIRM_PASSWORD = "confirmpassword";
public static final String USER_MOBILE_NUMBER = "mobilenumber";
public static final String USER_EMAIL = "email";
private String CREATE_TABLE_USER = "CREATE TABLE IF NOT EXISTS "
+ USER_TABLE+ " (" + USER_NAME
+ " VARCHAR," + PASSWORD + " VARCHAR,"
+ CONFIRM_PASSWORD + " VARCHAR," + USER_MOBILE_NUMBER + "
VARCHAR,"
+ USER_EMAIL+ " VARCHAR,"
+ ")";
public UserDBHandler(Context context) {
super(context,DATABASE, Constants.DB_VERSION, Constants.TAG);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_USER);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
try {
onCreate(db);
} catch (Exception e) {
e.printStackTrace();
}
}
}
UserDaoImlp.java
public class UserDaoImpl implements UserDao{
private SQLiteDatabase myDB;
private UserDBHandler dbHandler;
private String TAG = "ItopperDaoImpl";
public UserDaoImpl(Context context){
dbHandler = new UserDBHandler(context);
myDB = dbHandler.getWriteDB();
}
@Override
public void beginTransaction() {
// TODO Auto-generated method stub
myDB.beginTransaction();
}
@Override
public void setTransactionSuccessful() {
// TODO Auto-generated method stub
myDB.setTransactionSuccessful();
}
@Override
public void endTransaction() {
// TODO Auto-generated method stub
myDB.endTransaction();
}
public long insertUserDetails(UserDetailsDTO userDetails) {
ContentValues contentValues = new ContentValues();
contentValues.put(UserDBHandler.USER_NAME, userDetails.getUserName());
contentValues.put(UserDBHandler.PASSWORD, userDetails.getPasssword());
contentValues.put(UserDBHandler.CONFIRM_PASSWORD,
userDetails.getConfirmPassword());
contentValues.put(UserDBHandler.USER_MOBILE_NUMBER,userDetails.getMobileNumber());
contentValues.put(UserDBHandler.USER_EMAIL,userDetails.getEmailId());
return myDB.insert(UserDBHandler.USER_TABLE, null,
contentValues);
}
}
StartActivity
public class StartActivity extends Activity {
private EditText
userNameEdit,passwordEdit,confirmPasswordEdit,emailEdit,mobileNoEdit;
private Button sbtButton;
private Button cancelButton;
private String userName;
private String password;
private String confirmPwd;
private String mobileNo;
private String email;
private UserDaoImpl userDaoImpl;
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
userNameEdit = (EditText)findViewById(R.id.userNameedt);
passwordEdit = (EditText)findViewById(R.id.paswordEdit);
confirmPasswordEdit = (EditText)findViewById(R.id.confirmPaswordEdit);
emailEdit = (EditText)findViewById(R.id.emailedt);
mobileNoEdit = (EditText)findViewById(R.id.mobileNoedt);
sbtButton = (Button)findViewById(R.id.submitbtn);
cancelButton = (Button)findViewById(R.id.cancel);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
sbtButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
UserDetailsDTO userDetailsDto = new UserDetailsDTO();
userName = userNameEdit.getText().toString().trim();
password = passwordEdit.getText().toString().trim();
confirmPwd = confirmPasswordEdit.getText().toString().trim();
email = emailEdit.getText().toString().trim();
mobileNo = mobileNoEdit.getText().toString().trim();
userDetailsDto.setUserName(userName);
userDetailsDto.setPassword(password);
userDetailsDto.setConfirmPassword(confirmPwd);
userDetailsDto.setMobileNumber(mobileNo);
userDetailsDto.setEmailId(email);
userDaoImpl = new UserDaoImpl(mContext);
userDaoImpl.insertUserDetails(userDetailsDto);
}
});
}
}
以下是LogCat特定问题代码
12-31 12:09:17.749:E / AndroidRuntime(962):java.lang.NullPointerException 12月12日至31日:09:17.749:E / AndroidRuntime(962):在android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:157) 12-31 12:09:17.749:E / AndroidRuntime(962):at com.technowellservices.mobileuapp.database.DBGen.open(DBGen.java:63) 12-31 12:09:17.749:E / AndroidRuntime(962):at com.technowellservices.mobileuapp.database.DBHandler.getWriteDB(DBHandler.java:59) 12-31 12:09:17.749:E / AndroidRuntime(962):at com.technowellservices.mobileuapp.database.UserDaoImpl。(UserDaoImpl.java:20) 12-31 12:09:17.749:E / AndroidRuntime(962):at com.technowellservices.mobileuapp.Activities.StartActivity $ 1.onClick(StartActivity.java:70) 12-31 12:09:17.749:E / AndroidRuntime(962):在android.view.View.performClick(View.java:3480) 12-31 12:09:17.749:E / AndroidRuntime(962):在android.view.View $ PerformClick.run(View.java:13983) 12-31 12:09:17.749:E / AndroidRuntime(962):在android.os.Handler.handleCallback(Handler.java:605) 12-31 12:09:17.749:E / AndroidRuntime(962):在android.os.Handler.dispatchMessage(Handler.java:92) 12-31 12:09:17.749:E / AndroidRuntime(962):在android.os.Looper.loop(Looper.java:137) 12-31 12:09:17.749:E / AndroidRuntime(962):在android.app.ActivityThread.main(ActivityThread.java:4340) 12-31 12:09:17.749:E / AndroidRuntime(962):at java.lang.reflect.Method.invokeNative(Native Method) 12-31 12:09:17.749:E / AndroidRuntime(962):at java.lang.reflect.Method.invoke(Method.java:511) 12月12日至31日:09:17.749:E / AndroidRuntime(962):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784) 12-31 12:09:17.749:E / AndroidRuntime(962):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 12-31 12:09:17.749:E / AndroidRuntime(962):at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
错误的最可能原因是无效的上下文,在您的代码中将此行放在类声明下面:
UserDaoImpl userDaoImpl;
然后在onCreate init中就像:
userDaoImpl = new UserDaoImpl (this)