SQLiteOpenHelper.class
public class DBHelper extends SQLiteOpenHelper{
private final static int DB_VERSION = 1;
private final static String DB_NAME = "places.db";
public DBHelper(Context context){
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql =
"CREATE TABLE " + PlacesContract.Places.TABLE_NAME
+"("
+ PlacesContract.Places._ID + " INTEGER PRIMARY KEY AUTOINCREMENT ,"
+ PlacesContract.Places.NAME + " TEXT ,"
+ PlacesContract.Places.ADDRESS + " TEXT, "
+ PlacesContract.Places.DISTANCE + " TEXT "
+")";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String sql = "DROP TABLE IF EXISTS " + PlacesContract.Places.TABLE_NAME;
db.execSQL(sql);
onCreate(db);
}
}
FragmentA.class :
public class FragmentA extends Fragment implements OnClickListener, OnEditorActionListener, LoaderCallbacks<Cursor>{
SimpleCursorAdapter adapter;
View v;
private LocationManager locationManager;
private String provider;
public static FragmentA newInstance(){
// create a fragment:
FragmentA fragmentA = new FragmentA();
return fragmentA;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.a, container, false);
ListView lv = (ListView) v.findViewById(R.id.list);
//Check Location
locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
//criteria.setPowerRequirement(Criteria.POWER_LOW);
provider = locationManager.getBestProvider(criteria, true);
Log.d("\nProvider: ", provider);
Location lastKnownLocation = locationManager.getLastKnownLocation(provider);
if(lastKnownLocation == null){
Log.d("No Location", provider);
}
else{
Log.d(+ lastKnownLocation.getLatitude() + ", "
+ lastKnownLocation.getLongitude(), provider);
}
String lastLocation;
if(lastKnownLocation != null)
{
lastLocation = lastKnownLocation.getLatitude() + ", "
+ lastKnownLocation.getLongitude();
}
else
{
lastLocation = "Location Not Detected";
}
//End Location
String[] from = {PlacesContract.Places.NAME, PlacesContract.Places.ADDRESS, PlacesContract.Places.DISTANCE};
int[] to = {R.id.location_Name, R.id.location_Address, R.id.location_Distance};
//String imgURL = PlacesContract.Places.ICON;
String[]distance = {PlacesContract.Places.DISTANCE};
Log.d("This is distance", Arrays.toString(distance));
adapter = new SimpleCursorAdapter(getActivity(), R.layout.itemlist_location, null, from, to, 0);
lv.setAdapter(adapter);
getLoaderManager().initLoader(1, null, this);
Button btnSearch = (Button) v.findViewById(R.id.btnSearch);
EditText editSearch = (EditText) v.findViewById(R.id.editSearch);
btnSearch.setOnClickListener(this);
editSearch.setOnClickListener(this);
return v;
}
SearchPlaceService.Class
public class SearchPlacesService extends IntentService{
private static final String TAG = "SearchPlacesService";
public SearchPlacesService(){
super("SearchPlacesService");
}
@Override
protected void onHandleIntent(Intent intent) {
String query = intent.getStringExtra("query");
if(query == null || query.length() < 2){
return;
}
getContentResolver().delete(PlacesContract.Places.CONTENT_URI, null, null);
String result = GoogleAccess.searchPlace(query);
Log.d(TAG, result);
try {
JSONObject jsonResult = new JSONObject(result);
JSONArray resultsArray = jsonResult.getJSONArray("results");
for(int i=0; i<resultsArray.length(); i++){
JSONObject place = resultsArray.getJSONObject(i);
String name = place.getString("name");
String address = place.getString("formatted_address");
String lat = place.getJSONObject("geometry").getJSONObject("location").getString("lng");
String lng = place.getJSONObject("geometry").getJSONObject("location").getString("lat");
String distance = (lat + " " + lng);
ContentValues values = new ContentValues();
values.put(PlacesContract.Places.NAME, name);
values.put(PlacesContract.Places.ADDRESS, address);
values.put(PlacesContract.Places.DISTANCE, distance);
getContentResolver().insert(PlacesContract.Places.CONTENT_URI, values);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
错误: java.lang.IllegalArgumentExeception:column&#34; distance&#34;不存在
如果有人能告诉我问题是什么,我会很高兴,因为我无法找到它。
编辑:我添加了逗号,但我仍然遇到同样的错误。
答案 0 :(得分:3)
你错过了一个逗号:
PlacesContract.Places.ADDRESS + " TEXT " <<<<<<<<