logcat在这里:
list view activity --> clicked item(admin_parents_item) ---> option selected EDIT (current activity)
当我单击按钮来更新/编辑数据库时,它崩溃了。缺了点什么。如何解决此错误?
03-08 06:44:57.163 11022-11022/edu.angelo.parentsportal E/AndroidRuntime: FATAL EXCEPTION: main
Process: edu.angelo.parentsportal, PID: 11022
java.lang.NullPointerException: Attempt to invoke virtual method 'void edu.angelo.parentsportal.DatabaseHelper.updateData(int, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)' on a null object reference
at edu.angelo.parentsportal.Admin_Parents_Item_Edit$1.onClick(Admin_Parents_Item_Edit.java:63)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
admin_parent__item_edit.java(我当前的活动)
package edu.angelo.parentsportal;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Admin_Parents_Item_Edit extends AppCompatActivity {
Button btn_update;
EditText ed_Name;
EditText ed_Surname;
EditText ed_Email;
EditText ed_Phone_Number;
EditText ed_Password;
String s_Id;
String s_Name;
String s_Surname;
String s_Email;
String s_Phone_Number;
String s_Password;
Integer getId;
DatabaseHelper databaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin__parents__item__edit);
ed_Name = findViewById(R.id.ed_name_update);
ed_Surname = findViewById(R.id.ed_surname_update);
ed_Email = findViewById(R.id.ed_email_update);
ed_Phone_Number = findViewById(R.id.ed_PhoneNumber_update);
ed_Password = findViewById(R.id.ed_password_update);
Intent intent = getIntent();
s_Id = intent.getStringExtra("ID");
s_Name = intent.getStringExtra("Name");
s_Surname = intent.getStringExtra("Surname");
s_Email = intent.getStringExtra("Email Address");
s_Phone_Number = intent.getStringExtra("Phone number");
s_Password = intent.getStringExtra("Password");
getId = Integer.parseInt(s_Id);
ed_Name.setText(s_Name);
ed_Surname.setText(s_Surname);
ed_Email.setText(s_Email);
ed_Phone_Number.setText(s_Phone_Number);
ed_Password.setText(s_Password);
btn_update = findViewById(R.id.btn_admin_parent_update);
btn_update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
databaseHelper.updateData(getId,
ed_Name.getText().toString(),
ed_Surname.getText().toString(),
ed_Email.getText().toString(),
ed_Phone_Number.getText().toString(),
ed_Password.getText().toString());
finish();
}
});
}
}
Admin_parent_Item(我之前的活动)
package edu.angelo.parentsportal;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;
public class Admin_Parents_Item extends AppCompatActivity {
TextView tv_Id;
TextView tv_Name;
TextView tv_Surname;
TextView tv_Email;
TextView tv_Phone_Number;
TextView tv_Password;
String s_Id;
String s_Name;
String s_Surname;
String s_Email;
String s_Phone_Number;
String s_Password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin__parents__item);
tv_Id = findViewById(R.id.tv_id_info);
tv_Name = findViewById(R.id.tv_name_info);
tv_Surname = findViewById(R.id.tv_surname_info);
tv_Email = findViewById(R.id.tv_email_info);
tv_Phone_Number = findViewById(R.id.tv_PhoneNumber_info);
tv_Password = findViewById(R.id.tv_password_info);
Intent intent = getIntent();
if (intent != null) {
s_Id = intent.getStringExtra("ID");
s_Name = intent.getStringExtra("Name");
s_Surname = intent.getStringExtra("Surname");
s_Email = intent.getStringExtra("Email Address");
s_Phone_Number = intent.getStringExtra("Phone Number");
s_Password = intent.getStringExtra("Password");
}
tv_Id.setText(s_Id);
tv_Name.setText(s_Name);
tv_Surname.setText(s_Surname);
tv_Email.setText(s_Email);
tv_Phone_Number.setText(s_Phone_Number);
tv_Password.setText(s_Password);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.admin__parents_item, 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();
switch (id) {
case R.id.admin_parent_item_action_edit: {
Intent intent = new Intent(this, Admin_Parents_Item_Edit.class);
intent.putExtra("ID", s_Id);
intent.putExtra("Name", s_Name);
intent.putExtra("Surname", s_Surname);
intent.putExtra("Email Address", s_Email);
intent.putExtra("Phone number", s_Phone_Number);
intent.putExtra("Password", s_Password);
startActivity(intent);
finish();
break;
}
}
return super.onOptionsItemSelected(item);
}
}
DatabaseHelper.java
package edu.angelo.parentsportal;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Parents_Portal.db";
public static final String TABLE_NAME = "Parents_Table";
public static final String COL_0 = "ID";
public static final String COL_1 = "NAME";
public static final String COL_2 = "SURNAME";
public static final String COL_3 = "EMAIL_ADDRESS";
public static final String COL_4 = "PHONE_NUMBER";
public static final String COL_5 = "PASSWORD";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME +"(ID INTEGER PRIMARY KEY
AUTOINCREMENT, NAME TEXT, SURNAME TEXT, EMAIL_ADDRESS TEXT, PHONE_NUMBER TEXT,
PASSWORD TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean insertData(String name, String surname, String email_address,
String phone_number, String password) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_1, name);
contentValues.put(COL_2, surname);
contentValues.put(COL_3, email_address);
contentValues.put(COL_4, phone_number);
contentValues.put(COL_5, password);
long result = db.insert(TABLE_NAME, null, contentValues);
if (result == -1) {
return false;
}
else {
return true;
}
}
public Cursor getAllData() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor result = db.rawQuery("select * from " + TABLE_NAME, null);
return result;
}
public ArrayList<ParentModel> getAllParentsData() {
ArrayList<ParentModel> list = new ArrayList<>();
String sql = "select * from " + TABLE_NAME;
SQLiteDatabase mydb = this.getWritableDatabase();
Cursor cursor = mydb.rawQuery(sql, null);
if (cursor.moveToFirst()) {
do {
ParentModel parentModel = new ParentModel();
parentModel.setID(cursor.getString(0));
parentModel.setName(cursor.getString(1));
parentModel.setSurname(cursor.getString(2));
parentModel.setEmail(cursor.getString(3));
parentModel.setPhone_number(cursor.getString(4));
parentModel.setPassword(cursor.getString(5));
list.add(parentModel);
}
while (cursor.moveToNext())
;
}
return list;
}
public void updateData(int id, String name, String surname, String email,
String phone_number, String password) {
ContentValues contentValues = new ContentValues();
String sql = "select * from " + TABLE_NAME;
SQLiteDatabase mydb = this.getWritableDatabase();
Cursor cursor = mydb.rawQuery(sql, null);
if (cursor.moveToFirst()) {
do {
contentValues.put(COL_1, name);
contentValues.put(COL_2, surname);
contentValues.put(COL_3, email);
contentValues.put(COL_4, phone_number);
contentValues.put(COL_5, password);
}
while (cursor.moveToNext());
}
mydb.update(TABLE_NAME, contentValues, COL_0 + "=" + id, null);
mydb.close();
}
}
答案 0 :(得分:0)
您的databaseHelper为空。像下面这样在onCreate()
中对其进行初始化。
DatabaseHelper databaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin__parents__item__edit);
databaseHelper = new DatabaseHelper (this); // Add this line
.........................