我正在尝试使用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();
    }
}

CommentDataSource类:

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;
    }

}

我发现了java.lang.InstantiationException: can't instantiate class database.MySQLiteHelper; no empty constructor java.lang.InstantiationException: can't instantiate class database.MySQLiteHelper; no empty constructor ,但是我不知道如何解决它。

===============>>#1 票数:1 已采纳

错误很明显-您没有编写no-arg构造函数,但是代码需要一个。

  ask by DamianFox translate from so

未解决问题?本站智能推荐: