添加和显示来自内部数据库 SQLite 的数据时强制关闭 Android

Force Close when Adding and Showing data from internal database SQLite Android

这是我的 MainActivity Class

public class MainActivity extends AppCompatActivity {

    TextView textView;
    String text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = (TextView) findViewById(R.id.textView);
        DatabaseHelper db = new DatabaseHelper(this);

        //inserting
        db.addMessage(new Message("Halo pesan 1", "group1"));
        db.addMessage(new Message("Halo pesan 2", "group1"));
        db.addMessage(new Message("Halo pesan 3", "group2"));
        db.addMessage(new Message("Halo pesan 4", "group2"));

        //reading and displaying all message
        List<Message> messageList = db.getAllMessage();

        for(Message m : messageList){
            String log = "ID " + m.getId() + ", Message: " + m.getMessageText() + ", Sender: " + m.getSender() + "\n";
            text = text + log;
        }

        textView.setText(messageList.toString());

    }
}

这是我的数据库Class

public class DatabaseHelper extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "messageManager";
    private static final String TABLE_MESSAGE = "m_message";
    private static final String KEY_ID = "id";
    private static final String KEY_MESSAGE = "messageText";
    private static final String KEY_SENDER = "sender";

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

    @Override
    public void onCreate(SQLiteDatabase db){
        String CREATE_MESSAGE_TABLE = " CREATE TABLE " + TABLE_MESSAGE + "("
                +KEY_ID + " INTEGER PRIMARY KEY , " + KEY_MESSAGE + "TEXT , "
                +KEY_SENDER + "TEXT" + " )";
        db.execSQL(CREATE_MESSAGE_TABLE);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i , int b){
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_MESSAGE);

        onCreate(db);
    }

    void addMessage(Message message){
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_MESSAGE, message.getMessageText());
        values.put(KEY_SENDER, message.getSender());

        db.insert(TABLE_MESSAGE,null,values);
        db.close();

    }

    public List<Message> getAllMessage(){
        List<Message> messageList = new ArrayList<>();

        String selectQuery = " Select * FROM " + TABLE_MESSAGE;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery,null);

        if (cursor.moveToFirst()){
            do{
                Message message = new Message();
                message.setId(Integer.parseInt(cursor.getString(0)));
                message.setMessageText(cursor.getString(1));
                message.setSender(cursor.getString(2));

                messageList.add(message);
            }while (cursor.moveToNext());
        }
        return messageList;
    }

}

这是我的模型class

public class Message {

    int id;
    String messageText;
    String sender;

    public Message(){

    }

    public Message(String messageText , String sender){
        this.messageText = messageText;
        this.sender = sender;

    }

    public Message(int id , String messageText , String sender){
        this.id = id;
        this.messageText = messageText;
        this.sender = sender;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getMessageText() {
        return messageText;
    }

    public void setMessageText(String messageText) {
        this.messageText = messageText;
    }

    public String getSender() {
        return sender;
    }

    public void setSender(String sender) {
        this.sender = sender;
    }


}

This is the debug log

我只是想将一些数据添加到数据库并将其显示到屏幕上。 运行的时候没有报错,调试的时候就出问题了。 我已经更改了数据库名称和 table 名称,但仍然强制关闭。 根据我的分析,创建数据库有问题,但我不知道如何解决。

替换oncreate CREATE_MESSAGE_TABLE 字符串

String CREATE_MESSAGE_TABLE = "CREATE TABLE " + TABLE_MESSAGE + "("
                +KEY_ID + " INTEGER PRIMARY KEY," + KEY_MESSAGE + " TEXT,"
                +KEY_SENDER + " TEXT" + ")";