我有一個應用程序,我試圖在使用SQLlite創建的表內添加用戶信息。問題是當我按下應用程序崩潰的添加按鈕時。波紋管我包括所有的代碼和logcat。SQLlite導致按鈕按下時崩潰
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的錯誤(分離成兩個IMGS)和activity_main視圖
檢查你是否不忘記某處的空間......所以不是「在我的代碼中查找錯字」服務 – Selvin
從表名中刪除.db擴展名。即只使用studentTable作爲你的表名 – Meenaxi
@selvin檢查了空格,他們似乎沒問題 – zamzam