Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Db处理程序返回null,无法创建表?_Android_Sqlite - Fatal编程技术网

Android Db处理程序返回null,无法创建表?

Android Db处理程序返回null,无法创建表?,android,sqlite,Android,Sqlite,我有一个数据库类,我正在使用它将sql lite数据库连接到listview。该类存储我的所有上下文内容,例如检索行,但即使是这个简单的getPaitnets也失败了。这是我的完整类 public class DatabaseHandler extends SQLiteOpenHelper { private static final int DATABASE_VERSION = 1; private static final String DATAB

我有一个数据库类,我正在使用它将sql lite数据库连接到listview。该类存储我的所有上下文内容,例如检索行,但即使是这个简单的getPaitnets也失败了。这是我的完整类

    public class DatabaseHandler extends SQLiteOpenHelper {

        private static final int DATABASE_VERSION = 1;

        private static final String DATABASE_NAME = "CautioManager",
        TABLE_CONTACTS = "patient",
        KEY_ID = "id",
        KEY_NAME = "name",
        KEY_PHONE = "phone",
        KEY_CONTACTPHONE = "contactphone",
        KEY_EMAIL = "email",
        KEY_ADDRESS = "address",
        KEY_IMAGEURI = "imageUri";

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

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("CREATE TABLE " + TABLE_CONTACTS + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_NAME + " TEXT," + KEY_PHONE + " TEXT," + KEY_EMAIL + " TEXT," + KEY_ADDRESS + " TEXT," + KEY_IMAGEURI + " TEXT)");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);

            onCreate(db);
        }

        public void createContact(Patients contact) {
            SQLiteDatabase db = getWritableDatabase();

            ContentValues values = new ContentValues();

            values.put(KEY_NAME, contact.getName());
            values.put(KEY_PHONE, contact.getPhone());
            values.put(KEY_CONTACTPHONE, contact.get_contactPhone());

            values.put(KEY_EMAIL, contact.getEmail());
            values.put(KEY_ADDRESS, contact.getAddress());
            values.put(KEY_IMAGEURI, contact.getImageURI().toString());

            db.insert(TABLE_CONTACTS, null, values);
            db.close();
        }

        public Patients getPatientsById(int id) {
            SQLiteDatabase db = getReadableDatabase();

            Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID, KEY_NAME, KEY_PHONE, KEY_EMAIL, KEY_ADDRESS, KEY_IMAGEURI }, KEY_ID + "=?", new String[] { String.valueOf(id) }, null, null, null, null );

            if (cursor != null)
                cursor.moveToFirst();

            Patients _patients = new Patients(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5),  Uri.parse(cursor.getString(6)));
            db.close();
            cursor.close();
            return _patients;
        }

        public void deleteContact(Patients contact) {
            SQLiteDatabase db = getWritableDatabase();
            db.delete(TABLE_CONTACTS, KEY_ID + "=?", new String[] { String.valueOf(contact.getId()) });
            db.close();
        }

        public int getContactsCount() {
            SQLiteDatabase db = getReadableDatabase();
            Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_CONTACTS, null);
            int count = cursor.getCount();
            db.close();
            cursor.close();

            return count;
        }

        public int updatePatients(Patients _Patients) {
            SQLiteDatabase db = getWritableDatabase();

            ContentValues values = new ContentValues();

            values.put(KEY_NAME, _Patients.getName());
            values.put(KEY_PHONE, _Patients.getPhone());
            values.put(KEY_CONTACTPHONE, _Patients.get_contactPhone());

            values.put(KEY_EMAIL, _Patients.getEmail());
            values.put(KEY_ADDRESS, _Patients.getAddress());
            values.put(KEY_IMAGEURI, _Patients.getImageURI().toString());

            int rowsAffected = db.update(TABLE_CONTACTS, values, KEY_ID + "=?", new String[] { String.valueOf(_Patients.getId()) });
            db.close();

            return rowsAffected;
        }

        public List<Patients> getALlPatients() {
            List<Patients> _patients = new ArrayList<Patients>();

            SQLiteDatabase db = getWritableDatabase();
            Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_CONTACTS, null);

            if (cursor.moveToFirst()) {
                do {
                    _patients.add(new Patients(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4),cursor.getString(5), Uri.parse(cursor.getString(6))));
                }
                while (cursor.moveToNext());
            }
            if (cursor.getCount()==0)
            {

                _patients.add(new Patients(1,"David Buckley","","","","",null));
                _patients.add(new Patients(2,"James Buckley","","","","",null));
            }

            cursor.close();
            db.close();
            return _patients;
        }
    }
显示片段\患者\列表的布局文件

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:tools="http://schemas.android.com/tools"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         tools:context="com.davidbuckley.Cautio.Fragments.PatientsListFragment">

<!-- TODO: Update blank fragment layout -->

 <ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/listView"
    android:layout_gravity="center" />
 </FrameLayout>


显示导致故障的日志和行error@MurtazaKhursheedHussain很抱歉在上面的编辑中添加了这个问题,您在哪里使用db实例?
lv
null
。确保您试图获取的
列表视图
的id是正确的,并且这是
活动
布局的一部分。在“创建我的主要活动”中?im设置patientsList=db.GetPaintents();但它将返回空对象
 Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:tools="http://schemas.android.com/tools"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         tools:context="com.davidbuckley.Cautio.Fragments.PatientsListFragment">

<!-- TODO: Update blank fragment layout -->

 <ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/listView"
    android:layout_gravity="center" />
 </FrameLayout>