这是我的代码,通过它我将数据输入数据库并且我正在接收currentntrowId,其中输入数据并且currentrow_id正在更新但是cursor.getcount()仍为null
SQLiteDatabase dbr = help.getReadableDatabase();
String[] projection = {
columnid,
columnimage,
columnttitle,
columndesription,
columnurl
};
Cursor cursor = dbr.query(tablename, projection, null, null, null, null, null);
Cursor cur = dbr.query(tablename, projection, null, null, null, null, null);
for (int i = 0; i < mModelList1.size(); i++) {
cursor.moveToFirst();
if (cursor.getCount()==0){
Insert_in_a_row(i,1,mModelList1,help);
cur.moveToLast();
Log.d("uniq cursor.getCount()=",""+cursor.getCount()); //here i get the cursor.getcount() null
}
Log.d("fist cursor.getCount()=",""+cursor.getCount());
while (!cursor.isAfterLast()) {
if(cursor.getCount()>0) {
cur.moveToLast();
int Id = cur.getInt(cur.getColumnIndex(columnid)) + 1;
Log.d("size of mModellist ", "" + mModelList1.size());
Insert_in_a_row(i, Id, mModelList1, help);
Log.d("scnd cursor.getCount()=", "" + cur.getCount()); //here i get the cursor.getcount() null
}
cursor.moveToLast();
cursor.moveToNext();
}if(i==mModelList1.size()-1){cursor.close();
cur.close();
dbr.close();}
}
我调用插入Insert_in_a_row()
的方法 public void Insert_in_a_row(int i,int Id,List<NewsModel>
mModelList1,News_dbhelper help){
final SQLiteDatabase dbw = help.getWritableDatabase();
Log.d("size of mModellist ",""+mModelList1.size());
try{ URL ur=new URL(mModelList1.get(i).getimage());
bmp=BitmapFactory.decodeStream(ur.openConnection().getInputStream());
}catch (IOException e){e.printStackTrace();}
byte[] byteimage1 = getBytes(bmp);
ContentValues values = new ContentValues();
values.put(columnid, Id);
values.put(columnimage, byteimage1);
values.put(columnttitle, mModelList1.get(i).getTitle());
values.put(columndesription,mModelList1.get(i).getDescription());
values.put(columnurl, mModelList1.get(i).getUrl());
long currentRow_ID = dbw.insert(tablename, null, values);
if (Id != currentRow_ID) {
//Toast.makeText(con, "Error with saving news", Toast.LENGTH_LONG).show();
Log.d("news was not saved",""+currentRow_ID );// here I'm recieving an updated currentrow_id but cursor.getcount() is still null
} else {
// Toast.makeText(con, "You are having " + currentRow_ID + " News", Toast.LENGTH_LONG).show();
Log.d("news saved on title", ""+currentRow_ID);
}
}
之后当我通过getreadabledatabase()获取记录以从数据库接收数据时我收到错误无效表意味着数据未存储在数据库中
更新错误日志
D/news saved on title: 1
D/uniq cursor.getCount()=: 0
D/fist cursor.getCount()=: 0
D/size of mModellist: 7
I/art: Background partial concurrent mark sweep GC freed 278(23KB) AllocSpace objects, 6(481KB) LOS objects, 37% free, 6MB/10MB, paused 5.591ms total 19.775ms
D/news was not saved: 2
D/uniq cursor.getCount()=: 0
D/fist cursor.getCount()=: 0
D/size of mModellist: 7
D/news was not saved: 3
D/uniq cursor.getCount()=: 0
D/fist cursor.getCount()=: 0
D/size of mModellist: 7
D/news was not saved: 4
D/uniq cursor.getCount()=: 0
D/fist cursor.getCount()=: 0
D/size of mModellist: 7
D/news was not saved: 5
D/uniq cursor.getCount()=: 0
D/fist cursor.getCount()=: 0
D/size of mModellist: 7
D/news was not saved: 6
D/uniq cursor.getCount()=: 0
D/fist cursor.getCount()=: 0
D/size of mModellist: 7
D/news was not saved: 7
D/uniq cursor.getCount()=: 0
D/fist cursor.getCount()=: 0
W/System.err: java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.database.Cursor.moveToFirst()' on a null object reference
W/System.err: at com.Newsforest.android.Newsforest.MainActivity.gatherInformation(MainActivity.java:356)
W/System.err: at com.Newsforest.android.Newsforest.MainActivity.onCreate(MainActivity.java:179)
W/System.err: at android.app.Activity.performCreate(Activity.java:5990)
W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
W/System.err: at android.app.ActivityThread.access$800(ActivityThread.java:151)
W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err: at android.os.Looper.loop(Looper.java:135)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5254)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
MainActivity日志消息在
下面 W/System.err: java.lang.IllegalStateException: Invalid tables
W/System.err: at android.database.sqlite.SQLiteDatabase.findEditTable(SQLiteDatabase.java:973)
W/System.err: at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1163)
W/System.err: at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1034)
W/System.err: at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1202)
W/System.err: at com.Newsforest.android.Newsforest.MainActivity.gatherInformation(MainActivity.java:346)
W/System.err: at com.Newsforest.android.Newsforest.MainActivity.onCreate(MainActivity.java:179)
W/System.err: at android.app.Activity.performCreate(Activity.java:5990)
W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
W/System.err: at android.app.ActivityThread.access$800(ActivityThread.java:151)
W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err: at android.os.Looper.loop(Looper.java:135)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5254)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
我收到错误的MainActivity代码在
之下 try {//News_dbhelper mdbHelper= new News_dbhelper(this);
SQLiteDatabase db = mdbHelper.getReadableDatabase();
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
queryBuilder.setTables(tablename);
String[] projection = {
columnid,
columnimage,
columnttitle,
columndesription,
columnurl
};
Cursor C= db.query(tablename,
projection,
null,
null,
null,
null,
null);
// Log.d( "gatherInformation: ",""+C.getCount());
C.moveToFirst();
while (C.isAfterLast()){
NewsModel model = new NewsModel();
//Cursor cursor1 = db.rawQuery(" SELECT * FROM " + tablename + " WHERE " + columnid + "=?", new String[]{String.valueOf(j)});
model.setTitle(C.getString(C.getColumnIndex(columnttitle)));
model.setDescription(C.getString(C.getColumnIndex(columndesription)));
model.setUrl(C.getString(C.getColumnIndex(columnurl)));
model.setimage(C.getString(C.getColumnIndex(columnimage)));
mModelList.add(model);
C.moveToNext();
}
C.close();
}catch (SQLiteException e){e.printStackTrace();
Toast.makeText(this,"error reason"+e.toString(),Toast.LENGTH_LONG).show();}
catch (Exception e){e.printStackTrace();}
finally {
Log.d("size of mModellist ",""+mModelList.size());
recyclerView.setAdapter(new Adapter(MainActivity.this,mModelList));
}
仍然显示黑屏而不是recyclerView
W/CursorWindow: Window is full: requested allocation 892521 bytes, free space 572848 bytes, window size 2097152 bytes
W/CursorWindow: Window is full: requested allocation 617180 bytes, free space 565848 bytes, window size 2097152 bytes
W/CursorWindow: Window is full: requested allocation 675170 bytes, free space 91920 bytes, window size 2097152 bytes
D/size of mModellist: 8
W/ResourcesManager: Asset path '/system/framework/com.android.media.remotedisplay.jar' does not exist or contains no resources.
W/ResourcesManager: Asset path '/system/framework/com.android.location.provider.jar' does not exist or contains no resources.
D/DynamitePackage: Instantiated singleton DynamitePackage.
D/DynamitePackage: Instantiating com.google.android.gms.ads.ChimeraAdManagerCreatorImpl
答案 0 :(得分:1)
游标允许您访问查询结果集。如果在查询后插入新数据,查询结果集不会更改。因此,光标计数保持为0。
您需要再次查询数据库以查看新数据。
您的System.err日志中被视为警告的NPE不是由您发布的代码版本生成的。