调用类实例化为什么添加参数this会错误
来源:互联网 发布:企业怎么做网络销售 编辑:程序博客网 时间:2024/05/18 02:10
在开发我的小项目备忘本(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
- 调用类实例化为什么添加参数this会错误
- 类是如何实例化为对象的
- 使用DataContractJsonSerializer类将类型实例序列化为JSON字符串和反序列化为实例对象
- java 中this调用静态变量及实例变量的若干错误
- 实例化为子物体
- 给族添加实例参数
- 压缩感知中的lp球:p范数最优化为什么总会导致一个稀疏的解的原因
- 压缩感知中的lp球:p范数最优化为什么总会导致一个稀疏的解的原因
- 压缩感知中的lp球:p范数最优化为什么总会导致一个稀疏的解的原因
- 压缩感知中的lp球:p范数最优化为什么总会导致一个稀疏的解的原因
- 函数变参数调用实例
- Activity报错退出会调用什么生命周期
- 调用函数前未声明会出现什么情况?
- Fragment hide,show方法后,会调用什么方法
- 两个线程同时调用一个函数会出现什么情况?
- 两个线程同时调用一个函数会出现什么情况?
- Fragment hide,show方法后,会调用什么方法
- javascript 简易实现,命名空间,静态类,动态类 及调用实例,找不到this
- Android学习笔记--事件分发机制
- 奇怪的找不到类SpringNamingPolicy的错误
- 解决开机弹出系统属性框:由于启动计算机时出现了页面配置问题
- Leetcode Populating Next Right Pointers in Each Node
- Unix 命令之tar
- 调用类实例化为什么添加参数this会错误
- Exporting points in catia into .txt file or to Excel sheet
- 欢迎使用CSDN-markdown编辑器
- Redis服务的集中监控
- Insertion Sort List
- 项目整理(三)——物流
- 缓存、缓存框架、缓存算法讲解
- ios 约束(五)
- java中对象创建、初始化、引用