这是用于学生安置活动的Java代码,每当我单击按钮而不填充字段时,在单击按钮之前它都没有显示任何语法错误或其他错误,或者在崩溃时都会填充字段,这是一个尝试学习android的初学者请提供帮助。
public class company_profile extends AppCompatActivity {
EditText title, address;
SQLiteDatabase sqlitedb;
Button Add;
String companytitle, companyaddress, SQLiteDataBaseQueryHolder;
Boolean EditTextEmpty;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_company_profile);
title = (EditText) findViewById(R.id.com_name);
address = (EditText) findViewById(R.id.com_address);
Add = (Button) findViewById(R.id.addComapny);
Add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDataBaseBuild();
SQLiteTableBuild();
CheckEditTextStatus();
InsertDataIntoSQLiteDatabase();
}
});
}
public void SQLiteDataBaseBuild() {
sqlitedb = openOrCreateDatabase("PlacementDb", Context.MODE_PRIVATE, null);
}
public void SQLiteTableBuild() {
sqlitedb.execSQL("CREATE TABLE IF NOT EXISTS Company(id INTEGER PRIMARY KEY AUTOINCREMENT, ctitle VARCHAR(255), caddress VARCHAR(255));");
}
public void CheckEditTextStatus() {
companytitle = title.getText().toString();
companyaddress = address.getText().toString();
if (TextUtils.isEmpty(companytitle) || TextUtils.isEmpty(companyaddress)) {
EditTextEmpty = false;
} else {
EditTextEmpty = true;
}
}
public void InsertDataIntoSQLiteDatabase(){
if(EditTextEmpty == true)
{
sqlitedb.execSQL("INSERT INTO Company (ctitle,caddress) VALUES('"+companytitle+"','"+companyaddress+"');");
sqlitedb.execSQL(SQLiteDataBaseQueryHolder);
Toast.makeText(this,"Data Inserted Successfully", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(this,"Please Fill All The Required Fields.", Toast.LENGTH_LONG).show();
}
}
}
这是一个错误日志:
FATAL EXCEPTION: main
Process: com.example.studentplacement
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.trim()' on a null object reference
答案 0 :(得分:0)
可能是如何实现SQLite数据库的。尝试像这样使用它
DatabaseHelper.java
package YOUR_PACKAGE_NAME;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.HashMap;
public class DatabaseHelper extends SQLiteOpenHelper {
ArrayList<HashMap<String, String>> YourList = new ArrayList<>();
public static final String DATABASE_NAME = "MyDBName.db";
public static final String CONTACTS_TABLE_NAME = "Your_TableName";
public static final String CONTACTS_COLUMN1 = "column1";
public static final String CONTACTS_COLUMN2 = "column2";
public static final String CONTACTS_COLUMN3 = "column3";
private HashMap hp;
public DatabaseHelper(Context context){
super(context ,DATABASE_NAME,null,1);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(
"create table Your_TableName(column1 VARCHAR,column2 VARCHAR,column3 VARCHAR)"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS Your_TableName");
onCreate(db);
}
//Insert Data to SQLite database
public boolean insertData(String column1, String column2 ,String column3) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("column1", column1);
contentValues.put("column2", column2);
contentValues.put("column2", column3);
db.insert("Your_TableName", null, contentValues);
return true;
}
//delete all data in SQLite database
public Cursor deleteData() {
SQLiteDatabase db = this.getReadableDatabase();
db.execSQL("delete FROM Your_TableName");
return null;
}
//counting the number of items in your database
public int numberOfRows(){
SQLiteDatabase db = this.getReadableDatabase();
int numRows = (int) DatabaseUtils.queryNumEntries(db, CONTACTS_TABLE_NAME);
return numRows;
}
//Get all item in your database
public ArrayList<HashMap<String, String>> getAllDatabaseData() {
//hp = new HashMap();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from Your_TableName", null );
res.moveToFirst();
while(res.isAfterLast() == false){
HashMap<String, String> array_list = new HashMap<>();
array_list.put("column1",res.getString(res.getColumnIndex(CONTACTS_COLUMN1)));
array_list.put("column2",res.getString(res.getColumnIndex(CONTACTS_COLUMN2)));
array_list.put("column3",res.getString(res.getColumnIndex(CONTACTS_COLUMN3)));
YourList.add(array_list);
res.moveToNext();
}
return YourList;
}
}
,然后在您的“活动”中实施此操作。
public class company_profile extends AppCompatActivity {
EditText title, address;
SQLiteDatabase sqlitedb;
Button Add;
String companytitle, companyaddress;
Boolean EditTextEmpty;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_company_profile);
title = (EditText) findViewById(R.id.com_name);
address = (EditText) findViewById(R.id.com_address);
Add = (Button) findViewById(R.id.addComapny);
Add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
column1= title.getText().toString();
column2= address.getText().toString();
new DatabaseHelper(this).insertData(column1, column2, column3); //calling the method [inserData] in DatabaseHelper Class
}
});
}
}