好的,我是一个Android noob,所以忍受我,花了几个小时在这上面搜索。已经查看了建议的重复问题,这有助于我认为我的数据库当时没有创建/打开。
我有一个activity
MainActivity
,可以为activity
创建一个新的Pager
Fragments
。我有一个Sqlite
数据库,其中包含一个带有序列号的表,我希望在点击MainActivity
上的按钮后增加该序列号,然后将序列号发送到fragment
上的文本视图
所以我按下按钮的Main Activity
:
private boolean addFactFind(){
//Get a new sequence number
newSeq();
//Create new factFind
Intent factFindIntent = new Intent(this, EditFactFind.class);
//Give fact find new sequence number
setSeqOnFrag(newSeqNo);
//Open new fact find
startActivity(factFindIntent);
return true;
}
public void setSeqOnFrag(int newSeqNo) {
Summary.Trans frag_Trans = (Summary.Trans)getFragmentManager().findFragmentById(R.id.drawer_layout_sum);
frag_Trans.updateFFID(newSeqNo,ffidTextView);
}
public void newSeq() {
Fact_FindDAO fact_findDAO = new Fact_FindDAO(this);
fact_findDAO.setSequence();
newSeqNo=fact_findDAO.NewSeq;
}
我的数据库助手(一个片段,我遗漏了很长的列列表等):
public Fact_FindDBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE);
database.execSQL(DATABASE_CREATE_SEQ);
}
和DAO(再次省略更长的名单):
public Fact_FindDAO(Context context) {
dbHelper = new Fact_FindDBHelper(context);
}
public void open() {
database = dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
public void deleteFactFind(Fact_FindDBList factFindDBList){
long id = factFindDBList.getId();
System.out.println("Fact find deleted with id:" +id);
database.delete(Fact_FindDBHelper.TABLE_PERSONAL, Fact_FindDBHelper.PERSONAL_ID + " = " + id, null);
}
public List<Fact_FindDBList> getAllFactFinds(){
List<Fact_FindDBList> fact_findDBLists = new ArrayList<Fact_FindDBList>();
Cursor cursor = database.query(Fact_FindDBHelper.TABLE_PERSONAL, allColumns,null,null,null,null,null);
cursor.moveToFirst();
while (!cursor.isAfterLast()){
Fact_FindDBList fact_findDBList = cursorToFactFind(cursor);
fact_findDBLists.add(fact_findDBList);
cursor.moveToNext();
}
//ALWAYS close cursor
cursor.close();
return fact_findDBLists;
}
private Fact_FindDBList cursorToFactFind(Cursor cursor) {
Fact_FindDBList fact_findDBList = new Fact_FindDBList();
fact_findDBList.setId(cursor.getLong(0));
fact_findDBList.setClient(cursor.getString(1));
return fact_findDBList;
}
public int setSequence(){
Cursor cursor = database.rawQuery("Select "+Fact_FindDBHelper.SEQUENCE_NO+" from "+Fact_FindDBHelper.TABLE_SEQUENCE,null,null);
CurrentSeq = cursor.getInt(0);
cursor.moveToFirst();
cursor.close();
NewSeq = CurrentSeq ++;
String updateQuery = "UPDATE "+Fact_FindDBHelper.TABLE_SEQUENCE
+" SET "+Fact_FindDBHelper.SEQUENCE_NO
+" = "+NewSeq
+" WHERE "
+Fact_FindDBHelper.SEQUENCE_NO
+" = "+CurrentSeq;
database.execSQL(updateQuery);
return NewSeq;
}
当我点击按钮添加时,我得到一个空指针异常:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor android.database.sqlite.SQLiteDatabase.rawQuery(java.lang.String, java.lang.String[], android.os.CancellationSignal)' on a null object reference
at uk.co.mcclure_solicitors.wwjmcclure.Fact_FindDAO.setSequence(Fact_FindDAO.java:106)
at uk.co.mcclure_solicitors.wwjmcclure.MainActivity.newSeq(MainActivity.java:181)
at uk.co.mcclure_solicitors.wwjmcclure.MainActivity.addFactFind(MainActivity.java:162)
at uk.co.mcclure_solicitors.wwjmcclure.MainActivity.onClick(MainActivity.java:53)
除53
调用addFactFind
方法之外,代码段中的相关行位于上方。
现在我对现在要做的事情感到有些困惑,我认为空指针来自数据库不是由这一点创建的,所以实际上没有什么可查询的?可能是其他完全愚蠢的东西。对不起,我刚才这么说了,请放心吧!
答案 0 :(得分:2)
您的database
对象为空 - 在open()
setSequence()
之前,您需要致电private async void OpenFile()
{
var file = await ApplicationData.Current.LocalFolder.GetFileAsync("myVideo.mp4");
if (file != null)
{
var result = await Launcher.LaunchFileAsync(file);
}
}
答案 1 :(得分:0)
而不是:
Cursor cursor = database.rawQuery("Select "+Fact_FindDBHelper.SEQUENCE_NO+" from "+Fact_FindDBHelper.TABLE_SEQUENCE,null,null);
CurrentSeq = cursor.getInt(0);
cursor.moveToFirst();
你可以尝试一下这个:
Cursor cursor = database.rawQuery("Select "+Fact_FindDBHelper.SEQUENCE_NO+" from "+Fact_FindDBHelper.TABLE_SEQUENCE,null,null);
if(cursor.getCount()>0){
cursor.moveToFirst();
CurrentSeq = cursor.getInt(0);
}