我正致力于整合数据库以保存和保存。要在片段中检索的包含Recyclerview的联系信息。该应用程序在启动时会直接崩溃。
FATAL EXCEPTION: main Process: com.example.admin.navigationdrawer_demo, PID: 15637
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.admin.navigationdrawer_demo/com.example.admin.navigationdrawer_demo.MainActivity}: 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.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
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)
Caused by: 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.getReadableDatabase(SQLiteOpenHelper.java:187)
at com.example.admin.navigationdrawer_demo.Person_DB_OpenHelper.getAllPerson_MYCONTACT(Person_DB_OpenHelper.java:149)
at com.example.admin.navigationdrawer_demo.HomeFragment.fillListBirthdays(HomeFragment.java:64)
at com.example.admin.navigationdrawer_demo.HomeFragment.onCreateView(HomeFragment.java:55)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:339)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:601)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1237)
at android.app.Activity.performStart(Activity.java:6253)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
我可以创建数据库
DataBaseHelper.java:
public class Person_DB_OpenHelper extends SQLiteOpenHelper {
String TAG="DATABASE";
public static String DB_NAME="COMMUNITY_image.db";
public static int DB_VERSION=1;
public static String TABLE_NAME="PERSON_DETAIL_image";
public static String COL_ID="_id";
private static final String COL_KEY_IMAGE = "image";
public static String COL_FIRST_NAME="first_name";
public static String COL_MIDDLE_NAME="middle_name";
public static String COL_LAST_NAME="last_name";
public static String COL_DOB_string="Date_Of_Birth_String";
public static String COL_Date="Date";
public static String COL_MONTH="Month";
public static String COL_EMAIL_ID_MANDATORY="email_id_mandatory";
public static String COL_EMAIL_ID="email_id";
public static String COL_POSTAL_ADDRESS="postal_address";
public static String COL_OFFICE_ADDRESS="office_address";
public static String COL_MESSANGER_ADDRESS="messanger_address";
private String CREATE_TABLE= "create table "+TABLE_NAME+" ( "+
COL_ID+" integer primary key autoincrement, "+
COL_KEY_IMAGE+" BLOB, "+
COL_FIRST_NAME+" text, "+
COL_MIDDLE_NAME+" text, "+
COL_LAST_NAME+" text, "+
COL_DOB_string+" text, "+
COL_Date+" long, "+
COL_MONTH+" long, "+
COL_EMAIL_ID+" text, "+
COL_POSTAL_ADDRESS+" text, "+
COL_OFFICE_ADDRESS+" text, "+
COL_MESSANGER_ADDRESS+" text);";
public Person_DB_OpenHelper(Context context) {
super(context,DB_NAME,null,DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.d("Create","Table");
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public long insertPerson(Person person)
{
SQLiteDatabase database=getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put(COL_KEY_IMAGE,person.get_image());
contentValues.put(COL_FIRST_NAME,person.getFirst_name());
contentValues.put(COL_MIDDLE_NAME,person.getMiddle_name());
contentValues.put(COL_LAST_NAME,person.getLast_name());
contentValues.put(COL_DOB_string,person.getDOB());
contentValues.put(COL_Date,person.getDate1());
contentValues.put(COL_MONTH,person.getMonth());
contentValues.put(COL_Year,person.getYear());
contentValues.put(COL_DOA,person.getDOA());
contentValues.put(COL_MEMBER_TYPE,person.getMember_type());
contentValues.put(COL_PHONE_MANDATORY,person.getPhone_mandatory());
contentValues.put(COL_PHONE,person.getPhone());
contentValues.put(COL_LAND_LINE_NUMBER,person.getLand_line_number());
contentValues.put(COL_EMAIL_ID_MANDATORY,person.getEmail_id_mandatory());
contentValues.put(COL_EMAIL_ID,person.getEmail_id());
contentValues.put(COL_POSTAL_ADDRESS,person.getPostal_address());
contentValues.put(COL_OFFICE_ADDRESS,person.getOffice_address());
contentValues.put(COL_MESSANGER_ADDRESS,person.getMessanger_address());
Log.d("Object:","Person"+person);
long inserted_record_row_id=database.insert(TABLE_NAME,null,contentValues);
return inserted_record_row_id;
}
// public Cursor getAllPerson_MYCONTACT()
public List<Person> getAllPerson_MYCONTACT()
{
List <Person> ListMyContacts=new ArrayList<>();
SQLiteDatabase database=getReadableDatabase();
Cursor cursor_all_record=database.query(TABLE_NAME,new String[]{COL_ID,COL_FIRST_NAME,COL_MIDDLE_NAME,COL_LAST_NAME,COL_PHONE_MANDATORY},null,null,null,null,null);
//Cursor cursor_all_record=database.query(TABLE_NAME,new String[]{COL_MIDDLE_NAME},null,null,null,null,null,null);
Log.d(TAG,"Cursor "+cursor_all_record.getCount());
while (cursor_all_record.moveToNext())
{
Person p=new Person();
String name=cursor_all_record.getString(1);
p.setFirst_name(name);
// Log.d("DATABASE", "NAME:- "+name);
ListMyContacts.add(p);
}
//return cursor_all_record;
return ListMyContacts;
}
@TargetApi(Build.VERSION_CODES.N)
HomeFragment.java:
public class HomeFragment extends Fragment
{
private Context context;
// Context context=getContext();
Person_DB_OpenHelper person_db_openHelper=new Person_DB_OpenHelper(context);
// Event_DB_OpenHelper event_db_openHelper=new Event_DB_OpenHelper(context);
private List<Person> birthdays=new ArrayList<>();
private List<Event> events=new ArrayList<>();
private List<Person> aniversaries=new ArrayList<>();
public HomeFragment() {
}
public static HomeFragment newInstance()
{
HomeFragment fragment = new HomeFragment();
return fragment;
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
//Context context = view.getContext();
RecyclerView recyclerView = (RecyclerView) view;
fillListEvents();
fillListBirthdays();
fillListAniversaries();
recyclerView.setLayoutManager(new LinearLayoutManager(context));
recyclerView.setAdapter(new HomeAdapter(birthdays,aniversaries,events));
return view;
}
private void fillListBirthdays() {
birthdays=person_db_openHelper.getAllPerson_MYCONTACT();
/* Birthday birthday;
for (int i = 0; i < 2; i++) {
birthday = new Birthday("birthday " + (i + 1));
birthdays.add(birthday);
}*/
}