로봇이 나중에 장치에서 가져올 펀치 조합을 정의 할 수있는 로봇 용 앱을 작업하고 있습니다. 사용자가 이러한 교육을 저장할 수 있도록하기 위해 교육의 ID, 이름 및 펀치 조합을 보유하는 "교육"클래스를 정의했습니다. 이 훈련은 나중에 DatabaseHandler 클래스를 작성한 데이터베이스에 저장됩니다. 추가 및 표시하는 데이터가 잘 작동하지만 아래의 방법으로 항목을 삭제할 때마다 :SQL 데이터베이스에서 항목을 삭제 한 후 Nullpointer 예외가 발생했습니다.
public void deleteTraining(Training training) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_TRAININGS, KEY_ID + " = ?",
new String[] { String.valueOf(training.getID()) });
db.close();
}
을 나중에 다시 제의 GridView를 채우기 위해 시도 (A GridAdapter 클래스에서 처리), I는 Nullpointer를 얻을 수 예외
java.lang.NullPointerException이 : com.noeth.tobi에서 null 객체 참조
에 필드 'java.lang.String의 com.noeth.tobi.mcrobektrainingsplaner.Training._name'에서 읽을 수있는 시도. GridView.java : 50)
GridAdapter의의 getView 방법 :
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
// if it's not recycled, initialize some attributes
btn = new Button(context);
btn.setLayoutParams(new GridView.LayoutParams(370, 350));
btn.setPadding(2,100,2,100);
btn.setOnClickListener(new CustomOnClickListener(position, context));
btn.setOnLongClickListener(new CustomOnLongClickListener(position, context, btn));
}
else {
btn = (Button) convertView;
}
btn.setText(db.getTraining(position)._name); //Here the programm throws a Nullpointer Exception AFTER deleting an entry from the database
btn.setTextColor(Color.WHITE);
btn.setBackgroundResource(R.drawable.button_border);
btn.setTag("not_activated");
btn.setId(position);
return btn;
}
내가 루프가 단순히 내가하는 방법을 썼다 있도록 모든 식별자를 통해가는대로, 삭제 된 교육의 ID와 함께 할 수있는 뭔가가 있어야한다는 생각
public void recalcIDs(){
int k = 1;
int subtract = 1;
int id;
Training training;
for(int i = deleted.get(0)+1; i < db.getTrainingCount(); i++){
if(deleted.size() > 1){
if(i < deleted.get(k)){
training = db.getTraining(i);
id = training.getID();
training.setID(id-subtract);
}
else{
k+=1;
subtract+=1;
}
}
else{
training = db.getTraining(i);
id = training.getID();
training.setID(id-subtract);
}
}
}
그러나이하지 않는
recalcIDs 다음 삭제 된 훈련 후 오는 모든 항목의 ID를 다시 계산 recalcIDs 그것을 고쳐라.
앱을 다시 설치하고 완전히 새로운 데이터베이스로 시작하는 경우 모든 기능이 다시 작동합니다.
아무도 내가 잘못한 것을 생각하지 못했습니까?
P.S : 여기가 이름을 찾을 수 없습니다 getTraining 방법이다 : 나는 데이터베이스를 호출하지 않는 Training.setId
방법을 믿고있어
Training getTraining(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Training training;
Cursor cursor = db.query(TABLE_TRAININGS, new String[] { KEY_ID,
KEY_NAME, KEY_SK}, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null && cursor.moveToFirst()){
training = new Training(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getLong(2));
cursor.close();
}
else{
training = null;
Toast.makeText(con,"Couldn't find any training sessions!", Toast.LENGTH_LONG).show();
}
// return training
return training;
}
먼저 db의 유효한 ID를 모두 쿼리 한 다음 위치에 매핑하십시오. –