当我尝试在Android SQLite数据库中添加新值时,我的应用程序退出

时间:2015-08-21 14:07:55

标签: java android sqlite crash

我想创建一个像记事本这样的应用程序但是有一个我看不到的问题。当我点击“OK”按钮时,应用关闭。有我的代码:

MainActivity.java:

package com.example.administrateur.notepad;

import android.view.MenuInflater;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends Activity {

DatabaseHelper myDb;

private EditText titre,texte;
private Button ajoutNote;

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

    myDb = new DatabaseHelper(this);

    titre = (EditText)findViewById(R.id.titre);
    texte = (EditText)findViewById(R.id.texte);
    ajoutNote = (Button)findViewById(R.id.ajoutNote);

    addData();
}


public void addData(){
    ajoutNote.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            boolean isInserted = myDb.insertData(titre.getText().toString(), texte.getText().toString());
            if (isInserted)
                Toast.makeText(MainActivity.this, "Data inserted", Toast.LENGTH_LONG).show();
            else
                Toast.makeText(MainActivity.this, "Data not inserted", Toast.LENGTH_LONG).show();
        }
    });
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_main, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

DatabaseHelper.java:

package com.example.administrateur.notepad;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class DatabaseHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "notepad.db";
public static final String TABLE_NAME = "notepad_table";
public static final String COL_1 = "ID";
public static final String COL_2 = "TITRE";
public static final String COL_3 = "TEXTE";

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

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table" + TABLE_NAME + "(ID INTERGER PRIMARY KEY AUTOINCREMENT, TITRE TEXT, TEXTE TEXT)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
    onCreate(db);
}

public boolean insertData(String titre, String texte){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_2, titre);
    contentValues.put(COL_3, texte);
    long result = db.insert(TABLE_NAME,null, contentValues);
    if (result == -1)
        return false;
    else
        return true;
}
}

activity_main.xml:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
    android:id="@+id/titre"
    android:hint="@string/titre"
    android:textSize="20sp"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

<EditText
    android:id="@+id/texte"
    android:hint="@string/note"
    android:textSize="20sp"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/ajoutNote"
    android:id="@+id/ajoutNote"/>

感谢您阅读;)

1 个答案:

答案 0 :(得分:0)

我发现我的错误,抱歉添加了一个包含此类错误的帖子。在DatabaseHelper.java中,我写了“ID INTERGER PRIMARY KEY AUTOINCREMENT”而不是“ID INTEGER PRIMARY KEY AUTOINCREMENT”... 谢谢;)