I tried to link the connection firebase to android studio in my app. However, i can’t seem to run the application even though there is no coding error. I’ll provide the code below.
below is the java code.
<code>package com.example.rwn;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class ReportActivity extends AppCompatActivity {
private EditText editTextName, editTextBinLocation, editTextProblem;
private Button buttonSubmit;
private DatabaseReference databaseReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_report);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
// Initialize Firebase Database
databaseReference = FirebaseDatabase.getInstance().getReference("reports");
// Initialize UI elements
editTextName = findViewById(R.id.editTextName);
editTextBinLocation = findViewById(R.id.editTextBinLocation);
editTextProblem = findViewById(R.id.editTextProblem);
buttonSubmit = findViewById(R.id.buttonSubmit);
// Set onClick listener for the submit button
buttonSubmit.setOnClickListener(v -> submitReport());
}
private void submitReport() {
String name = editTextName.getText().toString().trim();
String binLocation = editTextBinLocation.getText().toString().trim();
String problem = editTextProblem.getText().toString().trim();
if (name.isEmpty() || binLocation.isEmpty() || problem.isEmpty()) {
Toast.makeText(this, "Please fill all fields", Toast.LENGTH_SHORT).show();
return;
}
String id = databaseReference.push().getKey();
Report report = new Report(id, name, binLocation, problem);
assert id != null;
databaseReference.child(id).setValue(report)
.addOnSuccessListener(aVoid -> {
Toast.makeText(this, "Report submitted", Toast.LENGTH_SHORT).show();
// Clear fields after submission
editTextName.setText("");
editTextBinLocation.setText("");
editTextProblem.setText("");
})
.addOnFailureListener(e -> Toast.makeText(this, "Failed to submit report", Toast.LENGTH_SHORT).show());
}
}
</code>
<code>package com.example.rwn;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class ReportActivity extends AppCompatActivity {
private EditText editTextName, editTextBinLocation, editTextProblem;
private Button buttonSubmit;
private DatabaseReference databaseReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_report);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
// Initialize Firebase Database
databaseReference = FirebaseDatabase.getInstance().getReference("reports");
// Initialize UI elements
editTextName = findViewById(R.id.editTextName);
editTextBinLocation = findViewById(R.id.editTextBinLocation);
editTextProblem = findViewById(R.id.editTextProblem);
buttonSubmit = findViewById(R.id.buttonSubmit);
// Set onClick listener for the submit button
buttonSubmit.setOnClickListener(v -> submitReport());
}
private void submitReport() {
String name = editTextName.getText().toString().trim();
String binLocation = editTextBinLocation.getText().toString().trim();
String problem = editTextProblem.getText().toString().trim();
if (name.isEmpty() || binLocation.isEmpty() || problem.isEmpty()) {
Toast.makeText(this, "Please fill all fields", Toast.LENGTH_SHORT).show();
return;
}
String id = databaseReference.push().getKey();
Report report = new Report(id, name, binLocation, problem);
assert id != null;
databaseReference.child(id).setValue(report)
.addOnSuccessListener(aVoid -> {
Toast.makeText(this, "Report submitted", Toast.LENGTH_SHORT).show();
// Clear fields after submission
editTextName.setText("");
editTextBinLocation.setText("");
editTextProblem.setText("");
})
.addOnFailureListener(e -> Toast.makeText(this, "Failed to submit report", Toast.LENGTH_SHORT).show());
}
}
</code>
package com.example.rwn;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class ReportActivity extends AppCompatActivity {
private EditText editTextName, editTextBinLocation, editTextProblem;
private Button buttonSubmit;
private DatabaseReference databaseReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_report);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
// Initialize Firebase Database
databaseReference = FirebaseDatabase.getInstance().getReference("reports");
// Initialize UI elements
editTextName = findViewById(R.id.editTextName);
editTextBinLocation = findViewById(R.id.editTextBinLocation);
editTextProblem = findViewById(R.id.editTextProblem);
buttonSubmit = findViewById(R.id.buttonSubmit);
// Set onClick listener for the submit button
buttonSubmit.setOnClickListener(v -> submitReport());
}
private void submitReport() {
String name = editTextName.getText().toString().trim();
String binLocation = editTextBinLocation.getText().toString().trim();
String problem = editTextProblem.getText().toString().trim();
if (name.isEmpty() || binLocation.isEmpty() || problem.isEmpty()) {
Toast.makeText(this, "Please fill all fields", Toast.LENGTH_SHORT).show();
return;
}
String id = databaseReference.push().getKey();
Report report = new Report(id, name, binLocation, problem);
assert id != null;
databaseReference.child(id).setValue(report)
.addOnSuccessListener(aVoid -> {
Toast.makeText(this, "Report submitted", Toast.LENGTH_SHORT).show();
// Clear fields after submission
editTextName.setText("");
editTextBinLocation.setText("");
editTextProblem.setText("");
})
.addOnFailureListener(e -> Toast.makeText(this, "Failed to submit report", Toast.LENGTH_SHORT).show());
}
}
<code>package com.example.rwn;
public class Report {
private String id;
private String name;
private String binLocation;
private String problem;
public Report() {
// Default constructor required for calls to DataSnapshot.getValue(Report.class)
}
public Report(String id, String name, String binLocation, String problem) {
this.id = id;
this.name = name;
this.binLocation = binLocation;
this.problem = problem;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getBinLocation() {
return binLocation;
}
public String getProblem() {
return problem;
}
}
</code>
<code>package com.example.rwn;
public class Report {
private String id;
private String name;
private String binLocation;
private String problem;
public Report() {
// Default constructor required for calls to DataSnapshot.getValue(Report.class)
}
public Report(String id, String name, String binLocation, String problem) {
this.id = id;
this.name = name;
this.binLocation = binLocation;
this.problem = problem;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getBinLocation() {
return binLocation;
}
public String getProblem() {
return problem;
}
}
</code>
package com.example.rwn;
public class Report {
private String id;
private String name;
private String binLocation;
private String problem;
public Report() {
// Default constructor required for calls to DataSnapshot.getValue(Report.class)
}
public Report(String id, String name, String binLocation, String problem) {
this.id = id;
this.name = name;
this.binLocation = binLocation;
this.problem = problem;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getBinLocation() {
return binLocation;
}
public String getProblem() {
return problem;
}
}
let me know if i need to provide more context/code
I modified the gradle code to suit my project needs. I also added a line to connect to the internet in the manifest.xml file below is the gradle code.
(project level)
<code>// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath("com.android.tools.build:gradle:8.0.0") // Use the latest stable version
classpath("com.google.gms:google-services:4.3.10")
}
}
plugins {
// Use the latest stable version of the Android Application plugin and Google Services plugin
id("com.android.application") version "8.0.0" apply false
id("com.google.gms.google-services") version "4.3.10" apply false
}
allprojects {
repositories {
google()
mavenCentral()
}
}
</code>
<code>// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath("com.android.tools.build:gradle:8.0.0") // Use the latest stable version
classpath("com.google.gms:google-services:4.3.10")
}
}
plugins {
// Use the latest stable version of the Android Application plugin and Google Services plugin
id("com.android.application") version "8.0.0" apply false
id("com.google.gms.google-services") version "4.3.10" apply false
}
allprojects {
repositories {
google()
mavenCentral()
}
}
</code>
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath("com.android.tools.build:gradle:8.0.0") // Use the latest stable version
classpath("com.google.gms:google-services:4.3.10")
}
}
plugins {
// Use the latest stable version of the Android Application plugin and Google Services plugin
id("com.android.application") version "8.0.0" apply false
id("com.google.gms.google-services") version "4.3.10" apply false
}
allprojects {
repositories {
google()
mavenCentral()
}
}
(app level)
<code>plugins {
id("com.android.application")
id("com.google.gms.google-services")
}
android {
namespace = "com.example.rwn"
compileSdk = 34
defaultConfig {
applicationId = "com.example.rwn"
minSdk = 24
targetSdk = 34
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
multiDexEnabled = true
}
buildTypes {
release {
isMinifyEnabled = false
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
}
dependencies {
implementation(platform("com.google.firebase:firebase-bom:33.0.0"))
implementation("androidx.multidex:multidex:2.0.1")
implementation("androidx.appcompat:appcompat:1.4.0")
implementation("com.google.android.material:material:1.4.0")
implementation("androidx.constraintlayout:constraintlayout:2.1.3")
implementation("androidx.activity:activity:1.4.0")
implementation("com.google.firebase:firebase-analytics")
implementation("com.google.firebase:firebase-database")
implementation("com.github.denzcoskun:ImageSlideShow:0.0.6")
implementation("com.github.PhilJay:MPAndroidChart:v3.1.0")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.3")
androidTestImplementation("androidx.test.espresso:espresso-core:3.4.0")
}
</code>
<code>plugins {
id("com.android.application")
id("com.google.gms.google-services")
}
android {
namespace = "com.example.rwn"
compileSdk = 34
defaultConfig {
applicationId = "com.example.rwn"
minSdk = 24
targetSdk = 34
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
multiDexEnabled = true
}
buildTypes {
release {
isMinifyEnabled = false
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
}
dependencies {
implementation(platform("com.google.firebase:firebase-bom:33.0.0"))
implementation("androidx.multidex:multidex:2.0.1")
implementation("androidx.appcompat:appcompat:1.4.0")
implementation("com.google.android.material:material:1.4.0")
implementation("androidx.constraintlayout:constraintlayout:2.1.3")
implementation("androidx.activity:activity:1.4.0")
implementation("com.google.firebase:firebase-analytics")
implementation("com.google.firebase:firebase-database")
implementation("com.github.denzcoskun:ImageSlideShow:0.0.6")
implementation("com.github.PhilJay:MPAndroidChart:v3.1.0")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.3")
androidTestImplementation("androidx.test.espresso:espresso-core:3.4.0")
}
</code>
plugins {
id("com.android.application")
id("com.google.gms.google-services")
}
android {
namespace = "com.example.rwn"
compileSdk = 34
defaultConfig {
applicationId = "com.example.rwn"
minSdk = 24
targetSdk = 34
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
multiDexEnabled = true
}
buildTypes {
release {
isMinifyEnabled = false
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
}
dependencies {
implementation(platform("com.google.firebase:firebase-bom:33.0.0"))
implementation("androidx.multidex:multidex:2.0.1")
implementation("androidx.appcompat:appcompat:1.4.0")
implementation("com.google.android.material:material:1.4.0")
implementation("androidx.constraintlayout:constraintlayout:2.1.3")
implementation("androidx.activity:activity:1.4.0")
implementation("com.google.firebase:firebase-analytics")
implementation("com.google.firebase:firebase-database")
implementation("com.github.denzcoskun:ImageSlideShow:0.0.6")
implementation("com.github.PhilJay:MPAndroidChart:v3.1.0")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.3")
androidTestImplementation("androidx.test.espresso:espresso-core:3.4.0")
}