I entered the data in slqite database still cursor.getCount() is null and the Error :- Invalid tables

I entered the data in slqite database still cursor.getCount() is null and the Error :- Invalid tables

这是我将数据输入数据库的代码,我正在接收输入数据的 currentrowId,currentrow_id 正在更新,但 cursor.getcount() 仍然为空

     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() 获取记录以从数据库接收数据时,出现错误 Invalid tables means data is not stored in the database

更新错误日志

  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.access0(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.access0(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.

您需要再次查询数据库才能看到新数据。

在您的 System.err 日志中被视为警告的 NPE 不是由您发布的代码版本产生的。