確實有人知道如何解決此問題嗎?嗯,我想要的是在我的SQLite數據庫和我在recyclerview項目,但我相處刪除行是此錯誤嘗試刪除sqlite數據庫中的項目時出現空指針異常
java.lang.NullPointerException: Attempt to invoke virtual method 'int petguide.com.petguide101.database_item.reg_Adapter.getItemId()' on a null object reference
at petguide.com.petguide101.database_item.reg_ViewHolder$1.onClick(reg_ViewHolder.java:35)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
這裏是我的代碼爲我的DB
public class Register_pet_database extends SQLiteOpenHelper {
public static final String DATABASE_NAME="Registered_pet.db";
public static final String TABLE_NAME="Pet_Registered";
public static final String ROWID="ID";
public static final String COl_1="Pet_name";
public static final String COl_2="Pet_type";
public static final String COl_3="Pet_breed";
public SQLiteDatabase myDB=getReadableDatabase();
public Register_pet_database(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table "+TABLE_NAME+"("+ROWID+"INTEGER PRIMARY KEY AUTOINCREMENT, Pet_name TEXT, Pet_type TEXT, Pet_breed TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS"+TABLE_NAME);
onCreate(db);
}
public boolean insertDataToPetRegister(String pet_name, String pet_type, String pet_breed){
SQLiteDatabase db=getWritableDatabase();
ContentValues contentValues= new ContentValues();
contentValues.put(COl_1,pet_name);
contentValues.put(COl_2,pet_type);
contentValues.put(COl_3,pet_breed);
long result= db.insert(TABLE_NAME,null,contentValues);
if(result == -1)
return false;
else
return true;
}
public Integer deleteData(String id){
SQLiteDatabase db=this.getWritableDatabase();
return db.delete(TABLE_NAME,"ID=?", new String[]{id});
}
public Cursor quearyData(String query){
return myDB.rawQuery(query,null);
}
public int getId(){
String query = "SELECT id FROM " + TABLE_NAME+ " WHERE ID=?" + ROWID + "'";
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery(query, null);
int id=-1;
if(res!=null&&res.moveToFirst())
id=res.getInt(res.getColumnIndex("ID"));
return id;
}
}
我爲我的適配器
public class reg_Adapter extends RecyclerView.Adapter<reg_ViewHolder>{
Activity activity;
List<db_getItem> items;
ArrayList<db_getItem> arrayList=new ArrayList<>();
db_getItem dbitem;
public reg_Adapter(Activity activity, List<db_getItem> items){
this.activity=activity;
this.items=items;
}
public reg_ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.reg_pet_recycler,parent,false);
return new reg_ViewHolder(view);
}
public void delete(int position) { //removes the row
arrayList.remove(position);
notifyItemRemoved(position);
}
@Override
public void onBindViewHolder(reg_ViewHolder holder, int position) {
holder.pet_name.setText(items.get(position).getPet_name());
holder.pet_type.setText(items.get(position).getPet_type());
holder.pet_breed.setText(items.get(position).getPet_breed());
}
@Override
public int getItemCount() {
return items.size();
}
public int getItemId() {
return dbitem.getPetID();
}
}
我的代碼代碼爲我viewHolder
public class reg_ViewHolder extends RecyclerView.ViewHolder {
Register_pet_database db;
TextView pet_name;
TextView pet_type;
TextView pet_breed;
Button btndel;
Register_pet_database register_pet_database;
reg_Adapter reg_adapter;
DBAdapter dbAdapter;
public reg_ViewHolder(final View itemView) {
super(itemView);
pet_name = (TextView) itemView.findViewById(R.id.pet_reg_name);
pet_type = (TextView) itemView.findViewById(R.id.pet_reg_type);
pet_breed = (TextView) itemView.findViewById(R.id.pet_reg_breed);
btndel =(Button) itemView.findViewById(R.id.btndel);
btndel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int id=reg_adapter.getItemId();
Integer delete=register_pet_database.deleteData(String.valueOf(id));
if(delete>0)
Toast.makeText(itemView.getContext(),"Deleted", Toast.LENGTH_LONG).show();
else
Toast.makeText(itemView.getContext(),"Not Deleted", Toast.LENGTH_LONG).show();
}
});
}
}
能告訴我如何解決這個問題嗎?
'reg_Adapter reg_adapter'永遠不會被分配...... –
不要試圖「在空對象引用上調用虛擬方法'int petguide.com.petguide101.database_item.reg_Adapter.getItemId()',或許? –
所以我應該怎麼做,關於該部分即時通訊混淆我該怎麼辦 –