调用类实例化为什么添加参数this会错误

在开发我的小项目备忘本(Reminder)的过程中有两个类

一个是MainActivity.java

一个是ReminderDbAdapter.java

MainActivity.java

package com.example.reminders; import android.database.Cursor; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.widget.ListView; public class MainActivity extends AppCompatActivity { //实现备忘录的增删改除 //向ListView添加条目 private ListView mListView; private ReminderDbAdapter mDbAdapter; private ReminderSimpleCursorAdapter mCursorAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mListView = (ListView) findViewById(R.id.reminders_list_view); mListView.setDivider(null); mDbAdapter=new ReminderDbAdapter(this);//问题地方 mDbAdapter.open(); Cursor cursor=mDbAdapter.fetchAllReminders(); String[] from=new String[]{ReminderDbAdapter.COL_CONTENT}; int[] to=new int[]{R.id.row_text}; mCursorAdapter=new ReminderSimpleCursorAdapter(MainActivity.this,R.layout.reminders_row ,cursor,from,to,0); //现在游标适配器(controller)更新了db数据库(model)的数据到ListView的view上的数据即试图变了 mListView.setAdapter(mCursorAdapter); } @Override public boolean onCreateOptionsMenu(Menu menu) { System.out.println("创建菜单"); getMenuInflater().inflate(R.menu.menu,menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_new: Log.d(getLocalClassName(), "creat new Reminders"); return true; case R.id.action_exit: finish(); return true; default: return false; } } }

ReminderDbAdapter.java

package com.example.reminders;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * Created by Administrator on 2016/12/24.
 */
class ReminderDbAdapter {


    public static class DatabaseHelper extends SQLiteOpenHelper {
        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            Log.w(TAG, DATABASE_CREATE);
            db.execSQL(DATABASE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(TAG, "Downgrade database from  version" + oldVersion + "to" +
                    newVersion + ",which will destroy all old  data");
            db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
            onCreate(db);
        }
    }

        public static final String COL_ID = "id";
        public static final String COL_CONTENT = "content";
        public static final String COL_IMPORTANT = "important";

        public static final int INDEX_ID = 0;
        public static final int INDEX_CONTENT = INDEX_ID + 1;
        public static final int INDEX_IMPORTENT = INDEX_ID + 2;

        public static final String TAG = "RemindersDbAdapter";

        private DatabaseHelper mDbHelper;
        private SQLiteDatabase mDb;

        private static final String DATABASE_NAME = "dba_remder";
        private static final String TABLE_NAME = "tbl_remdrs";
        private static final int DATABASE_VERSION = 1;

        private Context mContext;

        public static final String DATABASE_CREATE = "CREATE TABLE if exits" + TABLE_NAME +
                "(" + COL_ID + "INTEGER PRIMARY KEY autioncrement," + COL_CONTENT + "TEXT,"
                + COL_IMPORTANT + "INTEGER);";

        /*
        * 如何使用DatabaseHelper来打开关闭数据库
        * 构造函数保存了一个Context实例
        * close()方法使用助手类来关闭数据库
        * */
        public void ReminderDbAdapter(Context context) {
            this.mContext = context;
        }

        //open
        public void open() throws SQLException {
            mDbHelper = new DatabaseHelper(mContext);
            mDb = mDbHelper.getWritableDatabase();
        }

        //close
        public void close() {
            if (mDbHelper != null) {
                mDbHelper.close();
            }
        }

        /*包含在tbl_remdrs表中处理Reminder对象创建、读取、更新、和删除操作的所有逻辑
        这些叫做GRUD(创建、读取、更新、和删除)操作
        *   ContentValues是一个数据梭,用于将数据值传递给数据库的insert方法
        *   cursor-》光标
        * */
        public void createReminder(String name, boolean important) {
            ContentValues values = new ContentValues();
            values.put(COL_CONTENT, name);
            values.put(COL_IMPORTANT, important ? 1 : 0);
            mDb.insert(TABLE_NAME, null, values);
        }

        public long createReminder(Reminder reminder) {
            ContentValues values = new ContentValues();
            values.put(COL_CONTENT, reminder.getContent());//连接名字
            values.put(COL_IMPORTANT, reminder.getImportant());

            return mDb.insert(TABLE_NAME, null, values);//插入行
        }

        //读取
        public Reminder fetchReminderById(int id) {
            Cursor cursor = mDb.query(TABLE_NAME, new String[]{COL_ID, COL_CONTENT, COL_IMPORTANT}, COL_ID + "=?",
                    new String[]{String.valueOf(id)}, null, null, null, null);
            if (cursor != null) {
                cursor.moveToFirst();

            }
            return new Reminder(
                    cursor.getInt(INDEX_ID),
                    cursor.getString(INDEX_CONTENT),
                    cursor.getInt(INDEX_IMPORTENT)
            );
        }

        public Cursor fetchAllReminders() {
            Cursor mCursor = mDb.query(TABLE_NAME,
                    new String[]{COL_ID, COL_CONTENT, COL_IMPORTANT}, null, null, null, null, null);
            if (mCursor != null) {
                mCursor.moveToFirst();
            }
            return mCursor;
        }

        //更新
        public void updateReminder(Reminder reminder) {
            ContentValues values = new ContentValues();
            values.put(COL_CONTENT, reminder.getContent());
            values.put(COL_IMPORTANT, reminder.getImportant());
            mDb.update(TABLE_NAME, values,
                    COL_ID + "=?", new String[]{String.valueOf(reminder.getmId())});
        }
        //删除

        public void deleteReminderById(int nId) {
            mDb.delete(TABLE_NAME, COL_ID + "=?", new String[]{String.valueOf(nId)});
        }

        public void deleteAllReminders() {
            mDb.delete(TABLE_NAME, null, null);

        }

}

所有的类都没有错,但是就是在实例化这个对象的时候出现了这个错误。

后来找到了解决的办法 我用别的类试试能不能实例化,巧合的是干好自己写的另一个类有构造函数一些就解决了

所以原来是我在写类的时候没哟写构造函数,所以导致了传入参数的时候没有正确;


修改后的RemiunderDbAdapter.java是

package com.example.reminders;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * Created by Administrator on 2016/12/24.
 */
class ReminderDbAdapter {
    public ReminderDbAdapter(Context context) {
        mContext = context;
    }//添加的地方,缺少了参数的构造器

    public static class DatabaseHelper extends SQLiteOpenHelper {
        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            Log.w(TAG, DATABASE_CREATE);
            db.execSQL(DATABASE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(TAG, "Downgrade database from  version" + oldVersion + "to" +
                    newVersion + ",which will destroy all old  data");
            db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
            onCreate(db);
        }
    }

        public static final String COL_ID = "id";
        public static final String COL_CONTENT = "content";
        public static final String COL_IMPORTANT = "important";

        public static final int INDEX_ID = 0;
        public static final int INDEX_CONTENT = INDEX_ID + 1;
        public static final int INDEX_IMPORTENT = INDEX_ID + 2;

        public static final String TAG = "RemindersDbAdapter";

        private DatabaseHelper mDbHelper;
        private SQLiteDatabase mDb;

        private static final String DATABASE_NAME = "dba_remder";
        private static final String TABLE_NAME = "tbl_remdrs";
        private static final int DATABASE_VERSION = 1;

        private Context mContext;

        public static final String DATABASE_CREATE = "CREATE TABLE if exits" + TABLE_NAME +
                "(" + COL_ID + "INTEGER PRIMARY KEY autioncrement," + COL_CONTENT + "TEXT,"
                + COL_IMPORTANT + "INTEGER);";

        /*
        * 如何使用DatabaseHelper来打开关闭数据库
        * 构造函数保存了一个Context实例
        * close()方法使用助手类来关闭数据库
        * */
        public void ReminderDbAdapter(Context context) {
            this.mContext = context;
        }

        //open
        public void open() throws SQLException {
            mDbHelper = new DatabaseHelper(mContext);
            mDb = mDbHelper.getWritableDatabase();
        }

        //close
        public void close() {
            if (mDbHelper != null) {
                mDbHelper.close();
            }
        }

        /*包含在tbl_remdrs表中处理Reminder对象创建、读取、更新、和删除操作的所有逻辑
        这些叫做GRUD(创建、读取、更新、和删除)操作
        *   ContentValues是一个数据梭,用于将数据值传递给数据库的insert方法
        *   cursor-》光标
        * */
        public void createReminder(String name, boolean important) {
            ContentValues values = new ContentValues();
            values.put(COL_CONTENT, name);
            values.put(COL_IMPORTANT, important ? 1 : 0);
            mDb.insert(TABLE_NAME, null, values);
        }

        public long createReminder(Reminder reminder) {
            ContentValues values = new ContentValues();
            values.put(COL_CONTENT, reminder.getContent());//连接名字
            values.put(COL_IMPORTANT, reminder.getImportant());

            return mDb.insert(TABLE_NAME, null, values);//插入行
        }

        //读取
        public Reminder fetchReminderById(int id) {
            Cursor cursor = mDb.query(TABLE_NAME, new String[]{COL_ID, COL_CONTENT, COL_IMPORTANT}, COL_ID + "=?",
                    new String[]{String.valueOf(id)}, null, null, null, null);
            if (cursor != null) {
                cursor.moveToFirst();

            }
            return new Reminder(
                    cursor.getInt(INDEX_ID),
                    cursor.getString(INDEX_CONTENT),
                    cursor.getInt(INDEX_IMPORTENT)
            );
        }

        public Cursor fetchAllReminders() {
            Cursor mCursor = mDb.query(TABLE_NAME,
                    new String[]{COL_ID, COL_CONTENT, COL_IMPORTANT}, null, null, null, null, null);
            if (mCursor != null) {
                mCursor.moveToFirst();
            }
            return mCursor;
        }

        //更新
        public void updateReminder(Reminder reminder) {
            ContentValues values = new ContentValues();
            values.put(COL_CONTENT, reminder.getContent());
            values.put(COL_IMPORTANT, reminder.getImportant());
            mDb.update(TABLE_NAME, values,
                    COL_ID + "=?", new String[]{String.valueOf(reminder.getmId())});
        }
        //删除

        public void deleteReminderById(int nId) {
            mDb.delete(TABLE_NAME, COL_ID + "=?", new String[]{String.valueOf(nId)});
        }

        public void deleteAllReminders() {
            mDb.delete(TABLE_NAME, null, null);

        }

}

下原图,这就是我错误的显示



让我好一阵郁闷,心里想:为什么 不都是对的代码了吗?为什么这里就是会错呢?花了时间查找资料,都是大段的代码,自己没有仔细对比,没有直接对问题的描述。

所以自己就记录了这个小小的错误。加油


修改:程序里面有很多错误比如定义id的时候一般是public static final String COL_ID = "id"------>public static final String COL_ID = "_id";

               有可能其他程序写的时候没有注意,所以程序出现菜鸟级别错误

(没有写好这边文章好可惜,后来实话了我很久的时间找错误,找到了错误的,但是忘记写记下来了)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值