Android Can';t实例化类,无空构造函数,SQLiteHelper
Android Can';t实例化类,无空构造函数,SQLiteHelper,android,sqlite,Android,Sqlite,我试图用SQLite做一个Android应用程序,但当我试图在智能手机上启动我的应用程序时,它会出现一个白色屏幕,然后崩溃 因此,我查看了LogCat,它显示了以下错误: 04-18 01:33:32.186: E/AndroidRuntime(26741): FATAL EXCEPTION: main 04-18 01:33:32.186: E/AndroidRuntime(26741): java.lang.RuntimeException: Unable to instantiate ac
我试图用SQLite做一个Android应用程序,但当我试图在智能手机上启动我的应用程序时,它会出现一个白色屏幕,然后崩溃 因此,我查看了LogCat,它显示了以下错误:
04-18 01:33:32.186: E/AndroidRuntime(26741): FATAL EXCEPTION: main
04-18 01:33:32.186: E/AndroidRuntime(26741): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.sqliteexample/database.MySQLiteHelper}: java.lang.InstantiationException: can't instantiate class database.MySQLiteHelper; no empty constructor
04-18 01:33:32.186: E/AndroidRuntime(26741): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2034)
04-18 01:33:32.186: E/AndroidRuntime(26741): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
04-18 01:33:32.186: E/AndroidRuntime(26741): at android.app.ActivityThread.access$700(ActivityThread.java:140)
04-18 01:33:32.186: E/AndroidRuntime(26741): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
04-18 01:33:32.186: E/AndroidRuntime(26741): at android.os.Handler.dispatchMessage(Handler.java:99)
04-18 01:33:32.186: E/AndroidRuntime(26741): at android.os.Looper.loop(Looper.java:137)
04-18 01:33:32.186: E/AndroidRuntime(26741): at android.app.ActivityThread.main(ActivityThread.java:4921)
04-18 01:33:32.186: E/AndroidRuntime(26741): at java.lang.reflect.Method.invokeNative(Native Method)
04-18 01:33:32.186: E/AndroidRuntime(26741): at java.lang.reflect.Method.invoke(Method.java:511)
04-18 01:33:32.186: E/AndroidRuntime(26741): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
04-18 01:33:32.186: E/AndroidRuntime(26741): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
04-18 01:33:32.186: E/AndroidRuntime(26741): at dalvik.system.NativeStart.main(Native Method)
04-18 01:33:32.186: E/AndroidRuntime(26741): Caused by: java.lang.InstantiationException: can't instantiate class database.MySQLiteHelper; no empty constructor
04-18 01:33:32.186: E/AndroidRuntime(26741): at java.lang.Class.newInstanceImpl(Native Method)
04-18 01:33:32.186: E/AndroidRuntime(26741): at java.lang.Class.newInstance(Class.java:1319)
04-18 01:33:32.186: E/AndroidRuntime(26741): at android.app.Instrumentation.newActivity(Instrumentation.java:1068)
04-18 01:33:32.186: E/AndroidRuntime(26741): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2025)
04-18 01:33:32.186: E/AndroidRuntime(26741): ... 11 more
下面是MySQLiteHelper类:package database;
import android.util.Log;
import android.annotation.SuppressLint;
import android.content.Context;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
@SuppressLint("NewApi")
public class MySQLiteHelper extends SQLiteOpenHelper {
public static final String TABLE_COMMENTS = "comments";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_COMMENT = "comment";
private static final String DATABASE_NAME = "commments.db";
private static final int DATABASE_VERSION = 1;
// Database creation sql statement
private static final String DATABASE_CREATE = "create table "
+ TABLE_COMMENTS + "(" + COLUMN_ID
+ " integer primary key autoincrement, " + COLUMN_COMMENT
+ " text not null);";
public MySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(MySQLiteHelper.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_COMMENTS);
onCreate(db);
}
}
以下是TestDatabaseActivity类:
package activity;
import java.util.List;
import java.util.Random;
import model.Comment;
import dao.CommentsDataSource;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import com.example.sqliteexample.R;
public class TestDatabaseActivity extends ListActivity {
private CommentsDataSource datasource;
public TestDatabaseActivity(){
super();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_sqlite_helper);
datasource = new CommentsDataSource(this);
datasource.open();
List<Comment> values = datasource.getAllComments();
// Use the SimpleCursorAdapter to show the
// elements in a ListView
ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this,
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
// Will be called via the onClick attribute
// of the buttons in main.xml
public void onClick(View view) {
@SuppressWarnings("unchecked")
ArrayAdapter<Comment> adapter = (ArrayAdapter<Comment>) getListAdapter();
Comment comment = null;
switch (view.getId()) {
case R.id.add:
String[] comments = new String[] { "Cool", "Very nice", "Hate it" };
int nextInt = new Random().nextInt(3);
// Save the new comment to the database
comment = datasource.createComment(comments[nextInt]);
adapter.add(comment);
break;
case R.id.delete:
if (getListAdapter().getCount() > 0) {
comment = (Comment) getListAdapter().getItem(0);
datasource.deleteComment(comment);
adapter.remove(comment);
}
break;
}
adapter.notifyDataSetChanged();
}
@Override
protected void onResume() {
datasource.open();
super.onResume();
}
@Override
protected void onPause() {
datasource.close();
super.onPause();
}
}
包活动;
导入java.util.List;
导入java.util.Random;
导入模型。评论;
导入dao.CommentsDataSource;
导入android.app.ListActivity;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.ArrayAdapter;
导入com.example.sqliteexample.R;
公共类TestDatabaseActivity扩展了ListActivity{
私有评论数据源;
公共TestDatabaseActivity(){
超级();
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u my\u sqlite\u helper);
数据源=新注释数据源(此);
datasource.open();
列表值=datasource.getAllComments();
//使用SimpleCursorAdapter显示
//ListView中的元素
ArrayAdapter=新的ArrayAdapter(此,
android.R.layout.simple_list_item_1,值);
setListAdapter(适配器);
}
//将通过onClick属性调用
//main.xml中按钮的
公共void onClick(视图){
@抑制警告(“未选中”)
ArrayAdapter=(ArrayAdapter)getListAdapter();
注释=null;
开关(view.getId()){
案例R.id.add:
String[]comments=newstring[]{“Cool”、“Very nice”、“Hate it”};
int nextInt=new Random().nextInt(3);
//将新注释保存到数据库中
comment=datasource.createComment(comments[nextInt]);
添加(注释);
打破
案例R.id.delete:
如果(getListAdapter().getCount()>0){
comment=(comment)getListAdapter().getItem(0);
datasource.deleteComment(comment);
移除(注释);
}
打破
}
adapter.notifyDataSetChanged();
}
@凌驾
受保护的void onResume(){
datasource.open();
super.onResume();
}
@凌驾
受保护的void onPause(){
datasource.close();
super.onPause();
}
}
CommentsDataSource类:
import java.util.ArrayList;
import java.util.List;
import database.MySQLiteHelper;
import model.Comment;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
public class CommentsDataSource {
private SQLiteDatabase database;
private MySQLiteHelper dbHelper;
private String[] allColumns = { MySQLiteHelper.COLUMN_ID,
MySQLiteHelper.COLUMN_COMMENT };
public CommentsDataSource(Context context) {
dbHelper = new MySQLiteHelper(context);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
public Comment createComment(String comment) {
ContentValues values = new ContentValues();
values.put(MySQLiteHelper.COLUMN_COMMENT, comment);
long insertId = database.insert(MySQLiteHelper.TABLE_COMMENTS, null,
values);
Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS,
allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null,
null, null, null);
cursor.moveToFirst();
Comment newComment = cursorToComment(cursor);
cursor.close();
return newComment;
}
public void deleteComment(Comment comment) {
long id = comment.getId();
System.out.println("Comment deleted with id: " + id);
database.delete(MySQLiteHelper.TABLE_COMMENTS, MySQLiteHelper.COLUMN_ID
+ " = " + id, null);
}
public List<Comment> getAllComments() {
List<Comment> comments = new ArrayList<Comment>();
Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS,
allColumns, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Comment comment = cursorToComment(cursor);
comments.add(comment);
cursor.moveToNext();
}
// Make sure to close the cursor
cursor.close();
return comments;
}
private Comment cursorToComment(Cursor cursor) {
Comment comment = new Comment();
comment.setId(cursor.getLong(0));
comment.setComment(cursor.getString(1));
return comment;
}
}
import java.util.ArrayList;
导入java.util.List;
导入database.MySQLiteHelper;
导入模型。评论;
导入android.content.ContentValues;
导入android.content.Context;
导入android.database.Cursor;
导入android.database.SQLException;
导入android.database.sqlite.SQLiteDatabase;
公共类CommentsDataSource{
专用数据库;
私有MySQLiteHelper-dbHelper;
私有字符串[]allColumns={MySQLiteHelper.COLUMN\u ID,
MySQLiteHelper.COLUMN_COMMENT};
公共评论数据源(上下文){
dbHelper=新的MySQLiteHelper(上下文);
}
public void open()引发SQLException{
database=dbHelper.getWritableDatabase();
}
公众假期结束(){
dbHelper.close();
}
公共注释createComment(字符串注释){
ContentValues=新的ContentValues();
value.put(MySQLiteHelper.COLUMN_COMMENT,COMMENT);
long insertId=database.insert(MySQLiteHelper.TABLE_注释,null,
价值观);
Cursor Cursor=database.query(MySQLiteHelper.TABLE_注释,
allColumns,MySQLiteHelper.COLUMN_ID+“=”+insertId,null,
空,空,空);
cursor.moveToFirst();
Comment newComment=cursorToComment(光标);
cursor.close();
返回新成员;
}
公共无效删除注释(注释注释){
long id=comment.getId();
System.out.println(“用id:+id删除注释”);
delete(MySQLiteHelper.TABLE_注释,MySQLiteHelper.COLUMN_ID
+“=”+id,空);
}
公共列表getAllComments(){
列表注释=新建ArrayList();
Cursor Cursor=database.query(MySQLiteHelper.TABLE_注释,
所有列,null,null,null,null,null,null);
cursor.moveToFirst();
而(!cursor.isAfterLast()){
注释注释=游标注释(游标);
注释。添加(注释);
cursor.moveToNext();
}
//确保关闭光标
cursor.close();
返回评论;
}
专用注释光标或注释(光标){
注释=新注释();
comment.setId(cursor.getLong(0));
comment.setComment(cursor.getString(1));
回复评论;
}
}
我发现java.lang.InstantiationException:无法实例化类database.MySQLiteHelper;没有空构造函数
,
但是我不知道如何修复它。错误很明显-您没有编写无参数构造函数,但代码需要一个。我尝试了
public MySQLite(){super(context,DATABASE_NAME,null,DATABASE_VERSION);}
但它给了我错误。。