每次點擊頁面(按鈕更新,inschrijven,showallpeople或刪除)之後,每個按鈕都與SQL數據庫..有關。我不知道爲什麼,但我認爲它與列名有關。我不明白!在我的sql數據庫中插入數據後應用程序崩潰
所以這裏是我所有頁面的代碼。
這裏是我SQLiteHelper類:
package com.example.cedri.myapplication;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by cedri on 17/05/2016.
*/
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME= "KIND_DATABASE.db";
public static final String TABLE_NAME= "KIND_TABLE";
public static final int DATABASE_VERSION=1;
public static final String COL_1="ID";
public static final String COL_2="VOORNAAM";
public static final String COL_3="NAAM";
public static final String COL_4="LEEFTIJD";
public static final String COL_5="EMAIL";
private static final String CREATE_TABLE_KIND= "create table "+TABLE_NAME + " (" +
COL_1 +" INTEGER PRIMARY KEY AUTOINCREMENT," +
COL_2+" TEXT," +
COL_3+ " TEXT," +
COL_4+ " INTEGER," +
COL_5+" TEXT);";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_KIND);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS"+TABLE_NAME);
onCreate(db);
}
public boolean insertData(String voornaam,String naam,Integer leeftijd,String email){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put(COL_2,voornaam);
contentValues.put(COL_3,naam);
contentValues.put(COL_4,leeftijd);
contentValues.put(COL_5, email);
long result= db.insert(TABLE_NAME,null,contentValues);
if(result==-1){
return false;
}
else{
return true;
}
}
public Cursor getAllData(){
SQLiteDatabase db=this.getWritableDatabase();
Cursor res=db.rawQuery("select * from "+TABLE_NAME,null);
return res;
}
public Integer deleteData(String email){
SQLiteDatabase db=this.getWritableDatabase();
return db.delete(TABLE_NAME,"EMAIL=?",new String[]{email});
}
public boolean updateData(String voornaam, String naam, Integer leeftijd, String email){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2,voornaam);
contentValues.put(COL_3,naam);
contentValues.put(COL_4,leeftijd);
contentValues.put(COL_5, email);
db.update(TABLE_NAME,contentValues,"EMAIL = ?",new String[]{email});
return true;
}
}
這裏是我的MainActivity類別:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener,menu3Fragment.OnMainFragmentInteractionListener {
DatabaseHelper myDb;
TextView txtVoornaam,txtNaam,txtLeeftijd,txtEmail;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
myDb = new DatabaseHelper(this);
}
public void btnUpdate_Click(View view){
boolean isUpdate = myDb.updateData(txtVoornaam.getText().toString(),
txtNaam.getText().toString(),
Integer.valueOf(txtLeeftijd.getText().toString()),
txtEmail.getText().toString());
if(isUpdate==true){
Toast.makeText(MainActivity.this,"De data is geupdate",Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(MainActivity.this,"De data is niet geupdate",Toast.LENGTH_LONG).show();
}
}
public void btnDelete_Click(View view){
Integer isDelete = myDb.deleteData(
txtEmail.getText().toString());
if(isDelete>1){
Toast.makeText(MainActivity.this,"De data is verwijderd",Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(MainActivity.this,"De data is niet verwijderd",Toast.LENGTH_LONG).show();
}
}
public void btnGaNaarIngeschrevenLeden_Click(View view) {
Cursor res=myDb.getAllData();
if(res.getCount() == 0){
//show message
showMessage("Error", "Er werd geen data gevonden");
return;
}
StringBuffer buffer = new StringBuffer();
while(res.moveToNext()){
buffer.append("Id :"+ res.getString(0) + "\n");
buffer.append("Voornaam :"+ res.getString(1) + "\n");
buffer.append("Naam :"+ res.getString(2) + "\n");
buffer.append("Leeftijd :"+ res.getInt(3) + "\n");
buffer.append("Email :"+ res.getString(4) + "\n\n");
}
//show all data
showMessage("Ingeschreven kinderen 2016", buffer.toString());
}
public void btnSchrijfIn_Click(View view) {
txtVoornaam= (TextView)findViewById(R.id.Voornaam);
txtNaam= (TextView)findViewById(R.id.Naam);
txtLeeftijd= (TextView)findViewById(R.id.Leeftijd);
txtEmail= (TextView)findViewById(R.id.Email);
boolean isInsertData =
myDb.insertData(
txtVoornaam.getText().toString(),
txtNaam.getText().toString(),
Integer.valueOf(txtLeeftijd.getText().toString()),
txtEmail.getText().toString());
if(isInsertData==true){
//sendEmail();
Toast.makeText(MainActivity.this,"Uw kind werd succesvol ingeschreven!", Toast.LENGTH_LONG).show();
}
else
{
TextView foutmelding2=(TextView)findViewById(R.id.Foutmelding);
foutmelding2.setVisibility(TextView.VISIBLE);
}
Fragment fragment = new menu1Fragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction =fragmentManager.beginTransaction();
transaction.replace(R.id.main_content,fragment);
transaction.addToBackStack(null);
transaction.commit();
}
這是我的佈局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.cedri.chiroeine.InschrijvenPage"
android:background="@color/backgroundzwart">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="@string/Inschrijving_titel"
android:drawableLeft="@drawable/test"
android:layout_marginLeft="45dp"
android:layout_marginRight="20dp"
android:layout_marginTop="15dp"
android:textSize="20dp"
android:textColor="@color/achtergrond_inputvak"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="20dp"
android:layout_marginTop="50dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Inschrijving_inputvak1"
android:textColor="@color/bordeaurood"
android:textSize="20dp"/>
<EditText
android:layout_width="150dp"
android:layout_height="30dp"
android:layout_marginLeft="58dp"
android:layout_marginRight="15dp"
android:background="@color/achtergrond_inputvak"
android:id="@+id/Voornaam"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Inschrijving_inputvak2"
android:textColor="@color/bordeaurood"
android:textSize="20dp"/>
<EditText
android:layout_width="150dp"
android:layout_height="30dp"
android:layout_marginLeft="95dp"
android:layout_marginRight="15dp"
android:background="@color/achtergrond_inputvak"
android:id="@+id/Naam"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Inschrijving_inputvak3"
android:textColor="@color/bordeaurood"
android:textSize="20dp"/>
<EditText
android:layout_width="150dp"
android:layout_height="30dp"
android:layout_marginLeft="82dp"
android:layout_marginRight="15dp"
android:background="@color/achtergrond_inputvak"
android:id="@+id/Leeftijd"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Inschrijving_inputvak4"
android:textColor="@color/bordeaurood"
android:textSize="20dp"/>
<EditText
android:layout_width="150dp"
android:layout_height="30dp"
android:layout_marginLeft="100dp"
android:layout_marginRight="15dp"
android:background="@color/achtergrond_inputvak"
android:id="@+id/Email"/>
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_marginLeft="25dp"
android:layout_marginRight="75dp"
android:layout_marginTop="35dp"
android:textColor="@color/rooderror"
android:id="@+id/Foutmelding"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Inschrijving_btn_SchrijfIn"
android:background="@drawable/rounded_btn"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:layout_marginLeft="200dp"
android:id="@+id/SchrijfIn"
android:drawableLeft="@drawable/arrow"
android:onClick="btnSchrijfIn_Click"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Inschrijving_btn_GaNaarIngeschrevenLeden"
android:background="@drawable/rounded_btn"
android:paddingLeft="5dp" android:paddingRight="5dp"
android:layout_marginLeft="10dp"
android:onClick="btnGaNaarIngeschrevenLeden_Click"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/UpdateText"
android:background="@drawable/rounded_btn"
android:layout_marginLeft="10dp"
android:paddingLeft="5dp" android:paddingRight="5dp"
android:onClick="btnUpdate_Click"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/DeleteText"
android:background="@drawable/rounded_btn"
android:layout_marginLeft="10dp"
android:paddingLeft="5dp" android:paddingRight="5dp"
android:onClick="btnDelete_Click"/>
</LinearLayout>
</LinearLayout>
顯示我們的日誌。 沒有人閱讀logcat,沒有人不能猜測你的應用程序內部會發生什麼。 –
歡迎來到StackOverflow!爲了幫助他人理解您的問題,請發佈代碼示例,任何日誌的輸出(例如LogCat)或其他內容以演示[最小,完整和可驗證的示例](http://stackoverflow.com/help/ mcve)你的問題 –
但我嘗試過,但他們認爲它是垃圾郵件,是一個很好的截圖嗎? – peukertje