我一直在尝试将Spinner的静态数据存储到我的数据库中。我静态地设置了文本,click事件也没有读取数据。
这是我的代码:
ManageUsersActivity.java
public class ManageUsersActivity extends Activity implements View.OnClickListener{
DatabaseHelper helper;
EditText userName,userPassword, userId;
Spinner spinUserType;
String spinValue;
Button btnAddNewUser, btnEditUser, btnDelUser, btnViewAllUser;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_manage_users);
helper=new DatabaseHelper(this);
userName=(EditText)findViewById(R.id.userName);
userPassword=(EditText)findViewById(R.id.userPassword);
btnAddNewUser=(Button)findViewById(R.id.btnAddNewUser);
btnEditUser=(Button)findViewById(R.id.btnEditUser);
btnDelUser=(Button)findViewById(R.id.btnDelUser);
btnViewAllUser=(Button)findViewById(R.id.btnViewAllUser);
spinUserType=(Spinner)findViewById(R.id.spinUserType);
spinUserType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3){
if(spinUserType.getSelectedItem().equals("Rector")){
spinValue="Rector";
}
else if(spinUserType.getSelectedItem().equals("Student")){
spinValue="Student";
}
}
public void onNothingSelected(AdapterView<?> arg0){
spinValue="Rector";
}
});
btnAddNewUser.setOnClickListener(this);
btnEditUser.setOnClickListener(this);
btnDelUser.setOnClickListener(this);
btnViewAllUser.setOnClickListener(this);
}
public void onClick(View v){
if(v == btnAddNewUser){
if(userName.getText().toString().trim().length()==0||
userPassword.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter all values");
return;
}
}
boolean isInserted = helper.insertUserValue(userId.getText().toString(), userName.getText().toString(),
userPassword.getText().toString(), spinValue);
if(isInserted){
Toast.makeText(ManageUsersActivity.this,"Data Inserted",Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(ManageUsersActivity.this,"Data Not Inserted",Toast.LENGTH_SHORT).show();
}
clearText();
}
public void showMessage(String title,String message)
{
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
public void clearText()
{
userName.setText("");
userPassword.setText("");
userName.requestFocus();
}
}
这是我的 DatabaseHelper.java
public class DatabaseHelper extends SQLiteOpenHelper {
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "mmsDB";
//Tables Names
private static final String TABLE_ADMIN="admin_tbl";
private static final String TABLE_USER="user_tbl";
//Columns of TABLE_ADMIN
private static final String ADMIN_ID="a_id";
private static final String ADMIN_NAME="a_name";
private static final String ADMIN_PASSWORD="a_password";
//Columns of TABLE_USER
private static final String USER_ID="u_id";
private static final String USER_NAME="u_name";
private static final String USER_PASSWORD="u_password";
private static final String USER_TYPE="u_type";
// Create statements for Tables
private static final String CREATE_TABLE_ADMIN = "CREATE TABLE "+ TABLE_ADMIN +
"("+ADMIN_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "
+ADMIN_NAME+" TEXT, "
+ADMIN_PASSWORD+" TEXT" + ")";
private static final String CREATE_TABLE_USER = "CREATE TABLE "+ TABLE_USER +
"("+USER_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "
+USER_NAME+" TEXT, "
+USER_PASSWORD+" TEXT, "
+USER_TYPE+" TEXT" + ")";
public DatabaseHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// Creating tables
db.execSQL(CREATE_TABLE_ADMIN);
db.execSQL("insert into " +TABLE_ADMIN+ "(" + ADMIN_ID + ","
+ ADMIN_NAME + "," +ADMIN_PASSWORD+") values(1,'admin','root')");
db.execSQL(CREATE_TABLE_USER);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// On upgrade drop older tables
db.execSQL("DROP TABLE IF EXISTS "+TABLE_ADMIN);
db.execSQL("DROP TABLE IF EXISTS "+TABLE_USER);
// Create new tables
onCreate(db);
}
// admin_tbl methods
public Cursor getAllAdminValues(){
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from "+TABLE_ADMIN,null);
return res;
}
// user_tbl methods
public boolean insertUserValue(String u_id, String u_name, String u_password, String u_type){
//Inserting Records
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put(USER_ID, u_id);
contentValues.put(USER_NAME,u_name);
contentValues.put(USER_PASSWORD,u_password);
contentValues.put(USER_TYPE,u_type);
long result = db.insert(TABLE_USER,null,contentValues);
if(result == -1){
return false;
}
else{
return true;
}
}
public Cursor getAllRectorValues(){
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("",null);
return res;
}
public Cursor getAllStudentValues(){
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("",null);
return res;
}
}
这是我的 Logcat
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.mms, PID: 10637
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.mms.ManageUsersActivity.onClick(ManageUsersActivity.java:68)
at android.view.View.performClick(View.java:5269)
at android.view.View$PerformClick.run(View.java:21556)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5769)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
答案 0 :(得分:0)
您是否尝试将用户实际点击的微调器的内容分配给您要插入的spinValue字符串? 像
这样的东西spinValue = spinUserType.getSelectedItem().toString();
答案 1 :(得分:0)
问题出在这里:
boolean isInserted = helper.insertUserValue(userId.getText().toString(), userName.getText().toString(),
userPassword.getText().toString(), spinValue);
您尚未初始化Editext
userId
首先尝试在onCreate()
初始化
userId=(EditText)findViewById(R.id.userId);
答案 2 :(得分:0)
在手机中安装apk后,我添加了新表格(将其用作虚拟设备) 每次修改数据库时,我们都需要手动增加DATABASE_VERSION。