首先我有此错误消息
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.getWritableDatabase(SQLiteOpenHelper.java:163)
at net.purplebug.mydoctorfinder.MyDBHandler.showDoctors(MyDBHandler.java:133)
at net.purplebug.mydoctorfinder.HomeFragment.showDoctorList(HomeFragment.java:131)
所以我认为这可能是我为dbhandler实例编写Context作为参数的方式。
这是触发错误的代码。它是扩展Fragment的类中的一种方法。
public void showDoctorList() {
String result = "Match Found";
MyDBHandler dbHandler = new MyDBHandler(getContext(), null, null, 1);
ArrayList<ArrayList<String>> docArray = new ArrayList<ArrayList<String>>();
DoctorDataModel doctor = null;
try {
doctor = dbHandler.showDoctors();
} catch (SQLException e) {
e.printStackTrace();
}
if (doctor != null) {
TableRow row = new TableRow(getActivity());
row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
docArray = doctor.getDocArray();
for(int i = 0; i < docArray.size(); i++) {
for(int j = 0; j < docArray.get(i).size(); j++) {
TextView textView = new TextView(getActivity());
textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
textView.setPadding(5, 5, 5, 5);
textView.setText(docArray.get(i).get(j) + " ");
row.addView(textView);
}
tableLayout.addView(row);
}
} else {
result = "No Match Found";
}
Log.d("QUERY RESULT", result);
}
最后是DBHandler中与错误相关的代码。
public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
this.myContext = context;
}
public DoctorDataModel showDoctors() throws SQLException {
int index = 0;
String docLastName, docFirstName, docId;
String query = "Select doctor_id, lastName, firstName FROM " + TABLE_DOCTOR;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
DoctorDataModel doctor = new DoctorDataModel();
while (cursor.moveToNext()) {
docId = cursor.getString(0);
docLastName = cursor.getString(1);
docFirstName = cursor.getString(2);
doctor.setdocArrayList(index, docId, docLastName, docFirstName);
index++;
}
index = 0;
cursor.close();
db.close();
return doctor;
}
我真的认为这与我在此代码中获取Context的方式有关。
MyDBHandler dbHandler = new MyDBHandler(getContext(),null,null,1);
或者它可能是其他问题。那你们觉得怎么样?
答案 0 :(得分:1)
如果您想以片段形式获取应用程序上下文,那么您可以使用
getActivity().getApplicationContext()
注意: - 请考虑片段的生命周期,并确保片段附加到活动。
答案 1 :(得分:0)
你必须这样做:
`Context context;`
context = getActivity();
你可以在片段的任何地方使用它。
答案 2 :(得分:0)
你可以定义一个全局变量:
private Context context;
并在onCreate方法中初始化它:
context = this.getActivity();
稍后将其用作以下内容以避免
NullPointerException
:
if(context!=null){
//your code
}
通过这种方式,您可以在所有片段函数/方法中使用context
变量。
答案 3 :(得分:0)
在片段中,您可以通过:: getActivity()
获取上下文使用之前,只需检查,片段是否附加到活动?
Context context = getActivity();
if(context != null){
//write your code here...
}