未从数据库中检索数据

时间:2017-11-10 07:29:56

标签: android sqlite android-sqlite

我是Android的新手,我想创建一个应用程序来获取所有传入的数字并显示在列表中。 在第一次安装应用程序时运行但不显示传入的数字,当我们在安装后再次打开应用程序时会崩溃。

要在安装后运行应用程序,需要清除其数据(然后应用程序运行但不显示任何内容)

logcat的

    1-10 11:03:19.141 10244-10244/nischayvaish.com.test1 E/CursorWindow: Failed to read row 0, column -1 from a CursorWindow which has 1 rows, 2 columns.
11-10 11:03:19.141 10244-10244/nischayvaish.com.test1 D/AndroidRuntime: Shutting down VM
11-10 11:03:19.142 10244-10244/nischayvaish.com.test1 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                        Process: nischayvaish.com.test1, PID: 10244
                                                                        java.lang.RuntimeException: Unable to start activity ComponentInfo{nischayvaish.com.test1/nischayvaish.com.test1.MainActivity}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                                                                            at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                            at android.os.Looper.loop(Looper.java:154)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
                                                                         Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
                                                                            at android.database.CursorWindow.nativeGetLong(Native Method)
                                                                            at android.database.CursorWindow.getLong(CursorWindow.java:511)
                                                                            at android.database.CursorWindow.getInt(CursorWindow.java:578)
                                                                            at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:69)
                                                                            at nischayvaish.com.test1.MainActivity.readFromDb(MainActivity.java:60)
                                                                            at nischayvaish.com.test1.MainActivity.onCreate(MainActivity.java:38)
                                                                            at android.app.Activity.performCreate(Activity.java:6662)
                                                                            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                                                                            at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                            at android.os.Looper.loop(Looper.java:154) 
                                                                            at android.app.ActivityThread.main(ActivityThread.java:6077) 
                                                                            at java.lang.reflect.Method.invoke(Native Method) 
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 

这是 MainActivity

  private TextView tv1;
    private RecyclerView recycler_view;
    private RecyclerView.LayoutManager layoutManager;
    private ArrayList<IncomingNumber> arrayList = new ArrayList<>();
    private RecyclerAdapter adapter;
    private BroadcastReceiver broadcastReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv1 = (TextView) findViewById(R.id.emptyTxt);
        recycler_view = (RecyclerView) findViewById(R.id.recyclerView);
        layoutManager = new LinearLayoutManager(this);
        recycler_view.setLayoutManager(layoutManager);
        recycler_view.setHasFixedSize(true);
        adapter = new RecyclerAdapter(arrayList);
        recycler_view.setAdapter(adapter);
        readFromDb();
        broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                readFromDb();
            }
        };

    }

    private void readFromDb() {
        arrayList.clear();
        DbHelper dbHelper = new DbHelper(this);
        SQLiteDatabase database = dbHelper.getWritableDatabase();
        Cursor cursor = dbHelper.readNumber(database);
        if (cursor.getCount() > 0) {
            while (cursor.moveToNext()) {
                String number;
                int id;
                number = cursor.getString(cursor.getColumnIndex(DbContract.INCOMING_NUMBER));
                id = cursor.getInt(cursor.getColumnIndex("id"));
                arrayList.add(new IncomingNumber(id, number));
            }
            cursor.close();
            dbHelper.close();
            adapter.notifyDataSetChanged();
            recycler_view.setVisibility(View.VISIBLE);
            tv1.setVisibility(View.GONE);
        }
}

@Override
protected void onResume() {
    super.onResume();
    registerReceiver(broadcastReceiver, new IntentFilter(DbContract.UPDATE_UI_FILTER));
}

@Override
protected void onPause() {
    super.onPause();
    unregisterReceiver(broadcastReceiver);
}

这是 DbHelper.class

public class DbHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "numberDb";
    private static final int DATABASE_VERSION =2;
    private static final String CREATE = "CREATE TABLE " + DbContract.TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT," + DbContract.INCOMING_NUMBER + " TEXT);";
    private static final String DROP_TABLE = "drop table if exists " + DbContract.TABLE_NAME;

    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(DROP_TABLE);
        onCreate(db);
    }

    public void saveNumber(String number, SQLiteDatabase database) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(DbContract.INCOMING_NUMBER, number);
        Log.e(number,"incoming nuumber");
        database.insert(DbContract.TABLE_NAME, null, contentValues);
    }

    public Cursor readNumber(SQLiteDatabase database) {
        String[] projection = {"id", DbContract.INCOMING_NUMBER};
        return (database.query(DbContract.TABLE_NAME, projection, null, null, null, null, null));
    }

DbContract.class

public static final String TABLE_NAME = "incomingInfo";
    public static final String INCOMING_NUMBER = "incomingNumber";
    public static final String UPDATE_UI_FILTER = "nischayvaish.com.test1.UPDATE_UI";

2 个答案:

答案 0 :(得分:0)

如果没有对代码进行详细分析,以下语句会导致错误:

id = cursor.getInt(cursor.getColumnIndex("id"));

这是因为游标找不到名为id

的列

https://developer.android.com/reference/android/database/Cursor.html#getColumnIndex(java.lang.String)

  

返回给定列名的从零开始的索引,如果列不存在,则返回-1。如果您希望该列存在,请使用getColumnIndexOrThrow(String),这将使错误更加清晰。

结合jibrahim的回答,你对database.query的调用可能是错误的,应该引导你使用代码。

答案 1 :(得分:0)

我认为问题是getColumnIndex方法区分大小写,因此您应该使用getColumnIndex("id")

而不是使用getColumnIndex("ID")

所以 readDB 方法可以是: -

private void readFromDb() {
    arrayList.clear();
    DbHelper dbHelper = new DbHelper(this);
    SQLiteDatabase database = dbHelper.getWritableDatabase();
    Cursor cursor = dbHelper.readNumber(database);
    if (cursor.getCount() > 0) {
        while (cursor.moveToNext()) {
            String number;
            int id;
            number = cursor.getString(cursor.getColumnIndex(DbContract.INCOMING_NUMBER));
            id = cursor.getInt(cursor.getColumnIndex("ID")); //<<<<<<<<
            arrayList.add(new IncomingNumber(id, number));
        }
        cursor.close();
        dbHelper.close();
        adapter.notifyDataSetChanged();
        recycler_view.setVisibility(View.VISIBLE);
        tv1.setVisibility(View.GONE);
    }
}

备注

  • `//&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; '表示更改的行

  • 如果光标为空/有0行,你应该关闭光标,所以cursor.close()真的应该关闭,因此dbHelper.close()应该在if (cursor.getCount() > 0) {....}之外/之后构造。