2013-10-17 138 views
0

我的sql數據庫的刪除方法不會從數據庫中刪除條目。它給出了一個零指示字例外:從sql數據庫刪除行

10-18 00:44:25.069:E/AndroidRuntime(17968):顯示java.lang.NullPointerException 10-18 00:44:25.069:E/AndroidRuntime(17968) :在com.weather.app.LocationDB.deleteLocation(LocationDB.java:98)

public class LocationDB extends SQLiteOpenHelper { 

private static final int DATABASE_VERSION = 1; 

// Database Name 
private static final String DATABASE_NAME = "locationManager"; 

private static final String TABLE_LOCATIONS = "locations"; 
private static final String KEY_ID = "id"; 
private static final String KEY_LOCATION = "location"; 
private static final String KEY_COUNTRY = "country"; 
private SQLiteDatabase db; 

public LocationDB(Context context) { 
    super(context, DATABASE_NAME, null, DATABASE_VERSION); 
} 

@Override 
public void onCreate(SQLiteDatabase db) { 

    String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_LOCATIONS + " (" 
      + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_LOCATION 
      + " TEXT, " + KEY_COUNTRY 
      + " TEXT, " + " INTEGER)"; 
    db.execSQL(sql); 
} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldV, int newV) { 
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_LOCATIONS); 
    onCreate(db); 
} 
public void addLocation(LocationName location) { 
    SQLiteDatabase db = this.getWritableDatabase(); 

    ContentValues values = new ContentValues(); 
    values.put(KEY_LOCATION, location.getTaskName()); 
    values.put(KEY_COUNTRY, location.getTaskCountry()); 

    db.insert(TABLE_LOCATIONS, null, values); 
} 

public List<LocationName> getAllLocations() { 
    List<LocationName> locList = new ArrayList<LocationName>(); 
    // Select All Query 
    String selectQuery = "SELECT * FROM " + TABLE_LOCATIONS; 

    SQLiteDatabase db = this.getWritableDatabase(); 
    Cursor cursor = db.rawQuery(selectQuery, null); 

    // looping through all rows and adding to list 
    if (cursor.moveToFirst()) { 
     do { 
      LocationName task = new LocationName(); 
      task.setId(cursor.getInt(0)); 
      task.setTaskName(cursor.getString(1)); 
      task.setTaskCountry(cursor.getString(2)); 
      // Adding contact to list 
      locList.add(task); 
     } while (cursor.moveToNext()); 
    } 
    return locList; 
} 
public boolean deleteLocation(long rowId) { 
    return db.delete(TABLE_LOCATIONS, KEY_ID + "=" + rowId, null) > 0; 
} 

}

然後該應用重啓即使當條目沒有被去除,用於添加所述addLocation方法行工作正常。

LocationSettings活動:

public class LocationSettings extends Activity { 
protected static LocationDB db; 
static List<LocationName> list; 
MyAdapter adapt; 
private static TextView name; 
private ListView listView; 
private static final int DLG_EXAMPLE1 = 0; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_location_settings); 
    getActionBar().setDisplayHomeAsUpEnabled(true); 
    getActionBar().setHomeButtonEnabled(true); 
    db = new LocationDB(this); 
    list = db.getAllTasks(); 
    adapt = new MyAdapter(this, R.layout.drawer_list_item, list); 
    listView = (ListView) findViewById(R.id.left_drawer); 
    listView.setAdapter(adapt); 
    SwipeDismissListViewTouchListener touchListener = 
      new SwipeDismissListViewTouchListener(
        listView, 
        new SwipeDismissListViewTouchListener.DismissCallbacks() { 
         @Override 
         public boolean canDismiss(int position) { 
          return true; 
         } 

         @Override 
         public void onDismiss(ListView listView, int[] reverseSortedPositions) { 
          for (int position : reverseSortedPositions) { 
           db.deleteTask(position); 
           adapt.remove(adapt.getItem(position)); 
          } 
         } 
        }); 

    listView.setOnItemClickListener(new DrawerItemClickListener()); 
    listView.setOnTouchListener(touchListener); 

} 
public class DrawerItemClickListener implements ListView.OnItemClickListener { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     db.deleteTask(position); 
     adapt.notifyDataSetChanged(); 
    } 
} 
private class MyAdapter extends ArrayAdapter<LocationName> { 

    Context context; 
    List<LocationName> taskList = new ArrayList<LocationName>(); 
    int layoutResourceId; 

    public MyAdapter(Context context, int layoutResourceId, 
      List<LocationName> objects) { 
     super(context, layoutResourceId, objects); 
     this.layoutResourceId = layoutResourceId; 
     this.taskList = objects; 
     this.context = context; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     if (convertView == null) { 
      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater.inflate(R.layout.drawer_list_item,parent, false); 
      name = (TextView) convertView.findViewById(R.id.text1); 
     } 
     LocationName current = taskList.get(position); 
     name.setText(current.getTaskName()); 
     return convertView; 
    } 

} 

}

回答

2

你忘記deleteLocation調用delete方法之前初始化db

public int deleteLocation(long rowId) { 
    SQLiteDatabase db = this.getWritableDatabase(); 
    return db.delete(TABLE_LOCATIONS, KEY_ID + " = ?", 
          new String[] { String.valueOf(rowId) }); 
} 
+0

添加已停止空指針異常,但它不刪除條目,重新啓動應用程序仍顯示條目存在。日誌貓沒有顯示任何東西。 – GibranG

+0

@GibranG:然後調試代碼並檢查'deleteLocation'是真是假? –

+0

你的代碼在adt中給出了這個錯誤:「Type mismatch:can not convert from int to boolean」 – GibranG

0

使用的代碼的建議下面的片段從@ρяσѕρєяK:

public int deleteLocation(long rowId) { 
    SQLiteDatabase db = this.getWritableDatabase(); 
    return db.delete(TABLE_LOCATIONS, KEY_ID + " = ?", 
          new String[] { String.valueOf(rowId) }); 
} 

改變你的方法生成錯誤的簽名。我認爲

public boolean deleteLocation(long rowId) { 
    SQLiteDatabase db = this.getWritableDatabase(); 
    return db.delete(TABLE_LOCATIONS, KEY_ID + " = ?", 
          new String[] { String.valueOf(rowId) }) > 0; 
} 

將解決您的問題。

+0

刪除方法仍然不起作用,當我重新啓動時,行仍顯示,我應該發佈活動代碼? – GibranG

+0

@GibranG是否發生錯誤或者它不會刪除你想要的東西? – Paolo

+0

沒有錯誤發生,它只是不刪除條目,即使我重新啓動應用程序,條目仍然存在。 – GibranG