我是一名新的Android开发人员,我正在创建一个存储过期日期的应用程序。
问题:当我想从用户输入创建新日期时,SQLite正在调用null openOrCreateDatabase()方法
如果您有任何反馈或需要任何其他课程,请随意放纵我!
logcat的
java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
at tech.destinum.recorderis.DB.DBHelper.createDate(DBHelper.java:131)
at tech.destinum.recorderis.adapters.FormAdapter$1.onClick(FormAdapter.java:70)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
DBHelper
public class DBHelper extends SQLiteOpenHelper {
private final static String DB_NAME = "recorderis";
private final static int DB_VERSION = 1;
public final static String TABLE_USERS = "users";
public final static String USERS_COLUMN_ID = "_id";
public final static String USERS_COLUMN_NAME = "name";
public final static String USERS_COLUMN_CELLPHONE = "cellphone";
public final static String USERS_COLUMN_FIJO = "fijo";
public final static String USERS_COLUMN_EMAIL = "email";
public final static String USERS_COLUMN_PREMIUM = "premium";
public final static String TABLE_DATES = "dates";
public final static String DATES_COLUMN_ID = "_id";
public final static String DATES_COLUMN_NAME = "name";
public final static String DATES_COLUMN_DATE = "date";
public final static String DATES_COLUMN_USER_ID = "user_id";
public DBHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String users = String.format("CREATE TABLE " + TABLE_USERS + "("
+ USERS_COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ USERS_COLUMN_NAME + " TEXT, "
+ USERS_COLUMN_CELLPHONE + " INTEGER, "
+ USERS_COLUMN_FIJO + " INTEGER, "
+ USERS_COLUMN_EMAIL + " TEXT, "
+ USERS_COLUMN_PREMIUM + ")", TABLE_USERS, USERS_COLUMN_ID, USERS_COLUMN_NAME,
USERS_COLUMN_CELLPHONE, USERS_COLUMN_FIJO, USERS_COLUMN_EMAIL, USERS_COLUMN_PREMIUM);
String dates = String.format("CREATE TABLE " + TABLE_DATES + "("
+ DATES_COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ DATES_COLUMN_NAME + " TEXT, "
+ DATES_COLUMN_DATE + " DATE, "
+ DATES_COLUMN_USER_ID + " LONG)", TABLE_DATES, DATES_COLUMN_ID, DATES_COLUMN_NAME, DATES_COLUMN_DATE,
DATES_COLUMN_USER_ID);
try {
db.execSQL(users);
db.execSQL(dates);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERS);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_DATES);
onCreate(db);
}
public void createNewUser(String name, int cell, int fijo, String email, String premium){
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(USERS_COLUMN_NAME, name);
values.put(USERS_COLUMN_CELLPHONE, cell);
values.put(USERS_COLUMN_FIJO, fijo);
values.put(USERS_COLUMN_EMAIL, email);
values.put(USERS_COLUMN_PREMIUM, premium);
db.insertWithOnConflict(TABLE_USERS, null, values, SQLiteDatabase.CONFLICT_REPLACE);
db.close();
}
public void createDate(String name, String date, long user_id){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(DATES_COLUMN_NAME, name);
values.put(DATES_COLUMN_DATE, date);
values.put(DATES_COLUMN_USER_ID, user_id);
db.insertWithOnConflict(TABLE_DATES, null, values, SQLiteDatabase.CONFLICT_REPLACE);
db.close();
}
}
FormAdapter
public class FormAdapter extends RecyclerView.Adapter<FormAdapter.ViewHolder> {
public static final String FORM_PREFERENCES = "FormPreferences";
private Context mContext;
private ArrayList<Document> mDocuments;
private DBHelper mDBHelper;
public FormAdapter(Context mContext, ArrayList<Document> mDocuments) {
this.mContext = mContext;
this.mDocuments = mDocuments;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.format_form, parent, false));
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
final Document document = mDocuments.get(position);
mDBHelper = new DBHelper(mContext);
holder.mTitle.setText(document.getName());
holder.mTitleExpanded.setText(document.getName());
holder.mEditText.setTag(R.id.date_et, position);
holder.mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = (int) holder.mEditText.getTag(R.id.date_et);
Log.d("pos", String.valueOf(position));
SharedPreferences mSP = v.getContext().getSharedPreferences(FORM_PREFERENCES, Context.MODE_PRIVATE);
SharedPreferences.Editor mEditor = mSP.edit();
String data = holder.mEditText.getText().toString();
switch (position){
case 0:
mDBHelper.createDate(v.getContext().getString(R.string.doc_soat), data, document.getId());
Log.d("SOAT", data);
mEditor.putString("soat", data);
mEditor.commit();
break;
case 1:
mDBHelper.createDate(v.getContext().getString(R.string.doc_rtm), data, document.getId());
Log.d("RTM", data);
mEditor.putString("rtm", data);
mEditor.commit();
break;
case 2:
mDBHelper.createDate(v.getContext().getString(R.string.doc_str), data, document.getId());
Log.d("SRC", data);
mEditor.putString("src", data);
mEditor.commit();
break;
case 3:
mDBHelper.createDate(v.getContext().getString(R.string.doc_src), data, document.getId());
Log.d("STR", data);
mEditor.putString("str", data);
mEditor.commit();
break;
case 4:
mDBHelper.createDate(v.getContext().getString(R.string.doc_tao), data, document.getId());
Log.d("TO", data);
mEditor.putString("to", data);
mEditor.commit();
break;
}
}
});
}
@Override
public int getItemCount() {
return mDocuments != null ? mDocuments.size(): 0;
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView mTitle, mTitleExpanded;
public Button mButton;
public EditText mEditText;
public ImageView mImageView;
private int originalHeight = 0;
private boolean isViewExpanded = false;
private ConstraintLayout mConstraintLayout;
private DateWatcher mDateWatcher;
public ViewHolder(View view) {
super(view);
view.setOnClickListener(this);
mConstraintLayout = (ConstraintLayout) view.findViewById(R.id.expanded);
mTitle = (TextView) view.findViewById(R.id.name_title_tv);
mTitleExpanded = (TextView) view.findViewById(R.id.name_title_tv_expanded);
mButton = (Button) view.findViewById(R.id.add_button);
mEditText = (EditText) view.findViewById(R.id.date_et);
mImageView = (ImageView) view.findViewById(R.id.imageView_up);
mDateWatcher = new DateWatcher(mEditText);
mEditText.addTextChangedListener(mDateWatcher);
if (isViewExpanded == false) {
// Set Views to View.GONE and .setEnabled(false)
mConstraintLayout.setVisibility(View.GONE);
mConstraintLayout.setEnabled(false);
}
}
@Override
public void onClick(final View v) {
// If the originalHeight is 0 then find the height of the View being used
// This would be the height of the ConstraintLayout
if (originalHeight == 0) {
originalHeight = v.getHeight();
}
// Declare a ValueAnimator object
ValueAnimator valueAnimator;
if (!isViewExpanded) {
mTitle.setVisibility(View.GONE);
mTitle.setEnabled(false);
mImageView.setVisibility(View.GONE);
mConstraintLayout.setVisibility(View.VISIBLE);
mConstraintLayout.setEnabled(true);
isViewExpanded = true;
valueAnimator = ValueAnimator.ofInt(originalHeight, originalHeight + (int) (originalHeight)); // These values in this method can be changed to expand however much you like
} else {
isViewExpanded = false;
valueAnimator = ValueAnimator.ofInt(originalHeight + (int) (originalHeight), originalHeight);
Animation a = new AlphaAnimation(1.00f, 0.00f); // Fade out
a.setDuration(200);
// Set a listener to the animation and configure onAnimationEnd
a.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
mTitle.setVisibility(View.VISIBLE);
mTitle.setEnabled(true);
mImageView.setVisibility(View.VISIBLE);
mConstraintLayout.setVisibility(View.GONE);
mConstraintLayout.setEnabled(false);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
// Set the animation on the custom view
mConstraintLayout.startAnimation(a);
}
valueAnimator.setDuration(200);
valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
Integer value = (Integer) animation.getAnimatedValue();
v.getLayoutParams().height = value.intValue();
v.requestLayout();
}
});
valueAnimator.start();
}
}
}
答案 0 :(得分:1)
答案在错误消息中给出:在您的db帮助程序中,它在对Context的空引用上调用Context.openOrCreateDb。这意味着传递给mDBHelper构造函数的mContext为null,这反过来暗示提供给FormAdapter构造函数的mContext为null。