I have an application in which I'm trying to add user info inside a table created using SQLlite. The problem is when I press the add button the application crashes. bellow I include all the code and logcat.
MainActivity.java
package com.example.asus.sqlliteproject;
import android.support.v7.app.AppCompatActivity;
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 AppCompatActivity {
DataBaseHelper myDB;
EditText Name,LastName,Grades;
Button AddData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDB = new DataBaseHelper(this);
Name = (EditText)findViewById(R.id.name);
LastName = (EditText)findViewById(R.id.lastName);
Grades = (EditText)findViewById(R.id.Grades);
AddData = (Button)findViewById(R.id.addDataButton);
addData();
}
public void addData () {
AddData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean inserted = myDB.insertData(Name.getText().toString(),
LastName.getText().toString(),
Grades.getText().toString());
if (inserted){
Toast.makeText(MainActivity.this,"Text Inserted!", Toast.LENGTH_SHORT).show();
} else
Toast.makeText(MainActivity.this,"Unsuccessful!", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@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.asus.sqlliteproject;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by Asus on 29.4.2016.
*/
public class DataBaseHelper extends SQLiteOpenHelper {
public static final String DataBaseName = "student.db";
public static final String DataTableName = "studentTable.db";
public static final String ColID = "ID";
public static final String ColName = "Name";
public static final String ColLastName = "LastName";
public static final String ColGrades = "Grade";
public DataBaseHelper(Context context) {
super(context, DataBaseName, null, 1);
//SQLiteDatabase db = this.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table" + DataTableName + "(ID INTEGER PRIMARY KEY AUTOINCREMENT)," +
"Name TEXT, LastName TEXT, Grade INTEGER");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("Drop table if exists" + DataTableName);
onCreate(db);
}
public boolean insertData (String name, String LastName, String Grades) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(ColName,name);
contentValues.put(ColLastName,LastName);
contentValues.put(ColGrades,Grades);
long result = db.insert(DataTableName, null, contentValues);
if (result == -1) {
return false;
}else
return true;
}
}
logcat error (separated into two imgs) and activity_main view
Aucun commentaire:
Enregistrer un commentaire