`Hello so i'm a noobie coder and im trying to code this mobile app on android studio that tracks ur current location and then sends it over to sqlite database when the "start journey" button is being clicked. (location & timestamp will be sent over) . Similarly for the "end journey" button. however the error im encountering right now is that whenever i click the "start journey" button, the mobile app auto exits itself. There are also no databases created if i check at the /data/data//databases directory under device explorer in android studio. What may be the problem?
Tried chatgpt but did not get much useful input hence asking here. Below are my mainactivity.java , activity_main.xml, and DatabaseHelper.java
Exceeded max word count limited so i removed my imports** just assume i have all of the neccessary imports.
Mainactivity.java :
package com.verdefy;
import com.verdefy.DatabaseHelper;
public class MainActivity extends AppCompatActivity
implements OnMapReadyCallback {
private static final String TAG = MainActivity.class.getSimpleName();
private GoogleMap map;
private CameraPosition cameraPosition;
// The entry point to the Places API.
private PlacesClient placesClient;
// The entry point to the Fused Location Provider.
private FusedLocationProviderClient fusedLocationProviderClient;
// A default location (Singapore) and default zoom to use when location permission is
// not granted.
private final LatLng defaultLocation = new LatLng(1.3521, 103.8198);
private static final int DEFAULT_ZOOM = 15;
private static final int PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 1;
private boolean locationPermissionGranted;
// The geographical location where the device is currently located. That is, the last-known
// location retrieved by the Fused Location Provider.
private Location lastKnownLocation;
// Keys for storing activity state.
private static final String KEY_LOCATION = "location";
// Used for selecting the current place.
private static final int M_MAX_ENTRIES = 5;
private String[] likelyPlaceNames;
private String[] likelyPlaceAddresses;
private List[] likelyPlaceAttributions;
private LatLng[] likelyPlaceLatLngs;
// Variables for location tracking
private FusedLocationProviderClient fusedLocationClient;
private LocationCallback locationCallback;
private Location lastLocation;
private float totalDistance = 0;
private SendDataToDatabase sendDataToDatabase;
private LocationRequest locationRequest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sendDataToDatabase = new SendDataToDatabase();
// Retrieve location and camera position from saved instance state.
if (savedInstanceState != null) {
lastKnownLocation = savedInstanceState.getParcelable(KEY_LOCATION);
cameraPosition = savedInstanceState.getParcelable(KEY_CAMERA_POSITION);
}
// [END maps_current_place_on_create_save_instance_state]
// [END_EXCLUDE]
// Retrieve the content view that renders the map.
setContentView(R.layout.activity_main);
// Construct a PlacesClient; Initializes Places SDK for android with the API key
//stored in the buildconfig class
Places.initialize(getApplicationContext(), BuildConfig.PLACES_API_KEY);
placesClient = Places.createClient(this);
// Construct a FusedLocationProviderClient.
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
//Build the map and start it
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
//end
//initialize updateDistanceTextView
// Initialize locationCallback
locationCallback = new LocationCallback() {
// Define the method inside your class (e.g., MainActivity)
private void updateDistanceTextView(float distance) {
// Assuming you have a TextView named distanceTextView
TextView distanceTextView = findViewById(R.id.distanceTextView);
if (distanceTextView != null) {
distanceTextView.setText("Distance: " + distance + " meters");
}
}
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult); //Call superclass method if needed
if (locationResult == null) {
return;
}
for (Location location : locationResult.getLocations()) {
if (lastLocation != null) {
float distance = lastLocation.distanceTo(location);
totalDistance += distance;
updateDistanceTextView(totalDistance);
}
lastLocation = location;
}
}
};
// Inside onCreate() or any appropriate method
locationRequest = LocationRequest.create();
locationRequest.setInterval(10000); // Update interval in milliseconds
locationRequest.setFastestInterval(5000); // Fastest update interval in milliseconds
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); // Set priority
// [END maps_current_place_on_create]
}
// Start location updates
@SuppressLint("MissingPermission")
private void startLocationUpdates() {
fusedLocationProviderClient.requestLocationUpdates(locationRequest,
locationCallback,
Looper.getMainLooper());
}
/**
* Saves the state of the map when the activity is paused.
*/
// [START maps_current_place_on_save_instance_state]
@Override
protected void onSaveInstanceState(Bundle outState) {
if (map != null && lastKnownLocation!= null) {
outState.putParcelable(KEY_LOCATION, lastKnownLocation);
}
super.onSaveInstanceState(outState);
}
// [END maps_current_place_on_save_instance_state]
/**
* Sets up the options menu.
*
* @param menu The options menu.
* @return Boolean.
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.current_place_menu, menu);
return true;
}
/**
* Handles a click on the menu option to get a place.
*
* @param item The menu item to handle.
* @return Boolean.
*/
// [START maps_current_place_on_options_item_selected]
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.option_get_place) {
showCurrentPlace();
}
return true;
}
// [END maps_current_place_on_options_item_selected]
/**
* Manipulates the map when it's available.
* This callback is triggered when the map is ready to be used.
*/
// [START maps_current_place_on_map_ready]
@Override
public void onMapReady(GoogleMap map) {
this.map = map;
// [START_EXCLUDE]
// [START map_current_place_set_info_window_adapter]
// Use a custom info window adapter to handle multiple lines of text in the
// info window contents.
this.map.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
@Override
// Return null here, so that getInfoContents() is called next.
public View getInfoWindow(Marker arg0) {
return null;
}
@Override
public View getInfoContents(Marker marker) {
// Inflate the layouts for the info window, title and snippet.
View infoWindow = getLayoutInflater().inflate(R.layout.custom_info_contents,
(FrameLayout) findViewById(R.id.map), false);
TextView title = infoWindow.findViewById(R.id.title);
title.setText(marker.getTitle());
TextView snippet = infoWindow.findViewById(R.id.snippet);
snippet.setText(marker.getSnippet());
return infoWindow;
}
});
// [END map_current_place_set_info_window_adapter]
// Prompt the user for permission.
getLocationPermission();
// [END_EXCLUDE]
// Turn on the My Location layer and the related control on the map.
updateLocationUI();
// Get the current location of the device and set the position of the map.
getDeviceLocation();
}
// [END maps_current_place_on_map_ready]
/**
* Gets the current location of the device, and positions the map's camera.
*/
// [START maps_current_place_get_device_location]
private void getDeviceLocation() {
/*
* Get the best and most recent location of the device, which may be null in rare
* cases when a location is not available.
*/
try {
if (locationPermissionGranted) {
Task<Location> locationResult = fusedLocationProviderClient.getLastLocation();
locationResult.addOnCompleteListener(this, new OnCompleteListener<Location>() {
@Override
public void onComplete(@NonNull Task<Location> task) {
if (task.isSuccessful()) {
// Set the map's camera position to the current location of the device.
lastKnownLocation = task.getResult();
if (lastKnownLocation != null) {
map.moveCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(lastKnownLocation.getLatitude(),
lastKnownLocation.getLongitude()), DEFAULT_ZOOM));
}
} else {
Log.d(TAG, "Current location is null. Using defaults.");
Log.e(TAG, "Exception: %s", task.getException());
map.moveCamera(CameraUpdateFactory
.newLatLngZoom(defaultLocation, DEFAULT_ZOOM));
map.getUiSettings().setMyLocationButtonEnabled(false);
}
}
});
}
} catch (SecurityException e) {
Log.e("Exception: %s", e.getMessage(), e);
}
}
// [END maps_current_place_get_device_location]
/**
* Prompts the user for permission to use the device location.
*/
// [START maps_current_place_location_permission]
private void getLocationPermission() {
/*
* Request location permission, so that we can get the location of the
* device. The result of the permission request is handled by a callback,
* onRequestPermissionsResult.
*/
if (ContextCompat.checkSelfPermission(this.getApplicationContext(),
android.Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
locationPermissionGranted = true;
} else {
ActivityCompat.requestPermissions(this,
new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
}
}
// [END maps_current_place_location_permission]
/**
* Handles the result of the request for location permissions.
*/
// [START maps_current_place_on_request_permissions_result]
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String[] permissions,
@NonNull int[] grantResults) {
locationPermissionGranted = false;
if (requestCode
== PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION) {// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
locationPermissionGranted = true;
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
updateLocationUI();
}
// [END maps_current_place_on_request_permissions_result]
/**
* Prompts the user to select the current place from a list of likely places, and shows the
* current place on the map - provided the user has granted location permission.
*/
// [START maps_current_place_show_current_place]
private void showCurrentPlace() {
if (map == null) {
return;
}
if (locationPermissionGranted) {
// Use fields to define the data types to return.
List<Place.Field> placeFields = Arrays.asList(Place.Field.NAME, Place.Field.ADDRESS,
Place.Field.LAT_LNG);
// Use the builder to create a FindCurrentPlaceRequest.
FindCurrentPlaceRequest request =
FindCurrentPlaceRequest.newInstance(placeFields);
// Get the likely places - that is, the businesses and other points of interest that
// are the best match for the device's current location.
@SuppressWarnings("MissingPermission") final Task<FindCurrentPlaceResponse> placeResult =
placesClient.findCurrentPlace(request);
placeResult.addOnCompleteListener(new OnCompleteListener<FindCurrentPlaceResponse>() {
@Override
public void onComplete(@NonNull Task<FindCurrentPlaceResponse> task) {
if (task.isSuccessful() && task.getResult() != null) {
FindCurrentPlaceResponse likelyPlaces = task.getResult();
// Set the count, handling cases where less than 5 entries are returned.
int count;
if (likelyPlaces.getPlaceLikelihoods().size() < M_MAX_ENTRIES) {
count = likelyPlaces.getPlaceLikelihoods().size();
} else {
count = M_MAX_ENTRIES;
}
int i = 0;
likelyPlaceNames = new String[count];
likelyPlaceAddresses = new String[count];
likelyPlaceAttributions = new List[count];
likelyPlaceLatLngs = new LatLng[count];
for (PlaceLikelihood placeLikelihood : likelyPlaces.getPlaceLikelihoods()) {
// Build a list of likely places to show the user.
likelyPlaceNames[i] = placeLikelihood.getPlace().getName();
likelyPlaceAddresses[i] = placeLikelihood.getPlace().getAddress();
likelyPlaceAttributions[i] = placeLikelihood.getPlace()
.getAttributions();
likelyPlaceLatLngs[i] = placeLikelihood.getPlace().getLatLng();
i++;
if (i > (count - 1)) {
break;
}
}
// Show a dialog offering the user the list of likely places, and add a
// marker at the selected place.
MainActivity.this.openPlacesDialog();
} else {
Log.e(TAG, "Exception: %s", task.getException());
}
}
});
} else {
// The user has not granted permission.
Log.i(TAG, "The user did not grant location permission.");
// Add a default marker, because the user hasn't selected a place.
map.addMarker(new MarkerOptions()
.title(getString(R.string.default_info_title))
.position(defaultLocation)
.snippet(getString(R.string.default_info_snippet)));
// Prompt the user for permission.
getLocationPermission();
}
}
// [END maps_current_place_show_current_place]
/**
* Displays a form allowing the user to select a place from a list of likely places.
*/
// [START maps_current_place_open_places_dialog]
private void openPlacesDialog() {
// Ask the user to choose the place where they are now.
DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// The "which" argument contains the position of the selected item.
LatLng markerLatLng = likelyPlaceLatLngs[which];
String markerSnippet = likelyPlaceAddresses[which];
if (likelyPlaceAttributions[which] != null) {
markerSnippet = markerSnippet + "\n" + likelyPlaceAttributions[which];
}
// Add a marker for the selected place, with an info window
// showing information about that place.
map.addMarker(new MarkerOptions()
.title(likelyPlaceNames[which])
.position(markerLatLng)
.snippet(markerSnippet));
// Position the map's camera at the location of the marker.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(markerLatLng,
DEFAULT_ZOOM));
}
};
// Display the dialog.
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle(R.string.pick_place)
.setItems(likelyPlaceNames, listener)
.show();
}
// [END maps_current_place_open_places_dialog]
/**
* Updates the map's UI settings based on whether the user has granted location permission.
*/
// [START maps_current_place_update_location_ui]
private void updateLocationUI() {
if (map == null) {
return;
}
try {
if (locationPermissionGranted) {
map.setMyLocationEnabled(true);
map.getUiSettings().setMyLocationButtonEnabled(true);
} else {
map.setMyLocationEnabled(false);
map.getUiSettings().setMyLocationButtonEnabled(false);
lastKnownLocation = null;
getLocationPermission();
}
} catch (SecurityException e) {
Log.e("Exception: %s", e.getMessage());
}
}
// [END maps_current_place_update_location_ui]
private String getLocationName(double latitude, double longitude) {
// Your implementation to get the location name based on latitude and longitude
// This method should return the name of the location based on the provided coordinates
// You may use geocoding or reverse geocoding to achieve this
Geocoder geocoder = new Geocoder(MainActivity.this, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
if (addresses != null && addresses.size() > 0) {
Address address = addresses.get(0);
return address.getAddressLine(0); // You can customize this as per your requirements
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private String getCurrentTimestamp() {
// Get current timestamp
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String timestamp = sdf.format(new Date());
return timestamp;
}
// Your class definition
public class SendDataToDatabase {
// Your other methods
//START a journey and start tracking
// Send location and timestamp to the database
// Start tracking user's location
// You can use the Google Maps Directions API to calculate distance and carbon emission
// Start a timer or capture the start time for duration calculation
public void startJourney(View view) {
// Get current location and timestamp
if (lastKnownLocation != null) {
String startLocation = getLocationName(lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude());
String startTimeStamp = getCurrentTimestamp();
// Assuming addTravelData is a method of DatabaseHelper class
DatabaseHelper dbHelper = new DatabaseHelper(MainActivity.this); // Assuming this is your activity context
dbHelper.addTravelData(startLocation, startTimeStamp,null, null);
}
}
// END A JOURNEY, stop all tracking
public void endJourney(View view) {
// Stop tracking user's location
if (lastKnownLocation != null) {
String endLocation = getLocationName(lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude());
String endTimeStamp = getCurrentTimestamp();
DatabaseHelper dbHelper = new DatabaseHelper(MainActivity.this);
// Save end location and timestamp to the database
dbHelper.addTravelData(null,null, endLocation, endTimeStamp);
}
// Calculate distance travelled
// Calculate time duration
// Calculate carbon emission if possible
// Send all captured data (distance, duration, type of vehicle, etc.) to the database
}
// Method to handle start journey button click
public void onStartJourneyButtonClick(View view) {
sendDataToDatabase.startJourney(view);
}
// Method to handle end journey button click
public void onEndJourneyButtonClick(View view) {
sendDataToDatabase.endJourney(view);
}
}
}
2) Activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/dark_green"
tools:context=".MainActivity">
<TextView
android:id="@+id/NewJourneyTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/jua"
android:text="Start a New Journey"
android:textColor="@color/white"
android:textSize="25sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/map"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.178" />
<Button
android:id="@+id/startButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="44dp"
android:layout_marginBottom="72dp"
android:onClick="onStartJourneyButtonClick"
android:text="Start Journey"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:ignore="MissingConstraints" />
<Button
android:id="@+id/EndButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="44dp"
android:layout_marginBottom="72dp"
android:onClick="onEndJourneyButtonClick"
android:text="End Journey"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
tools:ignore="MissingConstraints" />
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="322dp"
android:layout_height="444dp"
android:layout_marginBottom="144dp"
android:background="@drawable/rounded_corners"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.494"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/distanceTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="100dp"
android:layout_marginTop="104dp"
android:padding="8dp"
android:text="Distance: "
android:textSize="16sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
3)DatabaseHelper.java
package com.verdefy;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.content.ContentValues;
// Create the database
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASENAME = "VERDEFY";
private static final int DATABASE_VERSION = 1;
public static final String TABLE_TRAVELDATA = "travel_data";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_STARTLOCATION = "startLocation";
public static final String COLUMN_ENDLOCATION = "endLocation";
public static final String COLUMN_STARTTIMESTAMP = "startTimeStamp";
public static final String COLUMN_ENDTIMESTAMP = "endTimeStamp";
private static final String TABLE_CREATE =
"CREATE TABLE " + TABLE_TRAVELDATA + " (" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_STARTLOCATION + " TEXT, " +
COLUMN_ENDLOCATION + " TEXT, " +
COLUMN_STARTTIMESTAMP + " DATETIME DEFAULT CURRENT_TIMESTAMP, " +
COLUMN_ENDTIMESTAMP + " DATETIME DEFAULT CURRENT_TIMESTAMP)";
public DatabaseHelper(Context context) {
super(context, DATABASENAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(TABLE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_TRAVELDATA);
onCreate(db);
}
// Add this method to your DatabaseHelper class
public void addTravelData(String startLocation, String startTimeStamp, String endLocation, String endTimeStamp) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_STARTLOCATION, startLocation);
values.put(COLUMN_STARTTIMESTAMP, startTimeStamp);
values.put(COLUMN_ENDLOCATION, endLocation);
values.put(COLUMN_ENDTIMESTAMP, endTimeStamp);
db.insert(TABLE_TRAVELDATA, null, values);
db.close();
}
}





