所以,我有一个活动(按下按钮)会将一个String,double,double发送到DatabaseHandler
类,它将把它保存到数据库中。当我使用db.addLocation(new Location(name, lat, lng));
时,我收到了错误
The addLocation(com.example.stands.Location) in the type
数据库处理器is not applicable for the arguments android.location.Location
Activity.java:
package com.example.stands;
import com.example.stands.DatabaseHandler;
public class AddLocation extends Activity implements LocationListener {
private Button saveLocation;
DatabaseHandler db;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_location);
saveLocation = (Button) findViewById(R.id.saveLocation);
db = new DatabaseHandler(this);
// Get the location manager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define the criteria how to select the locatioin provider -> use
// default
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
final Location location = locationManager.getLastKnownLocation(provider);
saveLocation.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
insert("McDonalds",129.092093. 103.39284);
}
});
}
public void insert(String name, double lat, double lng) {
db.addLocation(new Location(name, lat, lng));
}
}
DatabaseHandler.java:
package com.example.stands;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHandler extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "locationsManager";
// Locations table name
private static final String TABLE_LOCATIONS = "locations";
// Locations Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_LONG = "long";
private static final String KEY_LAT = "lat";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_LOCATIONS_TABLE = "CREATE TABLE " + TABLE_LOCATIONS + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
+ KEY_LONG + " DOUBLE,"
+ KEY_LAT + " DOUBLE" + ")";
db.execSQL(CREATE_LOCATIONS_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_LOCATIONS);
// Create tables again
onCreate(db);
}
/**
* All CRUD(Create, Read, Update, Delete) Operations
*/
// Adding new location
void addLocation(Location location) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, location.getName()); // location Name
values.put(KEY_LONG, location.getLongitude()); // location Latitude
values.put(KEY_LAT, location.getLatitude());
// Inserting Row
db.insert(TABLE_LOCATIONS, null, values);
db.close(); // Closing database connection
}
// Getting single location
Location getLocation(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_LOCATIONS, new String[] { KEY_ID,
KEY_NAME, KEY_LONG }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Location location = new Location(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), Double.parseDouble(cursor.getString(2)), Double.parseDouble(cursor.getString(3)));
// return location
return location;
}
// Getting All locations
public List<Location> getAllLocations() {
List<Location> locationList = new ArrayList<Location>();
// 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 {
Location location = new Location();
location.setID(Integer.parseInt(cursor.getString(0)));
location.setName(cursor.getString(1));
location.setLongitude(Double.parseDouble(cursor.getString(2)));
location.setLatitude(Double.parseDouble(cursor.getString(3)));
// Adding location to list
locationList.add(location);
} while (cursor.moveToNext());
}
// return location list
return locationList;
}
// Updating single location
public int updateLocation(Location location) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, location.getName());
values.put(KEY_LONG, location.getLongitude());
values.put(KEY_LAT, location.getLatitude());
// updating row
return db.update(TABLE_LOCATIONS, values, KEY_ID + " = ?",
new String[] { String.valueOf(location.getID()) });
}
// Deleting single location
public void deleteLocation(Location location) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_LOCATIONS, KEY_ID + " = ?",
new String[] { String.valueOf(location.getID()) });
db.close();
}
// Getting location Count
public int getLocationCount() {
String countQuery = "SELECT * FROM " + TABLE_LOCATIONS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}
}
Location.java:
public class Location {
// private variables
int id;
String name;
double longitude;
double latitude;
// Empty constructor
public Location() {
}
// constructor
public Location(int id, String name, double longitude, double latitude) {
this.id = id;
this.name = name;
this.longitude = longitude;
this.latitude = latitude;
}
// constructor
public Location(String name, double longitude, double latitude) {
this.name = name;
this.longitude = longitude;
this.latitude = latitude;
}
// getting ID
public int getID() {
return this.id;
}
// setting id
public void setID(int id) {
this.id = id;
}
// getting name
public String getName() {
return this.name;
}
// setting name
public void setName(String name) {
this.name = name;
}
// getting phone number
public Double getLatitude() {
return this.latitude;
}
public Double getLongitude() {
return this.longitude;
}
// setting phone number
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
}
有关我为什么会收到此错误的任何想法?
编辑:我之前在另一项活动中使用了DatabaseHandler
类,但它运行良好。
答案 0 :(得分:0)
您在使用com.example.stands.Location(the class that you created)
的活动中DatabaseHandler
使用android.location.Location
,这会给您出错。
更改
db.addLocation(new Location(name, lat, lng));
到
db.addLocation(new com.example.stands.Location(name, lat, lng));